=475FG= Dawger Posted February 18 Posted February 18 (edited) I need to call functions using a variable to store the function name and then call that function using the variable. Anyone know how to do this? @Grimes Edited February 18 by =475FG= Dawger
Actium Posted February 18 Posted February 18 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 Grimes Posted February 19 Solution Posted February 19 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 1 The right man in the wrong place makes all the difference in the world. Current Projects: Grayflag Server, Scripting Wiki Useful Links: Mission Scripting Tools MIST-(GitHub) MIST-(Thread) SLMOD, Wiki wishlist, Mission Editing Wiki!, Mission Building Forum
=475FG= Dawger Posted February 19 Author Posted February 19 (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 February 19 by =475FG= Dawger
Recommended Posts