• Calling A Function From A Table
    7 replies, posted
Is There Anyway To Call A Function From A Table At All. I've Tried To Insert The Function Name Into The Table With Arguments But It Returns A Nil Value.
Code?
Something like this perhaps? [lua] --Create a table T_Functions = {); T_Functions[1] = "a"; T_Functions[2] = "b"; T_Functions[1] = "c"; function Process(input) --Based on input, do something different if (input=="a") then DoShitLikeThis(); elseif (input=="b") then DoShitTheOtherWayAround(); else DoShitLikeThat(); end end Process(T_Functions[1]); Process(T_Functions[2]); Process(T_Functions[3]); [/lua]
[QUOTE=Anthophobian;22956255]Something like this perhaps? [lua] --Create a table T_Functions = {); T_Functions[1] = "a"; T_Functions[2] = "b"; T_Functions[1] = "c"; function Process(input) --Based on input, do something different if (input=="a") then DoShitLikeThis(); elseif (input=="b") then DoShitTheOtherWayAround(); else DoShitLikeThat(); end end Process(T_Functions[1]); Process(T_Functions[2]); Process(T_Functions[3]); [/lua][/QUOTE] Idiot, should have let him posted his code so we can see what he is doing wrong.
I'm assuming what you tried was [lua]tab[1] = funcName( arg1, arg2 )[/lua] where what you want to do is [lua]tab[1] = funcName[/lua]
[QUOTE=Cubar;22964096]Idiot, should have let him posted his code so we can see what he is doing wrong.[/QUOTE] Oor, post an example, so he can see what he did wrote, or learn/Change the code.
This is a little something I did a while back, just an attempt at a truth-table sort of function call. I'm still not sure why I did it, and I don't know if it's more efficient than just an if tree, but it's certainly easier to add more possibilities. [code] SuperScript = {} SuperScript.Item = 100 function SuperScript.GiveItem(num) SuperScript.Item = SuperScript.Item + num print("Added ".. num) end function SuperScript.TakeItem(num) SuperScript.Item = SuperScript.Item - num print("Subtracted ".. num) end local TT = {} TT[00] = function() print("Nothing Happened") end TT[01] = function(num) SuperScript.TakeItem(num) end TT[10] = function(num) SuperScript.GiveItem(num) end TT[11] = function(num) SuperScript.GiveItem(num) SuperScript.TakeItem(num) end function SuperScript.CalcItem(give, take, number) local slot = (give * 10) + take TT[slot](number) end print("Before") print(SuperScript.Item) SuperScript.CalcItem(0,0,10) SuperScript.CalcItem(0,1,5) SuperScript.CalcItem(1,0,15) SuperScript.CalcItem(1,1,50) print("After") print(SuperScript.Item) [/code]
Here is how you call a function from a table. There are two ways, as shown in the code. [lua]local t = {} t.func = function(name) print("Hello, " .. name) end -- Option 1 t.func( "Entoros" ) -- Option 2 t["func"]( "Entoros" )[/lua]
Sorry, you need to Log In to post a reply to this thread.