[Home] [Downloads] [Search] [Help/forum]

Gammon Software Solutions forum

See www.mushclient.com/spam for dealing with forum spam. Please read the MUSHclient FAQ!

[Folder]  Entire forum
-> [Folder]  MUSHclient
. -> [Folder]  Lua
. . -> [Subject]  Calling a function by string name

Home  |  Users  |  Search  |  FAQ
Username:
Register forum user name
Password:
Forgotten password?
(New message)
Subject: Calling a function by string name
Name:
Your forum user name.
Register forum user name
Password:
Your forum password.
Forgotten password?
Message:
Message to be posted (in English, please)
Maximum of 6000 characters. Text only please, no HTML.
Forum codes:
Check this if your message uses 'forum codes' or templates (auto-detected for new posts).
Forum codes Templates

Save this message ...


Subject review (reverse sequence)

Posted by Fadedparadox   USA  (91 posts)  [Biography] bio
Date Mon 25 Aug 2008 08:45 AM (UTC)  quote  ]
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
[Go to top] top

Posted by Nick Gammon   Australia  (18,801 posts)  [Biography] bio   Forum Administrator
Date Mon 25 Aug 2008 06:05 AM (UTC)  quote  ]

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
[Go to top] top

Posted by Fadedparadox   USA  (91 posts)  [Biography] bio
Date Mon 25 Aug 2008 04:16 AM (UTC)  quote  ]

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
[Go to top] top

Posted by Twisol   USA  (2,230 posts)  [Biography] bio
Date Mon 25 Aug 2008 03:26 AM (UTC)  quote  ]

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
[Go to top] top

Posted by Nick Gammon   Australia  (18,801 posts)  [Biography] bio   Forum Administrator
Date Mon 25 Aug 2008 12:53 AM (UTC)  quote  ]
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
[Go to top] top

Posted by Nick Gammon   Australia  (18,801 posts)  [Biography] bio   Forum Administrator
Date Mon 25 Aug 2008 12:51 AM (UTC)  quote  ]
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
[Go to top] top

Posted by Twisol   USA  (2,230 posts)  [Biography] bio
Date Sun 24 Aug 2008 11:36 PM (UTC)  quote  ]
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
[Go to top] top

Posted by WillFa   USA  (517 posts)  [Biography] bio
Date Sun 24 Aug 2008 11:16 PM (UTC)  quote  ]

Amended on Sun 24 Aug 2008 11:18 PM (UTC) by WillFa

Message
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.


[Go to top] top

Posted by Twisol   USA  (2,230 posts)  [Biography] bio
Date Sun 24 Aug 2008 11:10 PM (UTC)  quote  ]

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
[Go to top] top

Posted by WillFa   USA  (517 posts)  [Biography] bio
Date Sun 24 Aug 2008 10:55 PM (UTC)  quote  ]
Message
_G["funcname"](param1,param2, param3) works. :)

_G is the global environment table.
[Go to top] top

Posted by Twisol   USA  (2,230 posts)  [Biography] bio
Date Sun 24 Aug 2008 10:39 PM (UTC)  quote  ]
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
[Go to top] 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.


3,873 views.

[Reply to this subject]  Reply to this subject   [New subject]  Start a new subject   [Refresh] Refresh page

Go to topic:           Search the forum


[Go to top] top

[Home]

Written by Nick Gammon - 5K

Comments to: Gammon Software support
[RH click to get RSS URL] Forum RSS feed ( http://www.gammon.com.au/rss/forum.xml )

[Best viewed with any browser - 2K]    [Internet Contents Rating Association (ICRA) - 2K]    [Web site powered by FutureQuest.Net]