Register forum user name Search FAQ

Gammon Forum

Notice: Any messages purporting to come from this site telling you that your password has expired, or that you need to verify your details, confirm your email, resolve issues, making threats, or asking for money, are spam. We do not email users with any such messages. If you have lost your password you can obtain a new one by using the password reset link.

Due to spam on this forum, all posts now need moderator approval.

 Entire forum ➜ MUSHclient ➜ Lua ➜ Help serialiazing a table

Help serialiazing a table

It is now over 60 days since the last post. This thread is closed.     Refresh page


Posted by Shadoweave   (42 posts)  Bio
Date Sun 04 Nov 2007 12:01 PM (UTC)
Message
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.
Top

Posted by David Haley   USA  (3,881 posts)  Bio
Date Reply #1 on Sun 04 Nov 2007 12:05 PM (UTC)
Message
Do you have reason to believe that the 'enchantments' table does, in fact, exist? (i.e. is not nil)

David Haley aka Ksilyan
Head Programmer,
Legends of the Darkstone

http://david.the-haleys.org
Top

Posted by Shadoweave   (42 posts)  Bio
Date Reply #2 on Sun 04 Nov 2007 02:01 PM (UTC)
Message
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.
Top

Posted by David Haley   USA  (3,881 posts)  Bio
Date Reply #3 on Sun 04 Nov 2007 03:22 PM (UTC)
Message
What if you do:
SetVariable ("enchantments", serialize ('enchantments'))

from the immediate window? Does that set the variable correctly?

David Haley aka Ksilyan
Head Programmer,
Legends of the Darkstone

http://david.the-haleys.org
Top

Posted by Shadoweave   (42 posts)  Bio
Date Reply #4 on Sun 04 Nov 2007 05:49 PM (UTC)
Message
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
Top

Posted by Nick Gammon   Australia  (23,166 posts)  Bio   Forum Administrator
Date Reply #5 on Sun 04 Nov 2007 07:53 PM (UTC)

Amended on Sun 04 Nov 2007 07:55 PM (UTC) by Nick Gammon

Message
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.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by Nick Gammon   Australia  (23,166 posts)  Bio   Forum Administrator
Date Reply #6 on Sun 04 Nov 2007 08:01 PM (UTC)
Message
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"))) ()

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by Nick Gammon   Australia  (23,166 posts)  Bio   Forum Administrator
Date Reply #7 on Sun 04 Nov 2007 08:37 PM (UTC)
Message
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.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).

To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.


28,185 views.

It is now over 60 days since the last post. This thread is closed.     Refresh page

Go to topic:           Search the forum


[Go to top] top

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.