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

Utilizing the FS API

Started by potatostem, 05 January 2014 - 01:26 PM
potatostem #1
Posted 05 January 2014 - 02:26 PM
I'm still somewhat new to computercraft, so bear with me if this question is poorly worded.
I'm making a program that manages a tree farm with a turtle that activates at the end of the day. The issue I have is that the turtle takes a minute or two to finish, and logging out during that time requires me to manually reset it. I've done some research and it seems like I need to use the fs api to create files (I'm trying to save the turtle's position and what it's doing when the world is closed/the chunk is unloaded + have it recover the saved data and continue upon starting up again), but I'm having trouble finding tutorials and I'm pretty lost. Is there a better way to accomplish what I need, and if not can someone help me understand what I need to do?
Moody #2
Posted 05 January 2014 - 02:48 PM
here http://computercraft.info/wiki/Fs.open
i would store the current state to a table (eg all coordinates and a jobqueue)
serialize this table and store it to a file after a job is done. ( http://computercraft...Textutils_(API) )
then on startup i would read this file and let it continue.

where exactly are you having trouble?

EDIT: Quote from another recent post to demonstrate the FS api

so for example you have a file named "program" in the same directory and you want to edit the 3rd line and want to add a word to it

strT = { }
h = fs.open("program","r") -- create a READONLY-handle for the file
tmp = h.readLine() -- read the first line
while tmp ~= nil  -- if it exists, it will dump every line to an entry in the table
  strT((#strT)+1) = tmp
  tmp = h.readLine()
end
h.close() -- close the file

strT[3] = strT[3].." print(randomStr)"  -- manipulate the 3rd line

h = fs.open("program","w") -- create a WRITE handle for the file
for i=1,#strT,1 -- write everything back to the file
   h.writeLine(strT[i])
end
h.close() -- close it again
adds a print to the 3rd line
be carefull, as writing to a file overwrites everything (you cant directly write to a single line)
Edited on 05 January 2014 - 01:49 PM
potatostem #3
Posted 05 January 2014 - 03:59 PM
Mostly my problem is that I don't know how each thing works in relation to everything else.

I checked the wiki before posting and it's helpful for knowing what each line of code does, but not for knowing what I should be using or how to combine it and make it work. That's why I looked for a tutorial, but unfortunately I couldn't find anything helpful.

I'll see what I can put together with what you've provided, thank you!
Moody #4
Posted 05 January 2014 - 04:45 PM
for a tutorial see here ( http://www.computercraft.info/forums2/index.php?/topic/10654-storing-variables-in-a-file )
(really rudimentary)
i dont think there is any other solution than the FS api in CC.
you basically need a handle to acess the file:
handle = fs.open("filename","w")
then you can write/read/append the file anyway you want by using the handle:
handle.readLine()
you have to close the file at the end, to get the information stored and to avoid errors:
handle.close()
thats basically all you need

its no problem if you dont know everything, because you will need everything eventually.
so even if you try something that doesnt work in this case, it can only help you to get to know what it can. Just program ahead and learn from your mistakes! :)/>
Edited on 05 January 2014 - 03:48 PM
Bomb Bloke #5
Posted 05 January 2014 - 06:36 PM
My preferred method is to use the GPS API. When the turtle boots up, it can use that to work out where it is in the world, and you can then have it attempt to move itself to where the rest of the script is expecting it to start from.

The idea is to perform two GPS polls - one so the turtle knows where it is, then you have the turtle move forward a square and perform another poll. The difference between the two locations can be used to determine in which direction the turtle is facing.

In this way, you can replace the process of flogging the server due to disk writes for every movement the turtle makes, with one short segment of code at the start of your script. Here's one of my scripts that uses the GPS API, and here's a guide to building a satellite.