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
➜ 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 (23,165 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.
13,439 views.
It is now over 60 days since the last post. This thread is closed.
Refresh page
top