I created a convenience function to remove display objects:
function removeDisplayObject(obj) display.remove(obj) obj = nil end
However, when I call the function on a display object…
removeDisplayObject(a) if (a == nil) then print "object is nil" else print "object is not nil" end
… the display object passed to the function isn’t being set to nil. Anyone know why? My goal is to properly remove a display object with a single code statement.
The variable “obj” is only local inside the function and gets niled out automatically.
So do the following:
function removeDisplayObject(obj) display.remove(obj) end removeDisplayObject(a) a = nil if (a == nil) then print "object is nil" else print "object is not nil" end
But as you can see, it’s a bit redundant to call function with only one function in it.
So you can just do this.
display.remove(a) a = nil if (a == nil) then print "object is nil" else print "object is not nil" end
I think it should be noted that the line above will set ‘myObject’ to nil simply because ‘removeSelf’ has no return value, and so Lua defaults that value to ‘nil’.
That function removes the display object and converts it into a standard Lua table that should be garbage collected. But there is a caveat here, if anything is still referencing the aforementioned table, it will not be garbage collected (but instead be treated as a standard Lua table).
So it is better practise to always set the variable to nil, after removing it from the display.
The variable “obj” is only local inside the function and gets niled out automatically.
So do the following:
function removeDisplayObject(obj) display.remove(obj) end removeDisplayObject(a) a = nil if (a == nil) then print "object is nil" else print "object is not nil" end
But as you can see, it’s a bit redundant to call function with only one function in it.
So you can just do this.
display.remove(a) a = nil if (a == nil) then print "object is nil" else print "object is not nil" end
I think it should be noted that the line above will set ‘myObject’ to nil simply because ‘removeSelf’ has no return value, and so Lua defaults that value to ‘nil’.
That function removes the display object and converts it into a standard Lua table that should be garbage collected. But there is a caveat here, if anything is still referencing the aforementioned table, it will not be garbage collected (but instead be treated as a standard Lua table).
So it is better practise to always set the variable to nil, after removing it from the display.
timer.cancel() allocates some memory, this is cleared when it’s canceled. However, timer.cancel() returns a table. That table takes up some memory. Nilling it will let GC clear it. You may think it just overwrites the varaible, but Lua can actually trigger code when setting variables.
timer.cancel() allocates some memory, this is cleared when it’s canceled. However, timer.cancel() returns a table. That table takes up some memory. Nilling it will let GC clear it. You may think it just overwrites the varaible, but Lua can actually trigger code when setting variables.