This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Plystire's profile picture

[Question] Do computers run Termiante code when world is unloaded?

Started by Plystire, 26 April 2012 - 02:49 PM
Plystire #1
Posted 26 April 2012 - 04:49 PM
Pretty simple question. I want to know if computers can run any sort of code prior to the world being unloaded (IE when player quits to title)

The main problem with automating a task is having the task carry on when the world is reloaded. This is especially troublesome with turtles since they need to know where they're at, where they left off, and what to keep doing.

I think I have a system in place to do this with my turtles mining, but I don't want to be saving their state after every single block they break. It'd be nice if I could just save their states immediately before the world is unloaded.

Thanks in advance for any suggestions! :)/>/>

~Plystire
MysticT #2
Posted 26 April 2012 - 07:15 PM
There's no way to do that. You have to save the state after each action or after some time.
This was already suggested and discused in the suggestions forum.
Ceirenthir #3
Posted 26 April 2012 - 07:15 PM
I'd also like to know if this is possible. And if it's not, could it possibly be implemented? Would solve a lot of problems, notably ones similar to the OP stated.

edit: Oh, guess it isn't. Nevermind then, Lol.
libraryaddict #4
Posted 26 April 2012 - 08:18 PM
Its rather easy to save the state.

dofile("state") --Put at start of your file

function SaveState()
  Boobies = io.open("state", "w")
  Boobies:write("BlocksMined = "..[["]]..BlocksMined..[["]])
  Boobies:write("X = "..[["]]..X..[["]])
  Boobies:write("Y = "..[["]]..Y..[["]])
  Boobies:close()
end

Just SaveState after every so and so :)/>/>
Plystire #5
Posted 26 April 2012 - 09:58 PM
Saving a state is not always an option, though…

I wrote a recursive program for a turtle to search for and mine out pockets of ore. Suppose the turtle is in the middle of mining out a vein of coal or something, then I close out of the world… that's a nested function call… there's no way for me to restore that state.

It's a real pity that there's no way to save a lua script's state and restore it between world loads. That would make turtles actually viable for full automation. :)/>/>

~Plystire
Plystire #6
Posted 26 April 2012 - 10:01 PM
Hate to doublepost, but the editor completely f-ed up my last post when I tried to edit it.

libraryaddict, what does the "dofile" command do? I've not seen that mentioned before.

~Plystire
MysticT #7
Posted 26 April 2012 - 10:23 PM
libraryaddict, what does the "dofile" command do? I've not seen that mentioned before.
dofile loads and executes a file. It's almost the same as doing this:

local file = fs.open("path/file", "r")
if file then
  local func, err = loadstring(file.readAll())
  if func then
    func()
  end
end
The function is in the bios.lua file if you want to take a look at it, it's almost the same as that.