Posted 27 December 2012 - 12:59 PM
Hey guys, I've been annoyed lately that computers doesn't store the state when they unload.
I tried to resolve this with a simple library that makes it easy to save all global variables and then load them later.
[media]http://youtu.be/odc0b_fIUW4[/media]
The code that is contained within the script "loadtest" is:
The StateManager lib itself looks like this:
https://gist.github.com/4388010
I tried to resolve this with a simple library that makes it easy to save all global variables and then load them later.
[media]http://youtu.be/odc0b_fIUW4[/media]
The code that is contained within the script "loadtest" is:
stateManager = (loadfile "stateManager")()
function defVar()
testVar = 10
testTable = {
tableVar = 20,
nestedTable = {
nestedVar = 20,
stringVar = "Hello There",
},
}
end
function save()
stateManager:saveState("state", getfenv(1))
end
function load()
stateManager:loadState("state", getfenv(1))
end
function dumpTable(table)
for k,v in pairs(table) do
if type(v) == "table" then
dumpTable(v)
end
if type(v) == "number" or type(v) == "string" then
print(string.format("%s : %s", k, v))
end
end
end
The important parts are the functions save and load, these are the two that interfaces with statemanager. "getfenv(1)" just gets the table for this environment (the "global" table). Since this is a table you could pass any other table in to save the state of that.The StateManager lib itself looks like this:
https://gist.github.com/4388010