Notice: Any messages purporting to come from this site telling you that your password has expired, or that you need to verify your details, confirm your email, resolve issues, making threats, or asking for money, are
spam. We do not email users with any such messages. If you have lost your password you can obtain a new one by using the
password reset link.
Entire forum
➜ MUSHclient
➜ Lua
➜ Calling a function by string name
Calling a function by string name
|
It is now over 60 days since the last post. This thread is closed.
Refresh page
Posted by
| Twisol
USA (2,257 posts) Bio
|
Date
| Sun 24 Aug 2008 10:39 PM (UTC) |
Message
| 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! |
'Soludra' on Achaea
Blog: http://jonathan.com/
GitHub: http://github.com/Twisol | Top |
|
Posted by
| WillFa
USA (525 posts) Bio
|
Date
| Reply #1 on Sun 24 Aug 2008 10:55 PM (UTC) |
Message
| _G["funcname"](param1,param2, param3) works. :)
_G is the global environment table.
| Top |
|
Posted by
| Twisol
USA (2,257 posts) Bio
|
Date
| Reply #2 on Sun 24 Aug 2008 11:10 PM (UTC) Amended on Sun 24 Aug 2008 11:12 PM (UTC) by Twisol
|
Message
| 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... |
'Soludra' on Achaea
Blog: http://jonathan.com/
GitHub: http://github.com/Twisol | Top |
|
Posted by
| WillFa
USA (525 posts) Bio
|
Date
| Reply #3 on Sun 24 Aug 2008 11:16 PM (UTC) Amended on Sun 24 Aug 2008 11:18 PM (UTC) by WillFa
|
Message
| |
Posted by
| Twisol
USA (2,257 posts) Bio
|
Date
| Reply #4 on Sun 24 Aug 2008 11:36 PM (UTC) |
Message
| 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... |
'Soludra' on Achaea
Blog: http://jonathan.com/
GitHub: http://github.com/Twisol | Top |
|
Posted by
| Nick Gammon
Australia (23,102 posts) Bio
Forum Administrator |
Date
| Reply #5 on Mon 25 Aug 2008 12:51 AM (UTC) |
Message
| 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
|
- Nick Gammon
www.gammon.com.au, www.mushclient.com | Top |
|
Posted by
| Nick Gammon
Australia (23,102 posts) Bio
Forum Administrator |
Date
| Reply #6 on Mon 25 Aug 2008 12:53 AM (UTC) |
Message
|
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. |
- Nick Gammon
www.gammon.com.au, www.mushclient.com | Top |
|
Posted by
| Twisol
USA (2,257 posts) Bio
|
Date
| Reply #7 on Mon 25 Aug 2008 03:26 AM (UTC) Amended on Mon 25 Aug 2008 03:54 AM (UTC) by Twisol
|
Message
| 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) |
'Soludra' on Achaea
Blog: http://jonathan.com/
GitHub: http://github.com/Twisol | Top |
|
Posted by
| Fadedparadox
USA (91 posts) Bio
|
Date
| Reply #8 on Mon 25 Aug 2008 04:16 AM (UTC) Amended on Mon 25 Aug 2008 04:27 AM (UTC) by Fadedparadox
|
Message
| 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
| Top |
|
Posted by
| Nick Gammon
Australia (23,102 posts) Bio
Forum Administrator |
Date
| Reply #9 on Mon 25 Aug 2008 06:05 AM (UTC) Amended on Mon 25 Aug 2008 06:08 AM (UTC) by Nick Gammon
|
Message
| 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
|
- Nick Gammon
www.gammon.com.au, www.mushclient.com | Top |
|
Posted by
| Fadedparadox
USA (91 posts) Bio
|
Date
| Reply #10 on Mon 25 Aug 2008 08:45 AM (UTC) |
Message
| 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
| Top |
|
The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).
To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.
30,841 views.
It is now over 60 days since the last post. This thread is closed.
Refresh page
top