Posted 19 May 2013 - 10:00 PM
So I have a quarry program, and I am working on persistence. I have most of working, but I can't get the values of variables into the file. The current way I have it set up is I have an array of all the different variables I want to transfer (as strings), and then I'm using a loadstring to return the value of the "string" which is the name of a variable (all variables are local to the program). Here is about how it is set up now.
Also, just to break down the complicated part involving loadstring, it setting the environment of the function returned by loadstring to the program's local global environment (so it can see variables theoretically). Then it calls that function to return the value of the variable in the program, and tostrings it.
What am I doing wrong?
--Way at the top
local x , y, z = 0,0,0
--Assignment of vars
--About halfway down code
local restoreVars = {"x","y","z","pretendThereAreMoreThings"}
function saveProgress()
file = fs.open("fileName", "w")
for i=1, #restoreVars do
file.write(restoreVars[i].." = "..tostring(setfenv(loadstring("return "..restoreVars[i]),getfenv())()).."\n")
end
file.close()
end
--Later
local function mine()
--Do mining stuff
saveProgress()
end
Now, the writing part works, but the part with loadstring does not. The output file gets x = nil
y = nil
z = nil
everythingElse = nil
Also, just to break down the complicated part involving loadstring, it setting the environment of the function returned by loadstring to the program's local global environment (so it can see variables theoretically). Then it calls that function to return the value of the variable in the program, and tostrings it.
What am I doing wrong?