Game Development Reference
In-Depth Information
Debugging in Lua - the knights who say nil
Debugging your Lua code can be frustrating at times. But you soon learn to distinguish
between the minute subtleties in Lua's runtime errors. The compiler will say something is
nil
(Lua's
null
) in about 99.9 percent of cases. It's up to you to figure out why. Here are
the main culprits:
• You are referencing an object's own property without prepending
self.
or
self:
.
• You are calling an instance method with a dot notation, and not passing the in-
stance as the first parameter; something like
myObject.myMethod()
instead
of
myObject.myMethod(myObject)
. Use
myObject:myMethod()
in-
stead.
• You are referencing a variable from a place outside its scope. For example, a local
variable declared inside an
if
statement is being referenced outside the condition-
al.
• You forgot to return the class object at the end of your class or module/table de-
claration.
• You tried to access the zero index of an array.
• You forgot to add a few dos and thens or ends.
• And finally, maybe you're just having one of those days. A
nil
sort of day.
The Cocos IDE will show errors in bold; the same bold it uses for global variables, which
is confusing at times. But it helps nonetheless. Just make a habit of scanning your code for
bold text!
Tip
You might need to increase the heap memory inside the IDE. The quickest way to accom-
plish this is to find the file called
eclipse.ini
inside the Cocos IDE application folder.
On a Mac, this means inside the Cocos IDE app package: right-click on the app icon, select
Show Package Contents
, and then navigate to
Contents/MacOS/eclipse.ini
.
Then find the line where you read
-Xmx256m
or
-Xmx512m
and change it to
-
Xmx1024m
.
This might help in slower computers. My laptop crashed a lot while running the IDE.