Locals vs self. vars in modules

Hi all,

what do you guys think, what is more likely to do in the following example

cameraHelper

situation 1

local blackRectangle local cameraHelper = {} function cameraHelper:toBlack() blackRectangle = display.newRect(0, 0, \_width, \_height) end function cameraHelper:unBlack() blackRectangle:removeSelf() end return cameraHelper

situation 2

local cameraHelper = {} function cameraHelper:toBlack() self.blackRectangle = display.newRect(0, 0, \_width, \_height) end function cameraHelper:unBlack() self.blackRectangle:removeSelf() end return cameraHelper

Should I choose for situation 1 or 2?

Both are fine, IMHO, but because .self does not refer to the cameraHelper object I would always try to keep references within the table being returned, like this:

local cameraHelper = {} function cameraHelper:toBlack() if (cameraHelper.blackRectangle == nil) then cameraHelper.blackRectangle = display.newRect(0, 0, \_width, \_height) end end function cameraHelper:unBlack() if (cameraHelper.blackRectangle) then cameraHelper.blackRectangle:removeSelf() cameraHelper.blackRectangle = nil end end return cameraHelper

The extra checks are important to avoid calls trying to operate on nil values.

if you’re using it as a singleton (as it appears) then either form is fine.  (though the first form does a better job hiding blackRectangle entirely from external code)  second form would be needed if creating multiple instances that each need their own blackRectangle.

fwiw: if it were me, and i expected to call toBlack/unBlack repeatedly, i’d instead have create/destroy routines too, pass those (apparently global) _width/_height values to create() and create the rectangle just once (and eliminate dependence on globals).  toBlack/unBlack would simply set .isVisible true/false.  destroy() would remove and nil the reference.  hth

Tip:

Don’t use :

obj:removeSelf()

Use this instead:

display.remove( obj )

It is safer.  How?  If the obj has already been removed the code will encounter an error and may crash.

Ex:

local group = display.newGroup() local tmp = display.newCircle( group, 100, 100, 10 ) -- ... display.remove(group) -- removes group and tmp tmp:removeSelf() -- ERROR!

Ex2:

local group = display.newGroup() local tmp = display.newCircle( group, 100, 100, 10 ) -- ... display.remove(group) -- removes group and tmp display.remove(tmp) -- SAFE!

Thank you all for the information! I get it now.

I didnt know that there was a difference with display.remove vs removeSelf, thanks for pointing that out too!

Both are fine, IMHO, but because .self does not refer to the cameraHelper object I would always try to keep references within the table being returned, like this:

local cameraHelper = {} function cameraHelper:toBlack() if (cameraHelper.blackRectangle == nil) then cameraHelper.blackRectangle = display.newRect(0, 0, \_width, \_height) end end function cameraHelper:unBlack() if (cameraHelper.blackRectangle) then cameraHelper.blackRectangle:removeSelf() cameraHelper.blackRectangle = nil end end return cameraHelper

The extra checks are important to avoid calls trying to operate on nil values.

if you’re using it as a singleton (as it appears) then either form is fine.  (though the first form does a better job hiding blackRectangle entirely from external code)  second form would be needed if creating multiple instances that each need their own blackRectangle.

fwiw: if it were me, and i expected to call toBlack/unBlack repeatedly, i’d instead have create/destroy routines too, pass those (apparently global) _width/_height values to create() and create the rectangle just once (and eliminate dependence on globals).  toBlack/unBlack would simply set .isVisible true/false.  destroy() would remove and nil the reference.  hth

Tip:

Don’t use :

obj:removeSelf()

Use this instead:

display.remove( obj )

It is safer.  How?  If the obj has already been removed the code will encounter an error and may crash.

Ex:

local group = display.newGroup() local tmp = display.newCircle( group, 100, 100, 10 ) -- ... display.remove(group) -- removes group and tmp tmp:removeSelf() -- ERROR!

Ex2:

local group = display.newGroup() local tmp = display.newCircle( group, 100, 100, 10 ) -- ... display.remove(group) -- removes group and tmp display.remove(tmp) -- SAFE!

Thank you all for the information! I get it now.

I didnt know that there was a difference with display.remove vs removeSelf, thanks for pointing that out too!