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

Posted by Wanghom8228 on Fri 14 May 2010 09:04 AM — 2 posts, 12,426 views.

#0
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. :(
USA #1
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