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

Storing multiple variables

Started by Groomtar, 28 October 2012 - 04:01 PM
Groomtar #1
Posted 28 October 2012 - 05:01 PM
Hey there,

I am working on a list of programs working together.

So far I got my "manager" program, my "quarry" and my "input".

The Manager keeps track of what places need to be dug and when a turtle asks for a place to dig, it assigns it.

The Quarry tells the turtle how to handel the information given by the Manager, basically telling it how to dig.

The Input allows me to add new areas to be dug to my manager, so I dont have to shut it down to keep it running.

They are all running smooth and without flaws.


I just got the idea to store my quarry areas in a Saved Variable so my manager will remember them even if he gets shutdown.

I managed to get the file to store the first location, but as soon as I add additional locations, it fails (no lua error, the numbers are just not added to my .txt file).


Some Variables:

local s = {}


local dataJobList = "disk/managerJobList.txt"
local fileSave = fs.open(dataJobList, "w")
local fileLoad = fs.open(dataJobList, "r")


local jobTypeText = {"Quarry","Tunnel"}
local jobTypeInfo = {6,0}


local jobInfo0 = {} -- JobType
local jobInfo1 = {} -- xCoord Start
local jobInfo2 = {} -- zCoord Start
local jobInfo3 = {} -- yCoord Start
local jobInfo4 = {} -- Extra info 1
local jobInfo5 = {} -- Extra info 2
local jobInfo6 = {} -- Extra info 3

This is my part of the code that runs the save

-- Save
function save()
  -- Wipes old s variables
  for i = 1, 9 do
	s[i] = ""
  end
  -- Convert first data to string
  s[1] = tostring(jobNumberTotal)
  s[2] = tostring(jobNumberCurrent)

  -- Convert our table values into strings
  for i = 1, jobNumberTotal do
	s[3] = s[3]..tostring(jobInfo0[i]).." "
	s[4] = s[4]..tostring(jobInfo1[i]).." "
	s[5] = s[5]..tostring(jobInfo2[i]).." "
	s[6] = s[6]..tostring(jobInfo3[i]).." "
	s[7] = s[7]..tostring(jobInfo4[i]).." "
	s[8] = s[8]..tostring(jobInfo5[i]).." "
	s[9] = s[9]..tostring(jobInfo6[i]).." "
  end

  -- Making sure we end up with one long string
  for i = 3, 9 do
	s[i] = tostring(s[i])
  end

  -- Write the strings to the save
  for i = 1, 9 do
	fileSave.write(s[i].."n")
  end

  -- Close the File
  fileSave.close()
end
jag #2
Posted 28 October 2012 - 06:02 PM
Take a look at textutils.serialize() and textutils.unserialize()!
If you store all your variables:
var1 = "one"
var2 = "TWO!!"
var3 = 3
file.write(textitils.serialize({var1,var2,var3}))

And when you want to retreive the variables:
var1,var2,var3 = textutils.unserialize(file.readLine())
XoX #3
Posted 28 October 2012 - 06:04 PM
Once you write the numbers once you close the filehandle. So once you saved once your filehandle is closed and can't be used again.

Open it in the beginning of the save function instead of the beginning of the script, this way it will be opened every time it's needed.

Also you might want use f.writeLine(string)
This way it is easier to read because with readLine you will always read exactly 1 set of saved data.

Another thing, if you use the "w" (write) mode, if you save again you overwrite your old saved data. If you want to save multiple ones you either have to save them all in the same instance of the file being opened, or you have to use "a" (append) mode, which, instead of overwritting it will add onto the end of the file.
Groomtar #4
Posted 28 October 2012 - 07:12 PM
My idea is to overwrite. My code keeps refreshing the first jobs in the list anyways, so overwrite is needed :D/>/>
But I might look into how I open / close it, because that is probably my problem right now.
So what you are saying is that I should open it, and then just leave it open, since I will keep using it all the time to keep storring information ?
XoX #5
Posted 28 October 2012 - 08:27 PM
Look, you can only use the file handle while it open. What you did was first open it when your script starts, then when you call save() you wrote into the file and the you closed the filehandle, then when you use save() a second time, it can't do .write() or anything because it is working with a closed filehandle. I would suggest you simple move the local fileSave = fs.open(dataJobList,"w") to the beginning of the save() function.
But you can't simply leave it open, you have to .close() it to actually save the data to the file. Just open it again when you need it.
Groomtar #6
Posted 28 October 2012 - 09:52 PM
Thanks for pointing that out XoX, I totally oversaw that mistake, but it is very clear to me now :D/>/>
Groomtar #7
Posted 28 October 2012 - 10:04 PM
I just got time to work on it again, and it works like a charm now, cheers to everyone for the help !
XoX #8
Posted 28 October 2012 - 10:51 PM
No problem man, glad to help.