12-03-2015, 05:58 PM (This post was last modified: 12-03-2015, 06:09 PM by Zee1234.)
(12-03-2015, 05:29 PM)NiLSPACE Wrote: Try this:
cPluginManager:Get():ForEachPlugin(
function(a_Plugin)
cPluginManager:CallPlugin(a_Plugin:GetName(), mySpecialFunction)
end
)
Also, are you sure a function exists in all plugins with the content of "mySpecialFunction"?
Ignore this:
Swapped to a server where my plugin is the ONLY plugin.
[ spoiler="The new code"]
function Initialize(Plugin)
Plugin:SetName( "HookLoadedTest" )
Plugin:SetVersion( 15 )
cPluginManager:AddHook(cPluginManager.HOOK_PLUGINS_LOADED,OnPluginsLoaded)
LOG("Initialised " .. Plugin:GetName() .. " v." .. Plugin:GetVersion())
cRoot:Get():GetPluginManager():BindConsoleCommand("doallplugins",doAllPlugins,"")
return true
end
function OnDisable()
LOG( "Disabled Core!" )
end
function OnPluginsLoaded()
LOG("Plugins loaded!asdfasdrfqwerasdfqwer")
end
function doAllPlugins(split)
--[[
local PLMan = cRoot:Get():GetPluginManager()
PLMan:ForEachPlugin(testfunction)
--]]
cPluginManager:Get():ForEachPlugin(
function(a_Plugin)
cPluginManager:CallPlugin(a_Plugin:GetName(), "mySpecialFunction")
end
)
end
function mySpecialFunction()
return true
end
function testfunction(plugin)
cRoot:Get():GetPluginManager():CallPlugin(plugin:GetName(),"mySpecialFunction")
end
[ /spoiler]
And the new... interesting error:
Code:
doallplugins
[01:49:18] Executing console command: "doallplugins"
[01:49:18] cPluginManager::CallPlugin(): Calling self is not implemented (why would it?)
[01:49:18] Stack trace:
[01:49:18] [C](-1): CallPlugin
[01:49:18] Plugins/HookLoadedTest/main.lua(28): (no name)
[01:49:18] [C](-1): ForEachPlugin
[01:49:18] Plugins/HookLoadedTest/main.lua(26): (no name)
[01:49:18] Stack trace end
[01:49:18] Unknown command, type 'help' for all commands.
Same error if I copy the same code to a new plugin (and remove the command binding from the second one). Adding :Get() in the second part results in this:
[ spoiler]
Code:
doallplugins
[01:54:56] Executing console command: "doallplugins"
[01:54:56] LUA: Plugins/HookLoadedTest/main.lua:28: error in function 'CallPlugin'.
argument #1 is 'cPluginManager'; 'cPluginManager' expected.
[01:54:56] Stack trace:
[01:54:56] [C](-1): CallPlugin
[01:54:56] Plugins/HookLoadedTest/main.lua(28): (no name)
[01:54:56] [C](-1): ForEachPlugin
[01:54:56] Plugins/HookLoadedTest/main.lua(26): (no name)
[01:54:56] Stack trace end
[01:54:56] Error in <attached> calling function <callback>()
[01:54:56] LUA: Plugins/HookLoadedTest/main.lua:28: error in function 'CallPlugin'.
argument #1 is 'cPluginManager'; 'cPluginManager' expected.
[01:54:56] Stack trace:
[01:54:56] [C](-1): CallPlugin
[01:54:56] Plugins/HookLoadedTest/main.lua(28): (no name)
[01:54:56] [C](-1): ForEachPlugin
[01:54:56] Plugins/HookLoadedTest/main.lua(26): (no name)
[01:54:56] Stack trace end
[01:54:56] Error in <attached> calling function <callback>()
[01:54:56] LUA: Plugins/HookLoadedTest/main.lua:28: error in function 'CallPlugin'.
argument #1 is 'cPluginManager'; 'cPluginManager' expected.
[01:54:56] Stack trace:
[01:54:56] [C](-1): CallPlugin
[01:54:56] Plugins/HookLoadedTest/main.lua(28): (no name)
[01:54:56] [C](-1): ForEachPlugin
[01:54:56] Plugins/HookLoadedTest/main.lua(26): (no name)
[01:54:56] Stack trace end
[01:54:56] Error in <attached> calling function <callback>()
[01:54:56] Unknown command, type 'help' for all commands.
[ /spoiler]
Moving the callback to a named function only affects the related line numbers in the errors.
Edit: The interesting error is the obvious error! I think it actually works. Doing one last test to verify.
Edit2: yeah I was being a derp in the second part. The "interesting" error was just saying "I won't let you call a function within your own code" which makes sense. Doing more testing, but I think this will work.
Final Edit: It continues to work even on a server with a number of plugins that don't have "mySpecialFunction", although it does spam console with errors. Is there a way to suppress console error messages?