Error regarding localToContent

I have the following code:

if enemy and enemy~=nil then         local angleXpos,angleYpos=enemy:localToContent( 0,0 ) end

Now I have changed something enemy related and delete enemies earlier which results in an error sometime. It than says localToContent is not known or something like this (I don’t have the exact error right now, but try to reproduce it for posting it here)

It is 99% because of the enemy being deleted and the function with the code above still called… BUT I thought the line

if enemy and enemy~=nil then

will prevent the localToContent getting called if an enemy is not there.

My question: Is there a better way to do this if statement?

I may be wrong but I seem to remember objects can sometimes be in a state where they still technically exist (i.e. are not nil), but the things that make them a display object have been deleted.

You could try checking whether enemy.x ~= nil, or even whether localToContent exists before calling it.

Thanks for this info. Can you please tell me how to look if the localToContent exists? How is this looking in code?

Try

[lua]

if enemy.localToContent ~= nil

[/lua]

correct me if I am wrong but isnt…

if enemy and enemy~=nil

the same as saying…

if enemy==true and enemy~=false

?

it would work but its placing the same condition twice.

The error means the enemy display object was deleted, but you are still trying to operate on it.

You can check whether a display object is valid as follows:

local function isValid( obj ) return ( (obj ~= nil) and (type(obj.removeSelf) == "function") ) end

Thank you very much for the fast help!

There may be some cases where you could still get an error.  This will be due to the order in which you operate on an enemy in a particular frame. 

If you delete an enemy in the same frame that you later try to operate on it, you could still have an issue.

So, I suggest you mark all enemies with a flag when deleting them and then check that flag before operating on the enemy.

-- Critical logic protection if( enemy.isDead ) then return end

when deleting do this:

enemy.isDead = true .. other work you might do before deletion display.remove( enemy )

Just to verify:

if enemy then

If “enemy” is not nil, it will result to true.

if enemy ~= nil then

is also saying “if ‘enemy’ is not nil” so it will also result to true. You’re not testing a different condition. As @roaminggmaer posted, likely want you were wanting to test is:

if enemy and type( enemy.localToContent ) == "function" then

Rob

Thanks for all your help here! This really helps a LOT!

I may be wrong but I seem to remember objects can sometimes be in a state where they still technically exist (i.e. are not nil), but the things that make them a display object have been deleted.

You could try checking whether enemy.x ~= nil, or even whether localToContent exists before calling it.

Thanks for this info. Can you please tell me how to look if the localToContent exists? How is this looking in code?

Try

[lua]

if enemy.localToContent ~= nil

[/lua]

correct me if I am wrong but isnt…

if enemy and enemy~=nil

the same as saying…

if enemy==true and enemy~=false

?

it would work but its placing the same condition twice.

The error means the enemy display object was deleted, but you are still trying to operate on it.

You can check whether a display object is valid as follows:

local function isValid( obj ) return ( (obj ~= nil) and (type(obj.removeSelf) == "function") ) end

Thank you very much for the fast help!

There may be some cases where you could still get an error.  This will be due to the order in which you operate on an enemy in a particular frame. 

If you delete an enemy in the same frame that you later try to operate on it, you could still have an issue.

So, I suggest you mark all enemies with a flag when deleting them and then check that flag before operating on the enemy.

-- Critical logic protection if( enemy.isDead ) then return end

when deleting do this:

enemy.isDead = true .. other work you might do before deletion display.remove( enemy )

Just to verify:

if enemy then

If “enemy” is not nil, it will result to true.

if enemy ~= nil then

is also saying “if ‘enemy’ is not nil” so it will also result to true. You’re not testing a different condition. As @roaminggmaer posted, likely want you were wanting to test is:

if enemy and type( enemy.localToContent ) == "function" then

Rob

Thanks for all your help here! This really helps a LOT!