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.

Due to spam on this forum, all posts now need moderator approval.

 Entire forum ➜ MUSHclient ➜ Plugins ➜ How to invoke a method in another Plugin and get the return value.

How to invoke a method in another Plugin and get the return value.

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


Posted by Wanghom8228   (19 posts)  Bio
Date Fri 14 May 2010 09:04 AM (UTC)
Message
How can I invoke a method in another Plugin and get the return value? I know there is a method called "CallPlugin", but seems it cannot get the result value. :(
Top

Posted by Twisol   USA  (2,257 posts)  Bio
Date Reply #1 on Fri 14 May 2010 09:23 AM (UTC)

Amended on Fri 14 May 2010 09:30 AM (UTC) by Twisol

Message
Try my PPI module [1]. It lets you call a plugin that also uses PPI. Example:

Service plugin:
PPI = require "ppi"

-- expose a value with the given name
PPI.Expose("CallMe", function(one, two)
  return one + two
end)


Client plugin:
PPI = require "ppi"

-- somewhere for the interface to be stored
otherplugin = nil

-- called when the plugin with this ID is loaded/reloaded
PPI.OnLoad("plugin ID here", function(interface)
  otherplugin = interface
end)

-- needed so PPI knows when the other plugins have loaded/reloaded
function OnPluginListChanged()
  PPI.Refresh()
end
-- this would also work: OnPluginListChanged = PPI.Refresh

-- Lets say the alias is test * *
function SomeAliasFunction(name, line, matches)
  local one, two = tonumber(matches[1]), tonumber(matches[2])
  Note(otherplugin.CallMe(one, two))
end


If, in that example, you used "test 8 16", you'd see 24 on the screen. The client plugin may look bigger, but there's really three parts, whereas the service plugin only has one (in this contrived example).

1. The client plugin tells PPI what plugins it wants to communicate with, and it stores an interface to the plugin when it's loaded. The callback passed to OnLoad makes for a good place to do initialization with the plugin too.

2. PPI needs to be called when the OnPluginListChanged fires. This is how it knows when a plugin has (re)loaded.

3. The part that actually uses the interface. In the example it's a simple alias callback. It takes the captures and passes it to the function in the other plugin, then prints the result.


PPI takes the arguments, serializes them, stuffs them in MUSHclient variables, and calls PPI_INVOKE on the other side with CallPlugin(). PPI_INVOKE deserializes the arguments, calls the function you wanted to call, then serializes the return value and ships it back in a similar fashion. Note that this is all behind the scenes; the example above is pretty much all you need.

It's important to realize that the data sent across both ways is copied. A table changed on one side won't reflect on the other side. Also, metatables are not serialized.

[1]: http://github.com/Twisol/MUSHclient-PPI

'Soludra' on Achaea

Blog: http://jonathan.com/
GitHub: http://github.com/Twisol
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,765 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.