about global variables and tables

Posted by Graceecat on Sun 13 May 2007 08:10 AM — 5 posts, 23,870 views.

Taiwan #0
My global variables and tables vanished after restarting the mushclient. How do you save them? Question 2: how do you show them all? tks!


Australia Forum Administrator #1
Read:

http://www.gammon.com.au/forum/?id=6030

Scroll down to the part about "serializing variables".
Taiwan #2
According to question 2, I wrote my codes as follows:

require "serialize"
print (serialize.save ("_G"))


However it had a Run-time error.
World: aaa
Immediate execution
C:\mushclient407\MUSHclient\lua\serialize.lua:176: Cannot serialize '_G.progress.new' (function)
stack traceback:
[C]: in function 'error'
C:\mushclient407\MUSHclient\lua\serialize.lua:176: in function 'save_item'
C:\mushclient407\MUSHclient\lua\serialize.lua:169: in function 'save_item'
C:\mushclient407\MUSHclient\lua\serialize.lua:169: in function 'save_item'
C:\mushclient407\MUSHclient\lua\serialize.lua:61: in function 'save'
[string "Alias: "]:3: in main chunk


USA #3
You have to serialize the variables, not the whole script. Just go through whatever variables are needed between sessions, serialize those, and store them in MUSHclient variables with SetVariable(). Also keep in mind that there is a _G within _G, so referencing that as a whole can result in an infinite loop depending on what you're doing.

A good way to just store all the variables would be to go through for i,v in pairs(_G) and store anything where v is not a function, and i is not _G.
Australia Forum Administrator #4
The link I gave you had these sentences in it:


However, serializing (saving) the _G table is not really practical, as it contains things like functions, which cannot be serialized.

Thus, I recommend you place all variables needed for scripts into their own table.


Did you not read that bit?

All you have to do is store your variables in their own table, and serialize that.