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

[REL]StateManager

Started by delusionallogic, 27 December 2012 - 11:59 AM
delusionallogic #1
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:

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
CoolisTheName007 #2
Posted 27 December 2012 - 01:12 PM
The mod developers are working towards full persistence of a computer state (e.g. remembers which programs are running, whic functions, and where to resume them), ect; thought you should know. Btw, I do hope we get some kind of warning that the chunk just resumed (when persistence comes).
It is still useful, don't get me wrong.
delusionallogic #3
Posted 28 December 2012 - 01:21 AM
The mod developers are working towards full persistence of a computer state (e.g. remembers which programs are running, whic functions, and where to resume them), ect; thought you should know. Btw, I do hope we get some kind of warning that the chunk just resumed (when persistence comes).
It is still useful, don't get me wrong.

No worries, i won't be offended if it's not useful. I don't follow these forums much,and i haven't checked if this has already been done. All i want to know if if this has already been done, but better,