Since Lua functions are first class citizens, maybe the question can be restated this way:Miken1 wrote:This is amazing! thank you soooo much.
Just one last question, why do you use local functions instead of just normal functions ?
In Lua, variables are global by default. Each time you create a global variable, it is stored in the global environment table, named _G (in Lua5.1). Accessing this variable later on the code triggers a table lookup.why do you use local variables instead of just global variables ?
On the other hand, local variables are stored in registers. Accessing them way more faster than a table lookup. Yet, locals have a limited scope, while globals once declared, can be accessed from everywhere in the code. And this is actually a downside, since when you are messing with multiple globals accross different files, debugging can become painful.
So, a general and common habit of Lua programmers is to use locals everytime it is possible (not just because they are fast, but because they are clean.), and prevent the global environment to be modified.
In that way, Lua's standard library functions (table, string, etc) remain untouched in the global environment.
Here are some complementary reading on the same topic that's worth reading.