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
➜ serialize issues
It is now over 60 days since the last post. This thread is closed.
Refresh page
| Posted by
| Ogomez92
(42 posts) Bio
|
| Date
| Mon 08 Nov 2010 01:48 PM (UTC) |
| Message
| 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! | | Top |
|
| Posted by
| Bast
(78 posts) Bio
|
| Date
| Reply #1 on Mon 08 Nov 2010 03:15 PM (UTC) |
| Message
| Try:
gag = assert(loadstring("return " .. gags or ""))()
The table will be loaded into the gag variable.
Bast |
Bast
Scripts: http://github.com/endavis | | Top |
|
| Posted by
| Ogomez92
(42 posts) Bio
|
| Date
| Reply #2 on Mon 08 Nov 2010 04:30 PM (UTC) |
| Message
| Hey, thanks. that worked. But i don't understand the procedure..
How does adding return and or make it a table?..
Thanks. | | Top |
|
| Posted by
| Twisol
USA (2,257 posts) Bio
|
| Date
| Reply #3 on Mon 08 Nov 2010 05:29 PM (UTC) |
| Message
| 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. |
'Soludra' on Achaea
Blog: http://jonathan.com/
GitHub: http://github.com/Twisol | | Top |
|
| Posted by
| Nick Gammon
Australia (23,165 posts) Bio
Forum Administrator |
| Date
| Reply #4 on Mon 08 Nov 2010 07:28 PM (UTC) Amended on Mon 08 Nov 2010 08:25 PM (UTC) by Nick Gammon
|
| Message
| 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. |
- 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.
19,252 views.
It is now over 60 days since the last post. This thread is closed.
Refresh page
top