serialize issues

Posted by Ogomez92 on Mon 08 Nov 2010 01:48 PM — 5 posts, 21,873 views.

#0
Hi,
I'm trying to work out how to serialize a lua table.
This table consists of booleans which are string named, like this.
gag.spells=true
gag.combat=true
These are options for spam filters which i want to save.
What I did was this:
On the PluginSaveState function I serialized the gag table like this
require "serialize"
local gags = serialize.save_simple(gag)
Then I put it into a variable
SetVariable("gag",gags)
So far so good, but when i try to load the table using OnPluginInstall,
gag={}
--local gags = GetVariable("gag")
--gag= assert(loadstring(gags))()
I get the following error:
Plugin: AlterAeon (called from world: alter)
Function/Sub: OnPluginInstall called by Plugin AlterAeon
Reason: Executing plugin AlterAeon sub OnPluginInstall
[string "Plugin"]:601: [string "{..."]:1: unexpected symbol near '{'
stack traceback:
[C]: in function 'assert'
[string "Plugin"]:601: in function 'clear'

I'm using the latest version of mush 4.66.
Thoughts?
Thanks!
#1
Try:

gag = assert(loadstring("return " .. gags or ""))()


The table will be loaded into the gag variable.

Bast
#2
Hey, thanks. that worked. But i don't understand the procedure..
How does adding return and or make it a table?..
Thanks.
USA #3
Basically, serialize_simple takes a table and returns a string that looks like that table. Basically, "source code" for the table that can be executed later to get the table back. This source code looks exactly like this:

{
  ["foo"] = 1,
  ["bar"] = "something",
  42 = "and so on",
}


This basically is your regular table constructor, something you may have used before. When Lua executes it, it creates a new table. The "return" in loadstring() is necessary because loadstring() takes the string you give it and puts it directly into a function block. Without the "return", it's almost as though you typed this:

function()
  {
    ["foo"] = 1,
    ["bar"] = "something",
    42 = "and so on",
  }
end


And this function is returned by loadstring. However, there's no "return" there, is there? In fact, this is invalid code, and loadstring() will wail like a banshee if you try to do that. That's why you need the return: to get the table out from the function loadstring() creates.
Australia Forum Administrator #4
I normally do it a slightly different way. To save (in OnPluginSaveState) I do:


require "serialize"
SetVariable ("gag", "gag = " .. serialize.save_simple (gag))


The SetVariable line sets the variable with an assignment statement in front, so when the loadstring is executed, the data is put back into the "gag" global variable.

Then later on (in OnPluginInstall) I do this:


gag = {}  -- in case first time, make empty table
assert (loadstring (GetVariable ("gag") or "")) ()


That grabs the MUSHclient "gag" variable (which contains the assignment to the Lua "gag" variable) and loads it. If the variable is empty (first time you run the plugin) then loadstring just executes an empty statement, which causes no error.