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

Saving variable data

Started by kotorone1, 23 July 2012 - 10:17 PM
kotorone1 #1
Posted 24 July 2012 - 12:17 AM
Hello,

i have been trying to right an OS, and i currently have a problem. I need a way to save data between reboots. I have tried using io.write() but i can't target the one line of code that I need to change. for example

function testFunc()
local tochange == "data to change here"
end

In this example how would I change the value of tochange, in a way that is stored between reboots?
Thanks,
Matt

edit:
Also, how would I detect if a variable has a nil value? I am trying to see if a computer already has a label, but can't think of a way to do so.
thanks again,
Matt
cant_delete_account #2
Posted 24 July 2012 - 01:57 AM
To check if a variable is nil, use:

if VARIABLEHERE == nil then
doStuff()
end
Or:

if not VARIABLEHERE then
doOtherStuff()
end
And to save variables through reboot, try this:

local function saveVar(tVar, tVarContent, tReplace)
if not tReplace then
if not fs.exists("/.vars/"..tVar..".var") then
local tFile = fs.open("/.vars/"..tVar..".var", "w")
if tFile then
tFile.write(tVarContent)
tFile.close()
end
else
print("Error: Variable "..tVar.." already exists!")
end
else
fs.delete("/.vars/"..tVar..".var")
local tFile = fs.open("/.vars/"..tVar..".var", "w")
if tFile then
tFile.write(tVarContent)
tFile.close()
end
end
end
local function readVar(tVar)
if fs.exists("/.vars/"..tVar..".var") then
local tFile = fs.open("/.vars/"..tVar..".var", "r")
else
print("Error: Variable "..tVar.." doesn't exist!")
end
if tFile then
return tFile.readLine()
tFile.close()
end
end
Then use:
saveVar("name you want variable to be", "what variable contains this only works with strings right now", if you want to replace variable boolean true/false) – saves variable
readVar("variable name") – returns contents of variable entered
Of course, if I was releasing this I would polish it up, but this is just an example.
(never tested ^)
kotorone1 #3
Posted 24 July 2012 - 04:15 AM
Thanks.That looks like it will work perfectly

And can i also use

if VARIABLEHERE == nil then
doStuff()
end
to tell if a functon in one of my APIs does or does not exist/have a value?
kotorone1 #4
Posted 24 July 2012 - 05:47 AM
I tested this out, and it works until you hit the readVar() function. Whenever you try to run that you have an "attempt to index ? (a nil value)" error. I debugged the thing, and the error appears to be in

if tFile then  
return tostring(tFile.readLine())
these lines of code.

edit: fixed it by using "return tFile.readLine()"
KaoS #5
Posted 24 July 2012 - 07:15 AM
why don't you just save ALL of the data you want to back up in a table, serialize it and then save it to a file? You can then load that table back from the file and everything will be there with the labels you used in creating the table
cant_delete_account #6
Posted 24 July 2012 - 07:27 PM
I tested this out, and it works until you hit the readVar() function. Whenever you try to run that you have an "attempt to index ? (a nil value)" error. I debugged the thing, and the error appears to be in

if tFile then  
return tostring(tFile.readLine())
these lines of code.

edit: fixed it by using "return tFile.readLine()"
Yeah, forgot about that stuff with tostring and readLine, edited the post. But again, it was just example code.