Register forum user name Search FAQ

Gammon Forum

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.
 Entire forum ➜ MUSHclient ➜ Lua ➜ Trouble Serialiazing Lua Tables

Trouble Serialiazing Lua Tables

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


Posted by Zim   (22 posts)  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>
Top

Posted by Nick Gammon   Australia  (23,121 posts)  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
Top

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


9,902 views.

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

Go to topic:           Search the forum


[Go to top] top

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.