Jump to content

Call function using variable as function name


Go to solution Solved by Grimes,

Recommended Posts

Posted

There you go:

-- a random function
function random_function()
    return 42
end

-- name of random function as a string variable
local function_name = "random_function"

-- recover the original function
local recovered_function = loadstring("return " .. function_name)()
-- CAN BE REMOVED: just to prove both functions are identical
assert(recovered_function == random_function)

-- call the recovered random function
return recovered_function()

 

  • Solution
Posted

It could be defined in a table and then you just reference that table entry when calling it. 

local tbl = {}
function tbl.doWhatever()
   env.info("hello world")
end

for fcnName, _ in pairs(tbl) do  -- super lazy example
     tbl[fcnName]()
end
  • Like 1

The right man in the wrong place makes all the difference in the world.

Current Projects:  Grayflag ServerScripting Wiki

Useful Links: Mission Scripting Tools MIST-(GitHub) MIST-(Thread)

 SLMOD, Wiki wishlist, Mission Editing Wiki!, Mission Building Forum

Posted (edited)

Thanks for the suggestions.

What I have working at the moment is this:

_airbaseNeutral = _airbaseName..'Neutral'

_G[_airbaseNeutral]()

Apparently, this works for functions in the Global namespace.

Edited by =475FG= Dawger

 

 

 

 

EDsignaturefleet.jpg

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...