pancakepalace wrote:Hi guys,
I read all the docs I could find on the Lua language, but the use of local keyword when creating default values for function parameters is still unclear to my mind.
This is actually a pretty important subject with lua, i'll do my best to explain this for you.
Lua script uses two environments,
local and
global.
Variables, functions etc. in the local environment can only be accessed within the file in which they are created. For example:
This message cannot be called outside of its file with "msg", whereas this:
Is global, and CAN be accessed anywhere within your program.
As well local variables that are created within functions cannot be used outside of that function, another example:
Code: Select all
local doSomething = function()
local x = 1
end
print(x)
this would return nil, because "x" is local within that function, even if you were to call doSomething() before trying to print "x", it's not going to return any value.
If a variable/function/table doesn't have the word "local" in front of it, it's automatically going to be in the global environment.
It's worth noting that lua script runs faster in the local environment (the code will be executed faster and with less processing time), so using local variables and functions wherever possible is a good way to improve your code.
I'm pretty tired right now so hopefully that clears things up and I didn't just make an ass of myself.