Hello everyone !
As most of Lua users, especially in the wc3 context, there is a big problem we often encounter.
Lua is extremely permissive as its variables have no type.
You all know that only values are typed in Lua.
Declaring globals not being mandatory in Lua, combined with this lack of type safety, could make your life really difficult.
A single syntax error will silently break your map, and don't count on Lua to be verbose and tell you that something went wrong. Most of the time the thread with the syntax error will just crash with no apparent effect (and prevent the rest of the actions in the same thread from running).
There is a simple trick, directly borrowed from the free first edition of Programming In Lua.
All you have to do is add this line to your map triggers.
Don't put it in the Lua root or it will prevent your map from running.
Just put the line somewhere after map init, or even better, when your map game actually starts :
For example, in my map, I place this at the end of a "GameStarted" function, before the first level's countdown is started.
-> every time your script uses a global variable that is not defined, something that very often is caused by a syntax error, the script will display a warning with the name of the faulty global variable.
That's all, I hope this will help you.
IMPORTANT NOTE :
What this function does is intercept the index operator of tables in the script.
This means it won't catch attempts to access the value of a nil global variable, but rather the attempt to access the index of a global table that has not been defined as a table.
This said, non-table variable never require to be declared.
Note 2 :
I have to correct my previous note.
The way Lua works can sometimes be a bit confusing :
the reason it will catch attempts on tables only is not because the other variables are not tables, as every global variable is a part of the globals table called _G.
The reason is that there is no inherent problem with accessing globals as non-tables. They will simply be considered as nil.
Comparing to nil is a recurrent way to check if a variable is referring to a value or not.
But if you try to access a value indexed in a global while this global has not been defined as a table, then Lua cannot guess it is a table, and thus this will crash the current thread.
Sorry if this was a bit confusing. Fact is this simple line of code is really effective for recurrent issues with tables that have not been defined.
As most of Lua users, especially in the wc3 context, there is a big problem we often encounter.
Lua is extremely permissive as its variables have no type.
You all know that only values are typed in Lua.
Declaring globals not being mandatory in Lua, combined with this lack of type safety, could make your life really difficult.
A single syntax error will silently break your map, and don't count on Lua to be verbose and tell you that something went wrong. Most of the time the thread with the syntax error will just crash with no apparent effect (and prevent the rest of the actions in the same thread from running).
There is a simple trick, directly borrowed from the free first edition of Programming In Lua.
All you have to do is add this line to your map triggers.
Don't put it in the Lua root or it will prevent your map from running.
Just put the line somewhere after map init, or even better, when your map game actually starts :
Lua:
setmetatable(_G,{__index=function(_, n) print("Trying to read undeclared global : "..tostring(n)) end,})
For example, in my map, I place this at the end of a "GameStarted" function, before the first level's countdown is started.
-> every time your script uses a global variable that is not defined, something that very often is caused by a syntax error, the script will display a warning with the name of the faulty global variable.
That's all, I hope this will help you.
IMPORTANT NOTE :
What this function does is intercept the index operator of tables in the script.
This means it won't catch attempts to access the value of a nil global variable, but rather the attempt to access the index of a global table that has not been defined as a table.
This said, non-table variable never require to be declared.
Note 2 :
I have to correct my previous note.
The way Lua works can sometimes be a bit confusing :
the reason it will catch attempts on tables only is not because the other variables are not tables, as every global variable is a part of the globals table called _G.
The reason is that there is no inherent problem with accessing globals as non-tables. They will simply be considered as nil.
Comparing to nil is a recurrent way to check if a variable is referring to a value or not.
But if you try to access a value indexed in a global while this global has not been defined as a table, then Lua cannot guess it is a table, and thus this will crash the current thread.
Sorry if this was a bit confusing. Fact is this simple line of code is really effective for recurrent issues with tables that have not been defined.
Last edited: