This is a read-only snapshot of the ComputerCraft forums,
taken in April 2020.
[Lua][Question] Storing Functions in a Table
Started by Henness, 10 January 2013 - 08:22 AMPosted 10 January 2013 - 09:22 AM
I have seen a few programs store functions in a table like this one. I just wondering what are the benefits of doing that. Does it allow you to do something that you would not normally be able to do?
Posted 10 January 2013 - 09:38 AM
It makes function renaming a ton easier. Let's I have a neat group of functions that do nothing, and I went about making my functions the "normal" way.
But then I decide to make my group actually do something! So I'll just rename it to something and
Especially annoying if you don't have a text editor with multi-support select, e.g. CC's editor. D:
nothing = {}
function nothing.stuff()
end
function nothing.moreStuff()
end
-- like 12 more functions
But then I decide to make my group actually do something! So I'll just rename it to something and
something = {}
function nothing.stuff()
end
function nothing.moreStuff()
end
-- like 12 more functions THAT I HAVE TO RENAME AAAA
Especially annoying if you don't have a text editor with multi-support select, e.g. CC's editor. D:
Posted 10 January 2013 - 11:01 AM
And the actual storing of the functions in a table means that you can call them using variables, which is a bit more difficult if you don't have them in a table.
Like:
Like:
funcName = read()
if turtle[funcName] then turtle[funcName]() end
There, now I can use user input to decide which turtle function to call, cause they are all in a table (and indexed by strings).Posted 10 January 2013 - 11:23 AM
And the actual storing of the functions in a table means that you can call them using variables, which is a bit more difficult if you don't have them in a table.
Like:There, now I can use user input to decide which turtle function to call, cause they are all in a table (and indexed by strings).funcName = read() if turtle[funcName] then turtle[funcName]() end
Well that is incredibly useful for a project I'm working on :)/>
Posted 10 January 2013 - 11:31 AM
A lot of people use it for menu's with handler to call the selected item.
tab = {
{text = "Option 1", color = colours.red, handler = function() print("...") end},
{text = "Option 2", color = colours.yellow, handler = function() print("...") end}
}
etc etc.. menu code...
selected = 2 or whatever
...
tab[selected].handler() -- will call the selected function within the table.
Posted 10 January 2013 - 12:56 PM
I use them for the method that remiX stated, however I do it a little different… I create the function then store its reference… like this
its the same basic principle, the only difference is my doSomething function isn't limited to only being used by the table, anything else can use it to if i want to to…
local function doSomething()
print( "Did it" )
end
local tTable = {
{ text = "Option", colour = colors.orange, action = doSomething }
}
tTable[ selection ].action()
its the same basic principle, the only difference is my doSomething function isn't limited to only being used by the table, anything else can use it to if i want to to…
Posted 10 January 2013 - 01:01 PM
I use them for the method that remiX stated, however I do it a little different… I create the function then store its reference… like thislocal function doSomething() print( "Did it" ) end local tTable = { { text = "Option", colour = colors.orange, action = doSomething } } tTable[ selection ].action()
its the same basic principle, the only difference is my doSomething function isn't limited to only being used by the table, anything else can use it to if i want to to…
Well, there's no need to create a new function just for one line :P/> I only do it the way i stated if I change/do only 5 or less things, else I create a function for it
Posted 10 January 2013 - 01:03 PM
Well, there's no need to create a new function just for one line :P/> I only do it the way i stated if I change/do only 5 or less things, else I create a function for it
I never stated your way was wrong or anything, I just stated that I don't always use it that way and provided them with another way… If I know my use case and know its not going to be used later I do it like that, but say for a button that also has a key shortcut. I don't I have it as a function.