HOWTO: Inter-plugin communication
#1
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.
Reply
Thanks given by: ThuGie , Taugeshtu , NiLSPACE


Messages In This Thread
HOWTO: Inter-plugin communication - by FakeTruth - 10-11-2012, 08:07 AM
RE: HOWTO: Inter-plugin communication - by ThuGie - 10-11-2012, 08:11 AM
RE: HOWTO: Inter-plugin communication - by xoft - 10-11-2012, 09:08 PM
RE: HOWTO: Inter-plugin communication - by xoft - 10-11-2012, 11:05 PM
RE: HOWTO: Inter-plugin communication - by ThuGie - 10-12-2012, 12:36 AM
RE: HOWTO: Inter-plugin communication - by xoft - 10-12-2012, 04:57 AM
RE: HOWTO: Inter-plugin communication - by xoft - 10-23-2013, 05:43 AM
RE: HOWTO: Inter-plugin communication - by xoft - 10-23-2013, 06:33 AM
RE: HOWTO: Inter-plugin communication - by xoft - 11-25-2013, 01:33 AM
RE: HOWTO: Inter-plugin communication - by xoft - 12-07-2013, 06:51 AM



Users browsing this thread: 1 Guest(s)