July 2018
Beginner
202 pages
5h 42m
English
The loose, typed nature of variables in Lua can be great for prototyping games quickly, but it can also lead to a lot of bugs! For example, consider the following code:
five21 = 521 -- Variable name ends with a 1for i=1,1000 do if i == five21 then five2l = "Five Twenty One" -- ERROR! Variable name ends with an l break endendprint("value: " .. five21)
Can you spot the problem? During the first assignment, the five21 variable ends with a 1, but in the second assignment it ends with a lower case l. Characters such as 0 and O, 1 and l, or I and l can lead to easy typos depending on the font being used.
You can fix these types of typos using explicit variable declaration. Set the __newindex and __index metamethods of the global ...