Trouble Serialiazing Lua Tables

Posted by Zim on Fri 29 Jan 2016 01:05 AM — 3 posts, 13,455 views.

#0
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>
Australia Forum Administrator #1
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"
   >
#2
Well.. that was simple, thanks!