Plugin callback functions

Posted by Madcatz on Wed 12 May 2010 03:32 PM — 5 posts, 17,386 views.

#0
Do any of the OnPlugin* functions get called when the world is first opened? I have some tables that I'm serializing on OnPluginSaveState(), so that they're saved when the world is closed, but I'm not sure where I should be unserializing them. Most of the examples I see use OnPluginInstall(), but that doesn't seem to run if I just close the world and open it back up. I _could_ use OnPluginConnect(), but it's not uncommon for a user to disconnect and connect without the plugin saving state, in which case the tables would get overwritten -- though I suppose I could serialize in OnPluginDisconnect()...

Anyways, if I want the plugin to serialize and unserialize its tables so that they're not lost when the world is closed, where should I do this?

My current code is as follows:


function OnPluginSaveState()
  Note("OnSaveState")
  require "serialize"
  SetVariable ("captures", "captures = " .. serialize.save_simple (captures))
  SetVariable ("capturesecho", "capturesecho = " .. serialize.save_simple (capturesecho))
  SetVariable ("capturesordernums", "capturesordernums = " .. serialize.save_simple (capturesordernums))
  SetVariable ("capturesordernames", "capturesordernames = " .. serialize.save_simple (capturesordernames))
end

function OnPluginInstall()
Note("OnInstall")
assert (loadstring (GetVariable ("captures") or "")) ()
assert (loadstring (GetVariable ("capturesecho") or "")) ()
assert (loadstring (GetVariable ("capturesordernums") or "")) ()
assert (loadstring (GetVariable ("capturesordernames") or "")) ()
end


And it's not unserializing when I want it to (when I open the world after it's been closed).

And just to clarify: I actually _do_ need to serialize in order to save the tables when I close MUSHclient, yes?
USA #1
I'm nearly positive it is in fact being run. The reason you're not seeing "OnInstall" is because the output buffer hasn't been fully initialized by the time the plugins are called. Instead of using a Note() there, try a DoAfterSpecial():

DoAfterSpecial(5, [[Note("OnInstall")]], 12)


It should Note after 5 seconds.
#2
Ah, you're right. I should be able to figure out what's going wrong now. Thanks :)
USA #3
It's easier to test this stuff by using "Reinstall" on the Plugins dialog with your plugin, for the record. No need to close and reopen the world, and the output buffer obviously is working.
Australia Forum Administrator #4
You will simplify this stuff in the future if you put your variables into a "holding table", that way you just have to every serialize one thing, rather than having to add a line in the save function, and a line in the load function, for every extra variable you use.

eg.


myvars = {}

myvars.captures = 1
myvars.capturesecho = true
myvars.capturesordernums = 42
myvars.capturesordernames  = "swordfish"



Now just serialize "myvars".