Gammon Forum
Entire forum
MUSHclient
Lua
Problem with loadstring and serializing tables
Problem with loadstring and serializing tables
|
It is now over 60 days since the last post. This thread is closed.
  Refresh page
Posted by
| Mentallaxative
(3 posts) bio
|
Date
| Sun 23 Dec 2007 03:25 PM (UTC) |
Message
| Hi there,
I'm trying to make a Lua plugin that counts the number of different animals I hunt on the mud I play. There are well over 100 of them so I thought I would try placing them all into a table.
I've run into a problem with loading the serialized table back during OnPluginInstall.
I get the error:
Description: [string "Plugin"]:147: [string "{..."]:1: unexpected symbol near '{'
stack traceback:
[C]: in function 'assert'
[string "Plugin"]:147: in function <[string "Plugin"]:145>
Called by: Function/Sub: OnPluginInstall called by Plugin Hunt_Counts
Reason: Executing plugin Hunt_Counts sub OnPluginInstall
The two functions in question are:
function OnPluginInstall ()
hunt={}
assert (loadstring (GetVariable ("hunt") or "")) ()
end -- OnPluginInstall
function OnPluginSaveState ()
require "serialize"
SetVariable ("hunt", serialize.save_simple (hunt))
end -- OnPluginSaveState
The { in [string "{..."] is referring to the serialized variable, which is something like
{
["Yellow Alligator"] = 1,
}
which I found out using GetPluginVariable. | top |
|
Posted by
| Nick Gammon
Australia (22,973 posts) bio
Forum Administrator |
Date
| Reply #1 on Sun 23 Dec 2007 07:23 PM (UTC) |
Message
| The serialize.save_simple does not save a variable name, as it doesn't know it (you are passing a table address, not a variable name).
Thus you need to put it back at one end or the other. One way would be:
SetVariable ("hunt", "hunt = " .. serialize.save_simple (hunt))
Or, when you load it back:
assert (loadstring ("hunt = " .. GetVariable ("hunt") or " {}")) ()
(But don't do both). |
- 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.
10,506 views.
It is now over 60 days since the last post. This thread is closed.
  Refresh page
top
Quick links:
MUSHclient.
MUSHclient help.
Forum shortcuts.
Posting templates.
Lua modules.
Lua documentation.
Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.