Skip to Content
Lua Quick Start Guide
book

Lua Quick Start Guide

by Gabor Szauer
July 2018
Beginner
202 pages
5h 42m
English
Packt Publishing
Content preview from Lua Quick Start Guide

Explicit variables

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 ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Beginning Lua Programming

Beginning Lua Programming

Kurt Jung, Aaron Brown
Vim Masterclass

Vim Masterclass

Jason Cannon

Publisher Resources

ISBN: 9781789343229Supplemental Content