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

turtle quarry with restart after server shutdown

Started by Cing, 07 April 2015 - 04:01 PM
Cing #1
Posted 07 April 2015 - 06:01 PM
I have already looked for this problem and i found a post,
but there wasn't a clear answer to this problem.

So could please someone help.
I have already made a direction program
http://pastebin.com/gkUuzUev
but now i only need a way to get this information back.

thanks in regards
HPWebcamAble #2
Posted 07 April 2015 - 07:35 PM
Every time the turtle changes its position, you'd need to save the x,y, and z to a file
Then, when the turtle starts, you assume the turtle is starting at the position on the file

Alternatively, you could have a GPS server tower, and have the turtle find its location using that when it starts up
Cing #3
Posted 07 April 2015 - 08:01 PM
thanks for the tip :)/>
But how do I write to a file and get the information back from the file?
Lupus590 #4
Posted 07 April 2015 - 08:17 PM
fs api
HPWebcamAble #5
Posted 08 April 2015 - 04:47 AM
fs api

Err, to elaborate, you'd use the fs API (Wiki Page: http://computercraft.info/wiki/Fs_%28API%29)
( Did you really need to post 5 letters? )

Here is an example:

local x = 1
local y = 2
local z = 42

--# Saving the coords to a file

local fileHandle = fs.open("path","w") --# fileHandle is just a varible, make it what you want, "path" is the path of the file, "w" means Write mode
fileHandle.writeLine(x) --# Writes x to the file, and goes to the next line
fileHandle.writeLine(y)
fileHandle.writeLine(z)
fileHandle.close() --# ALWAYS close() when you are done reading/writing! It saves the file, and does a few other things

--# Reading the file

local file = fs.open("path","r") --# decided to use 'file' instead of 'fileHandle', "r" is Read mode
x = file.readLine() --# We know the first line is the x pos
y = file.readLine() --# And the second is the Y
z = file.readLine() --# and z
file.close() --# Remember to close!
Lupus590 #6
Posted 08 April 2015 - 10:11 AM
fs api

Err, to elaborate, you'd use the fs API (Wiki Page: http://computercraft...ki/Fs_%28API%29)

posted this late last night, was ment to add or to it later, opps
Cing #7
Posted 08 April 2015 - 01:53 PM
thanks a lot HPwebcamable :D/>
and i get it lupus590 ;)/>

but can you also choose a line when writing and reading?
Lupus590 #8
Posted 08 April 2015 - 02:04 PM
if you want to read the 3rd line only, you will have to read and discard the 1st and 2nd
writing is a bit more complex, if you have a file with 5 lines, and wanted to edit the 3rd, you will have to read the whole file, then write the first 2 lines, your modified 3rd line then the last 2 lines
Edited on 08 April 2015 - 12:05 PM
Cing #9
Posted 08 April 2015 - 03:01 PM
oke get it thanks
Dragon53535 #10
Posted 08 April 2015 - 04:32 PM
Just gonna leave this tutorial link here:Basic File IO
Cing #11
Posted 08 April 2015 - 05:22 PM
now i got another question
how can i check if the lines exists with fs.exists
and how can i delete all the lines at the and of my program
Dragon53535 #12
Posted 08 April 2015 - 05:26 PM
You're going to have to explain more. Are you meaning can you check how many lines are in a file? or if the file "lines" exists?

and for the second question, you're going to have to elaborate on that.
Cing #13
Posted 08 April 2015 - 05:35 PM
i want a the beginning of the program check if there is informatie stored and if there is then it should go futher were it left
and at the end of the program i want to delete the information so it doesn't go back down again and dig


ps: sorry for my bad english
Cing #14
Posted 08 April 2015 - 06:23 PM
You're going to have to explain more. Are you meaning can you check how many lines are in a file? or if the file "lines" exists?

i want to check if the lines exists
Edited on 08 April 2015 - 04:23 PM
Bomb Bloke #15
Posted 08 April 2015 - 10:43 PM
Do you want to check if a certain line exists within a certain file, or do you want to check if the whole file exists?

Do you want to delete some lines within a file, or do you want to delete the whole file?
Cing #16
Posted 09 April 2015 - 04:13 PM
i wanted to see if the hole file exists.

But i already got it sorted out :D/>
i just used "fs.exists("path")
and "fs.delete("path") in my program.