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

Saving a changing variable to a file

Started by glitchies, 30 January 2014 - 01:53 PM
glitchies #1
Posted 30 January 2014 - 02:53 PM
Hello, today i am trying to learn how to read and write info to a text file from my turtle, so i decided to start with something simple, a chunk loading turtle that does a spiral pattern. for the most part i understand the code, but i am having problems writing a changing variable to the file.


peripheral.wrap("right")
local tArgs = {...}
local file = fs.open("save", "r")
local y = file.readAll()
file.close()
if tArgs[1] == nil then tArgs[1] = 1 end
function legs()
   for i = 1, y do
	  turtle.forward()
   end
   turtle.turnLeft()
   y = y + 32
end
function whole()
   for i = 1, 4 do
	  legs()
   end
   file = fs.open("save" , "r")
   file.write(y)
   file.close()
end
for i = 1, tArgs[1] do
   whole()
end

I start out with the turtle grabbing value for "y" from a file, which in this case i had set up earlier with the value 32, then it does it's thing with it, moves, adds 32 to it a few times, then it writes it to the file, so that the next time i run the program, it picks up from where it left off on the size of the spiral. Where my problem is, is I want to write the value of "y" to the file, but obviously that gives an error when i just use file.write(y). What is the proper syntax for this?
Lyqyd #2
Posted 30 January 2014 - 06:08 PM
You're opening the file handle in read mode when you are trying to write to it. Try using write mode to open the file handle instead. Replace the "r" in the second fs.open call with "w".
glitchies #3
Posted 31 January 2014 - 04:09 PM
right, did that now. but still getting an error on line 19.

spiral:19: attempt to index ? ( a nill value)

line 19 is

   file.write(y)
Lyqyd #4
Posted 31 January 2014 - 04:53 PM
Please post the whole updated code.
glitchies #5
Posted 01 February 2014 - 09:23 AM

peripheral.wrap("right")
peripheral.wrap("left")
local tArgs = {...}
local file = fs.open("save", "r")
local y = file.readAll()
file.close()

if tArgs[1] == nil then tArgs[1] = 1 end
function legs()
   for i = 1, y do
		  turtle.forward()
   end
   turtle.turnLeft()
   y = y + 32
end

function whole()
   for i = 1, 4 do
		  legs()
   end
   file = fs.open("save" , "w")
   file.write(y)
   file.close()
end
for i = 1, tArgs[1] do
   whole()
end
Bomb Bloke #6
Posted 01 February 2014 - 06:59 PM
That's odd, line 19 reads "legs()". There's no indexing going on there - just a function call.

If you've modified the script again since posting the last error, please post the current error, along with the current contents of your "save" file.
awsmazinggenius #7
Posted 01 February 2014 - 07:04 PM
By the way, in order to use peripheral.wrap() you have to collect it's return value. Example for a monitor:

local monitor
for _, v in pairs(rs.getSides()) do
  if peripheral.getType(v) == "monitor" and peripheral.call(v, "isColor") then
    monitor = peripheral.wrap(v)
  end
end

monitor.write "Hello, World!" 
glitchies #8
Posted 03 February 2014 - 12:28 AM
sorry, i forgot that i added the second line in there cause i was testing a wireless modem with some gps stuff and forgot to remove that line.

the line that is giving errors is

file.write(y)

as to the peripheral.wrap() on line 1, it is a chunkloader module that seams to work just fine just by calling it.

edit: omg, i'm an idiot… i just found my problem. i created the save file outside of minecraft, and uploaded it, and forgot to change the permissions on it be writable. it was error-ring out cause it couldn't write the variable.
Edited on 03 February 2014 - 12:00 AM