HOWTO: Inter-plugin communication - Printable Version +- Cuberite Forum (https://forum.cuberite.org) +-- Forum: Plugins (https://forum.cuberite.org/forum-1.html) +--- Forum: Plugin Discussion (https://forum.cuberite.org/forum-8.html) +--- Thread: HOWTO: Inter-plugin communication (/thread-579.html) |
HOWTO: Inter-plugin communication - FakeTruth - 10-11-2012 As of rev945 I added the functionality needed to call functions on another plugin. Even though this opens possibilities of deadlocks, these possibilities were already always there so I thought there was no harm in adding this. To use this, first you need to acquire a reference to the plugin you want to use. You get this reference by asking the plugin manager for a plugin by its name. PluginManager = cRoot:Get():GetPluginManager() local CorePlugin = PluginManager:GetPlugin("Core") Now you have this reference you can call ANY global function in its script. CorePlugin:Call("HandleReloadCommand")This will reload all plugins. The HandleReloadCommand is (AFAIK) the only function that does not require an array of strings or a player reference. This Call() function is very flexible though, it can take any number of arguments and returns any number of values. You can for example do this -- Plugin1 local Coins = 0 function GetCoins() return Coins end function AddCoins( Amount, PlayerName ) Coins = Coins + Amount LOG("Added " .. Amount .. " coins to " .. PlayerName ); return Coins, PlayerName end -- Plugin2 function HandleCoinCommand() PluginManager = cRoot:Get():GetPluginManager() local Plugin1 = PluginManager:GetPlugin("Plugin1") LOG( "Coins: " .. Plugin1:Call("GetCoins") ) local NumCoins, PlayerName = Plugin1:Call("AddCoins", 100, "FakeTruth") LOG("AddCoins returned: ".. NumCoins ..", ".. PlayerName ) LOG( "Coins: " .. Plugin1:Call("GetCoins") ) end When Plugin2:HandleCoinCommand() is called, this should be the output Coins: 0 Added 100 coins to FakeTruth AddCoins returned: 100, FakeTruth Coins: 100 There's one thing you should keep in mind though, and that is that you can only pass integers, booleans, strings and usertypes (cPlayer, cEntity, cCuboid, etc.). This means NO tables, arrays, lists, etc. RE: HOWTO: Inter-plugin communication - ThuGie - 10-11-2012 Really nice, And amazed at the speed you added it! Will use it today! haha as its past 12:00 :p. RE: HOWTO: Inter-plugin communication - xoft - 10-11-2012 Have you done any measurements as to the speed of this? I recall it being mentioned that people wanted to use this as for "code library plugins", but from the underlying code it would seem that the Plugin:Call() call is quite time-consuming, so a code library usage would be a bad idea. RE: HOWTO: Inter-plugin communication - FakeTruth - 10-11-2012 What kind of code library? Doing this 100 times per tick should still be fast I think (I'll do some tests) However doing things a million times per tick in Lua is not recommended anyway. Okay doing it 10 times a tick goes by unnoticed. 100 times adds up to 3% of my CPU (including excessive logging and passing 3 arguments) all in debug mode. When I disable the logging it uses 0% of my CPU. In release mode it uses 0~1% CPU with logging Looks pretty fast to me, I logged something in the log every time the function was called and that seems to be the biggest bottleneck. No logging, debug mode 10000 calls per tick uses 0~1% of my CPU RE: HOWTO: Inter-plugin communication - xoft - 10-11-2012 Sounds reasonable Good job. RE: HOWTO: Inter-plugin communication - FakeTruth - 10-12-2012 Thanks, I'm way in over my head though. I have no idea what I'm doing but it seems to work XD RE: HOWTO: Inter-plugin communication - ThuGie - 10-12-2012 Hehe, are there really plugins that need to call it that much ? RE: HOWTO: Inter-plugin communication - xoft - 10-12-2012 Now you should take your time and document all this in the wiki RE: HOWTO: Inter-plugin communication - NiLSPACE - 10-22-2013 Is there a way to convert a table to a userdata since userdata can be passed with the Call function. How do you create a userdata object in the first place? RE: HOWTO: Inter-plugin communication - bearbin - 10-22-2013 Easiest thing to do is probably concat the table and resurrect it on the other side, asumming it's full of strings or ints. |