World/Plugin communication(and other assorted questions)

Posted by Ircria on Tue 28 Sep 2010 05:34 AM — 5 posts, 23,260 views.

#0
Hello.

First of all, I'm sorry if this is in the wrong section.

Now, I've come across a few problems over the last couple days. Most of them, as the title may suggest, deal with communication between the plugins and the world, vice versa, and plugins with other plugins.

Do scripts(functions specifically) have the ability to be global so the content inside of them may be used by the plugins if in the world or another plugin, or vice versa when referring to the world?

Is it possible to get the client-stored variables(the ones returned by GetVariable) in plugins from the world, vice versa, and between different plugins?

Is there any way for plugins to advertise their existence to other plugins and send data to them?

For a couple non-plugin related questions...

Is it possible to save lua variables/tables in a format where they could be executed with dofile()?

Does anybody have anybody else have issues getting anything done through loadstring() to execute?


Thank you all for any help, and I'm sorry if this was a bit wordy.
USA #1
Ircria said:
Do scripts(functions specifically) have the ability to be global so the content inside of them may be used by the plugins if in the world or another plugin, or vice versa when referring to the world?

You can use CallPlugin() to call a global function in another script's space. Since you can also call API functions it has access to, like SetVariable, this can be pretty useful in a lot of ways.

Ircria said:
Is it possible to get the client-stored variables(the ones returned by GetVariable) in plugins from the world, vice versa, and between different plugins?

Sadly, no. CallPlugin can't access the world space. You can hack around it by using aliases and Execute(), but it's not foolproof.

Ircria said:
Is there any way for plugins to advertise their existence to other plugins and send data to them?

I wrote a PPI module that makes this very easy. Nick created his own version which comes bundled with MUSHclient; I don't have any experience working with that version, but give it a try first. If it doesn't meet your expectations, mine might.

Ircria said:
Is it possible to save lua variables/tables in a format where they could be executed with dofile()?

Serialize to a string using serialize.save_simple in the serialize.lua module, then... uh, save to a file with a "return " prepended to the content. Then you can dofile() it and get the result back. But I'd rather save to a string and put it in a MUSHclient variable, which MUSHclient can save automatically if you have save_state="y" in the <plugin> tag, and then read it back in with loadstring("return " .. GetVariable("stuff"))().

Ircria said:
Does anybody have anybody else have issues getting anything done through loadstring() to execute?

Example? Make sure you call the result of loadstring() though; it returns a function containing the code you passed to it, instead of executing it right away.

Ircria said:
Thank you all for any help

Not a problem!
Australia Forum Administrator #2
Ircria said:

Is it possible to get the client-stored variables(the ones returned by GetVariable) in plugins from the world, vice versa, and between different plugins?


I think Twisol misunderstood your question.

Yes, any client variables (the GetVariable ones) are readily accessible to all plugins.

Template:function=GetPluginVariable
GetPluginVariable

The documentation for the GetPluginVariable script function is available online. It is also in the MUSHclient help file.



You can read any plugin's variable by knowing its plugin ID, and the main script space variables by using a plugin ID of an empty string.

Ircria said:

Is there any way for plugins to advertise their existence to other plugins and send data to them?


You can advertise your existence with BroadcastPlugin. You can also send data to them the same way. Also using CallPlugin you can send data to a particular plugin.

Template:function=BroadcastPlugin
BroadcastPlugin

The documentation for the BroadcastPlugin script function is available online. It is also in the MUSHclient help file.



Template:function=CallPlugin
CallPlugin

The documentation for the CallPlugin script function is available online. It is also in the MUSHclient help file.



Ircria said:

Is it possible to save lua variables/tables in a format where they could be executed with dofile()?

Does anybody have anybody else have issues getting anything done through loadstring() to execute?


As Twisol said, serializing is a popular and useful way of doing it. This is described here:

Template:post=4960
Please see the forum thread: http://gammon.com.au/forum/?id=4960.


There should be no issues with loadstring, providing you remember to execute the returned function. If nothing happens you need to put () at the end of the returned value.
USA #3
Hmm, yeah, I think I did misunderstand your question. Sorry!
Australia Forum Administrator #4
For advertising, a useful callback function is OnPluginListChanged. This is documented here:

http://www.gammon.com.au/scripts/doc.php?general=plugin_callbacks

It's useful because you get called when the list of plugins changes (for example, one is added). Thus that is a useful time to advertise your existence.