Calling a function by string name

Posted by Twisol on Sun 24 Aug 2008 10:39 PM — 11 posts, 42,843 views.

USA #0
I'm trying to use CallPlugin() to pass the name of a function to a function in another plugin, basically to register a callback function if something happens in the first plugin. The problem is, I don't know of any way to call a function given just its name, or even its address, from Lua. I'm pretty new to Lua, so chances are I'm missing something obvious...

CallPlugin() quite obviously does the same thing I'm doing, though. Parameter two takes the name of the function to call in the other plugin!
USA #1
_G["funcname"](param1,param2, param3) works. :)

_G is the global environment table.
USA #2
Alright.. it doesn't look like that's working. Here's the code...


callbacks = {}

RegisterCallback = function (callbackstring)
  table.insert(callbacks, callbackstring)
end

OnPluginBroadcast = function (msg, id, name, text)
  for j,v in ipairs(callbacks) do
    _G[v](msg, id, name, text)
  end
end


It's supposed to listen for broadcasts from other plugins, and then go through the callbacks registered with it and broadcast it to them, because apparently BroadcastPlugin won't broadcast to the script file. I've managed to register the name of the function with the plugin via a call to RegisterCallback in the script file, but when this plugin recieves a broadcast, it can't call the function because it doesn't exist. I wasn't really sure _G would work in that case anyways...
Amended on Sun 24 Aug 2008 11:12 PM by Twisol
USA #3
If you're just looking to forward broadcasts to the main script, have you looked at
http://www.gammon.com.au/forum/?id=8871&page=999

There's a quick plugin that gets the effect of calling OnPluginBroadcast(...) in the main script.


Amended on Sun 24 Aug 2008 11:18 PM by WillFa
USA #4
That would work, but it seems kind of hackish. I've tried GetPluginVariable, with "" for the plugin ID as the docs say, but it still can't get my function. Rather odd, I was really hoping it would work...
Australia Forum Administrator #5
Registering the name of a function seems a round-about way of doing it to me, for one thing that will fail if the function is declared local.

Why not just store the function itself? In Lua functions are first-class values and can be stored in tables. This works for me:


callbacks = {}

function RegisterCallback  (f)
  table.insert(callbacks, f)
end

function OnPluginBroadcast (msg, id, name, text)
  for _, v in ipairs (callbacks) do
    v (msg, id, name, text)
  end
end


function a (x, y, z)
  print ("in a, x=", x)
end -- a

function b (x, y, z)
  print ("in b, x=", x)
end -- a

RegisterCallback (a)
RegisterCallback (b)


OnPluginBroadcast ("test", 1, 2, 3)


Output
in a, x= test
in b, x= test


Australia Forum Administrator #6
Quote:

It's supposed to listen for broadcasts from other plugins, and then go through the callbacks registered with it and broadcast it to them, because apparently BroadcastPlugin won't broadcast to the script file.


The script file and plugins have different script spaces, you can't call a function across script spaces.
USA #7
Well, I worked around it a bit by Executing the function to call with the scripting prefix before it, from the plugin. It works great now, and the script file doesn't even know the plugin exists (besides the fact that it gets broadcasts now).


OnPluginBroadcast = function (msg, id, name, text)
  if GetInfo(36) ~= "" and GetInfo(28) == "Lua" then
    Execute(GetInfo(36) .. "if OnPluginBroadcast ~= nil then OnPluginBroadcast(" .. msg .. ", \"" .. id .. "\", \"" .. name .. "\", \"" .. text .. "\") end")
  end
end


I built an if-statement into the Execute in case the user doesn't actually have OnPluginBroadcast defined in their script file. Thanks for the help!

(EDIT: Thanks to fadedparadox for pointing that trick out off-forums in the first place)
Amended on Mon 25 Aug 2008 03:54 AM by Twisol
USA #8
Well, why worry about what the scripting prefix is, or if it exists? Save it, change it, use it, reset it.

Also, you can alternate single and double quotes so you don't need to escape them.


OnPluginBroadcast = function (msg, id, name, text)
  if GetInfo (28) == "Lua" then
    local x = GetInfo(36)
    SetAlphaOption ("script_prefix", [[\~\]])
    Execute ([[\~\]] .. "if type (OnPluginBroadcast) == 'function' then OnPluginBroadcast (" .. msg .. ", '" .. id .. "', '" .. name .. "', '" .. text .. "') end")
    SetAlphaOption ("script_prefix", x)
  end -- if
end -- func
Amended on Mon 25 Aug 2008 04:27 AM by Fadedparadox
Australia Forum Administrator #9
To be consistent, how about:


if GetAlphaOption ("script_language") == "Lua" then
    local x = GetAlphaOption ("script_prefix")
    SetAlphaOption ("script_prefix", [[\~]])
    Execute ([[\~]] .. "if type (OnPluginBroadcast) == 'function' then OnPluginBroadcast (" .. msg .. ", '" .. id .. "', '" .. name .. "', '" .. text .. "') end")
    SetAlphaOption ("script_prefix", x)
  end -- if



Amended on Mon 25 Aug 2008 06:08 AM by Nick Gammon
USA #10
Good idea, Nick.

Also, I ran into a problem with what me and Sol were trying to do - the data we were passing back and forth often includes single or double quotes, so:


OnPluginBroadcast = function (msg, id, name, text)
  if GetAlphaOption ("script_language") == "Lua" then
    local x = GetAlphaOption ("script_prefix")
    SetAlphaOption ("script_prefix", [[\~\]])
    Execute ([[\~\]] .. "if type (OnPluginBroadcast) == 'function' then OnPluginBroadcast (" .. msg .. ", '" .. id .. "', '" .. name .. "', [[" .. text .. "]]) end")
    SetAlphaOption ("script_prefix", x)
  end -- if
end -- func