The biggest annoyance and pitfall is basic errors caused by typos. In Lua, unlike other languages, you don’t need to declare variables.
If you use a variable without declaring it, it becomes a global. This can cause uncaught errors if you have a typo. Consider the following:
local levelComplete = true
if LevelComplete == true then
print(“you won”)
end
Notice the typo, ‘LevelComplete’ with a capital ‘L’. Lua/Corona won’t catch this and instead make ‘LevelComplete’ a new global, and of course it will never be set to true, and so you could never win this game!
To get around the problem you can add the following code to the beginning of your main.lua to catch these issues. This will prevent ALL globals from being declared, and so would catch the above by displaying an error.
function declare (name, initval)
rawset(_G, name, initval or false)?
end
setmetatable(_G, {
__newindex = function(_ENV, var, val)
if var ~= “tableDict” then
error((“attempt to set undeclared global\”%s\”"):format(tostring(var)), 2)
else?
rawset(_ENV, var, val)
end
end,
__index = function(_ENV, var, val)
if var ~= “tableDict” then?
error((“attempt to read undeclared global\”%s\”"):format(tostring(var)), 2) else
rawset(_ENV, var, val)?
end?
end,
})
If you do want to use a Global then you need to use declare as below:
declare(“Levels”)
declare(“BonusLevels”)



