[Home] [Downloads] [Search] [Help/forum]


Register forum user name Search FAQ

Gammon Forum

[Folder]  Entire forum
-> [Folder]  MUSHclient
. -> [Folder]  Lua
. . -> [Subject]  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] Refresh page


Posted by Mentallaxative   (3 posts)  [Biography] 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.
[Go to top] top

Posted by Nick Gammon   Australia  (22,973 posts)  [Biography] 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
[Go to top] top

Posted by Mentallaxative   (3 posts)  [Biography] bio
Date Reply #2 on Mon 24 Dec 2007 09:46 AM (UTC)
Message
Thanks, it's working now! :D
[Go to top] 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] Refresh page

Go to topic:           Search the forum


[Go to top] 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.

[Home]


Written by Nick Gammon - 5K   profile for Nick Gammon on Stack Exchange, a network of free, community-driven Q&A sites   Marriage equality

Comments to: Gammon Software support
[RH click to get RSS URL] Forum RSS feed ( https://gammon.com.au/rss/forum.xml )

[Best viewed with any browser - 2K]    [Hosted at HostDash]