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

Gammon Forum

See www.mushclient.com/spam for dealing with forum spam. Please read the MUSHclient FAQ!

[Folder]  Entire forum
-> [Folder]  MUSHclient
. -> [Folder]  Lua
. . -> [Subject]  Trouble Serialiazing Lua Tables
Home  |  Users  |  Search  |  FAQ
Username:
Register forum user name
Password:
Forgotten password?

Trouble Serialiazing Lua Tables

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


Posted by Zim   (22 posts)  [Biography] bio
Date Fri 29 Jan 2016 01:05 AM (UTC)
Message
I've read various documentation on the forms about serializing tables. I'm still having issues implementing it into my code. I wrote this plugin to help demonstrate what I'm trying to do in the simplest way possible. In this plugin: I have one alias that allows you to insert strings into a table and another one to print the table. What I would like to do is to save the table over reinstalls of the plugin. I understand that serializing is the key to making this happen but I must be missing something. Here is what I tried:

<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE muclient>
<!-- Saved on Thursday, January 28, 2016, 7:40 PM -->
<!-- MuClient version 5.00 -->

<!-- Plugin "test" generated by Plugin Wizard -->

<muclient>
<plugin
   name="test"
   author="Zim"
   id="6741be368ebea9880ba5faac"
   language="Lua"
   purpose="testing stuff"
   date_written="2016-01-28 19:38:51"
   requires="5.00"
   version="1.0"
   >

</plugin>
<aliases>
    <alias
   match="^test = (.*)$"
   enabled="y"
   regexp="y"
   send_to="12"
   sequence="100"
  >
  <send>
	table.insert(test, "%1")
  </send>
  </alias>
    <alias
   match="show test$"
   enabled="y"
   regexp="y"
   send_to="12"
   sequence="100"
  >
  <send>
	for i, v in ipairs(test) do
        print(i, v)
    end
  </send>
  </alias>
</aliases>


<!--  Get our standard constants -->

<include name="constants.lua"/>

<!--  Script  -->


<script>
<![CDATA[
require "serialize"  -- needed to serialize table to string
test = {}  -- ensure table exists, if not loaded from variable

-- on plugin install, convert variable into Lua table

function OnPluginInstall ()
  assert (loadstring (GetVariable ("test") or "")) ()
end -- function OnPluginInstall

-- on saving state, convert Lua table back into string variable

-- save_simple is for simple tables that do not have cycles (self-reference)
-- or refer to other tables

function OnPluginSaveState ()
  SetVariable ("test", 
               "test = " .. serialize.save_simple (test))
end -- function OnPluginSaveState
]]>
</script>


</muclient>
[Go to top] top

Posted by Nick Gammon   Australia  (21,322 posts)  [Biography] bio   Forum Administrator
Date Reply #1 on Fri 29 Jan 2016 03:55 AM (UTC)
Message
Add this to the <plugin> part:


 save_state="y"


Otherwise the serialized variable is not saved and restored.

i.e.


<plugin
   name="test"
   author="Zim"
   id="6741be368ebea9880ba5faac"
   language="Lua"
   purpose="testing stuff"
   save_state="y"
   date_written="2016-01-28 19:38:51"
   requires="5.00"
   version="1.0"
   >

- Nick Gammon

www.gammon.com.au, www.mushclient.com
[Go to top] top

Posted by Zim   (22 posts)  [Biography] bio
Date Reply #2 on Fri 29 Jan 2016 05:54 AM (UTC)
Message
Well.. that was simple, thanks!
[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.


1,470 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 FutureQuest]