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

Can a script automatically reload from where it stopped?

Started by jollwish, 12 June 2015 - 02:49 PM
jollwish #1
Posted 12 June 2015 - 04:49 PM
Hi everyone, I'm an absolute noob :P/>

When I'm designing my mining, farming and felling turtles, I find that after I exited the game and came back, all the computers and turtles stop working. I know that I can name the lua scripts as 'startup' so that they can be automatically run, but you see the working process is hardwared in the lua scripts and I cannot let it start from where it stopped…

So it there a way to resolve the problem? Thanks in advance.
Creator #2
Posted 12 June 2015 - 06:02 PM
If you restart the compute inbetween, then no. However, if you don't there is a way. It will use coroutines. Here is how:

local func = loadfile(path) -- transform file content into function
local co = coroutine.create(func)
coroutine.resume(co,arguments) -- runs coro for the first time with specified arguments
while true do
  coroutine.resume(co,os.pullEvent())
  -- do stuff
end
Lignum #3
Posted 12 June 2015 - 06:14 PM
If you restart the compute inbetween, then no. However, if you don't there is a way. It will use coroutines. Here is how:

local func = loadfile(path) -- transform file content into function
local co = coroutine.create(func)
coroutine.resume(co,arguments) -- runs coro for the first time with specified arguments
while true do
  coroutine.resume(co,os.pullEvent())
  -- do stuff
end

You would have to process the requested events as well. os.pullEvent("key") would pull any event, instead of restricting it to key events.
But regardless, that solution has little relevance to the problem.

——-

The proper way to accomplish this is to save the program's state to a file. Then, when your program starts, you can read that file to see where it left off. What exactly you save to your file depends entirely on how your program functions, so we can't help you much with that. Take a look at the FS api, which allows you to work with the file system.
KingofGamesYami #4
Posted 12 June 2015 - 06:56 PM
Basically, you're going to have to re-write your scripts to save every movement or action it performs. Then, you need to do even more re-writing to make it load what you saved previously and continue from there.
Bomb Bloke #5
Posted 13 June 2015 - 02:52 AM
Most of my turtle-based scripts start out by getting their current position using the GPS API, then moving the turtle back to its starting position and resuming operations from there.
jollwish #6
Posted 13 June 2015 - 03:12 AM
Most of my turtle-based scripts start out by getting their current position using the GPS API, then moving the turtle back to its starting position and resuming operations from there.
Nice idea! It indeed resolve most of my turtle-reload problem! :D/>