What’s better?
I like the first one better because it looks more cleaner than the second one.
Would there be a difference in performance or what should I know about doing this?
Sorry for the horrible naming of this topic and horrible explanation.
What’s better?
I like the first one better because it looks more cleaner than the second one.
Would there be a difference in performance or what should I know about doing this?
Sorry for the horrible naming of this topic and horrible explanation.
I prefer number 2, as I dislike if statements spam.
However it’s not organized because it’s not in a table.
When there are multiple things use a table that stores multiple things. In this case it’s a dictionary as each function is “unique”
local printFunctions = {
Print1 = function()
end,
Print2 = function()
end,
Print3 = function()
end,
}
printFunctions.Print1()
The other one looks better to me. But I’m just wondering if the way I’m doing it does anything with performance plus doesn’t having more things in a table use up more memory?
To me, I’d really rather prefer to have →
local functions = {
Print1 = print,
Print2 = function() print("123456678910") end,
Print3 = function( ... ) print( "Print3", ... ) end,
}
functions.Print1("Hello!")
functions.Print2()
functions.Print3("Hello, hows your day been?")
or alternatively, to fit with the framework of the other,
local functions = {
Print1 = print,
Print2 = function() print("123456678910") end,
Print3 = function( ... ) print( "Print3", ... ) end,
}
function_ = function( option, ... )
if ( functions[option] ) then
functions[option](...)
end
end
function_("Print1", "Hello!")
function_("Print2")
function_("Print3", "Hello, hows your day been?")
If you’re not running these that often, I’d say readability > how efficient it is. Storing the functions in an array, and making it neat and easy to read is better, because of the high chance you’ll need to add more functions in the future.
Hmm, I guess it’s alright with if statements since there is only three in a row. Plus the functions are really similar to each other as they just print, I don’t believe it’s a good example (it just prints lol) if it’s more in context then perhaps it would be different. Maybe something more different yet under the same category is better with a table method like a table of disasters storing a disaster function meteor, flood, lightning, etc.
However let’s not go into micro-optimization yet with memory. It’s pretty pointless, if you are interested you could go to script performance in the console and check out the memory but the difference will be minimal like 10 mb perhaps? Not noticeable in game especially for this one single table with only three functions or 100 compared to a 3d generated terrain map table data hence go for readability like @xKiiyoko said.
You could also argue that if you are trying to access Print 3 in method 1 the function will have to first check print 1 then print 2. But the difference is minimal and not noticeable untill you repeat it up to a big number like 10000. So also go for readability instead of performance.
Okay, thanks! I’m terrible at the performance log thing anyways lol
If you’re not running them that often then they shouldn’t really be high-level functions. They won’t be automatically garbage collected. It’s probably more performant to just write function
per call at that point or not use any outright function at all.
Because of its location it will take longer to access, but not anything that could ever be noticed. Everything in Lua is object based, a function’s memory cost exists within its individual existence on the stack as well as whatever occurs when it is called.
I’d also like to add that at a certain point you’re more or less writing a Lua package, and you might as well use a module instead of an explicit table.
this looks like a modulescript in a script lol.