Help serialiazing a table

Posted by Shadoweave on Sun 04 Nov 2007 12:01 PM — 8 posts, 31,227 views.

#0
The trigger:

</trigger>
  <trigger
   enabled="y"
   match="^Password correct\. Welcome to Lusternia\.$"
   name="Login"
   regexp="y"
   script="login"
   send_to="12"
   sequence="100"
  >

And the script:

function login ()
reset_vars ()
reset_tables ()
get_score ()
loadstring (GetVariable ("enchantments"))
SetChanged (true)
end -- funtion

function OnWorldSave ()
require "serialize"
SetVariable ("enchantments", serialize ('enchantments'))
end -- function

Why isn't it working? When I try to print the table "enchantments" it says that it is a nil value.
USA #1
Do you have reason to believe that the 'enchantments' table does, in fact, exist? (i.e. is not nil)
#2
I create the table from the immediate window, print it, check to see if SetChanged is true, then I close the world. I login again and if I try to print it it doesn't work.
USA #3
What if you do:
SetVariable ("enchantments", serialize ('enchantments'))

from the immediate window? Does that set the variable correctly?
#4
What I wrote in the immediate window:

enchantments ={}
enchantments.x = "123"
require "serialize"
SetVariable ("enchantments", serialize ('enchantments'))

The error:

Run-time error
World: Lusternia
Immediate execution
[string "Immediate"]:4: attempt to call global 'serialize' (a table value)
stack traceback:
        [string "Immediate"]:4: in main chunk
Australia Forum Administrator #5
If you look at the examples on this page:

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

You see that serialize is a table, and you need to call functions inside that table. One way is:


enchantments ={}
enchantments.x = "123"
enchantments.y = "345"

require "serialize"
SetVariable ("enchantments", serialize.save ('enchantments'))

enchantments variable is now:

enchantments = {}
  enchantments.y = "345"
  enchantments.x = "123"


Or if your tables are fairly simple and do not refer to themselves you can use save_simple, like this:


enchantments ={}
enchantments.x = "123"
enchantments.y = "345"

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


enchantments variable is now:

enchantments = {
  y = "345",
  x = "123",
  }


Note the slightly different usage in the second example, as it is given the table itself, not the name of the table, you have to put "enchantments = " as part of the variable to be saved.

The "save_simple" function gives a slightly more readable result, as it looks like a Lua table.
Amended on Sun 04 Nov 2007 07:55 PM by Nick Gammon
Australia Forum Administrator #6
Also your loadstring needs work. The function loadstring returns a function which needs to be called, that is instead of:


loadstring (GetVariable ("enchantments"))


You need:


loadstring (GetVariable ("enchantments")) ()


The extra pair of brackets at the end makes the result of parsing the text actually get executed and enter your script space.

You might also check for syntax errors in your enchantments variable (in case you manually edit it), by doing an assert like this:


assert (loadstring (GetVariable ("enchantments"))) ()
Australia Forum Administrator #7
Quote:

function OnWorldSave ()
require "serialize"
SetVariable ("enchantments", serialize ('enchantments'))
end -- function


I am not sure this will ever be called anyway. The way you have done this, the script function OnWorldSave is only created when the trigger fires, however MUSHclient checks for things like a "world save" function when the script file is processed. You really need a script file, and to put the OnWorldSave inside that, rather than nested inside a trigger.