Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

Two events happening at the same time - "a nil value" error
Started by cbriggsnz Jun 10 2016 01:13 PM

- - - - -
1 reply to this topic
applylinearimpulse physics a nil value

Best Answer roaminggamer , 10 June 2016 - 02:21 PM

The object stays around one cycle longer than the body, so check directly for a physics function

if( event.other.removeSelf == nil or event.other.applyLinearImpulse == nil ) then return end

-- this part is not needed: not event.other

[TOPIC CONTROLS]
This topic has been archived. This means that you cannot reply to this topic.
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

cbriggsnz

[GLOBAL: userInfoPane.html]
cbriggsnz
  • Contributor

  • 283 posts
  • Corona SDK

Hi all,

 

I have 8 grenades grouped together in my game, when one explodes it pushes the other grenades using an applyLinearImpulse, which also sets of the other grenades around it, creating a chain reaction. Grenades remove themselves when the explosion happens.

 

This seems to work 9 out of 10 times.  On the 10th time I get this error "attempt to call method 'applyLinearImpulse' (a nil value)".  I thought I would have handled this error by checking if the object still exists before applying the impulse, but no.  

 

I think the problem is a very specific timing issue (I think).

 

Happening same cycle??

Grenade 1 - checks grenade 2 - returns OK

Grenade 2 - removes itself

 

Then (next cycle) Grenade 1 applies linear impulse on Grenade 2 - doesn't exist anymore - error

 

 

Here is the offending code, I hope it enough for someone to give me an idea.

 

pushCircle.collision = function (target, event)
if ( event.phase == "began") then
if not event.other or event.other.removeSelf == nil then return end
local tempCor = math2d.sub(event.target, event.other)
local distance = math2d.length(tempCor.x, tempCor.y)
local angle = math2d.vector2Angle(tempCor.x, tempCor.y)
local vecX, vecY = math2d.angle2Vector( angle )
 
timer.performWithDelay( 1, function() -- happening inside collision so add 1 millisecond delay
if not event.other or event.other.removeSelf == nil then return end
event.other.bodyType = "dynamic"
print("event.other.id", event.other.id)
event.other:applyLinearImpulse( -vecX * 100, -vecY * 100, event.other.x, event.other.y )
return true
end, "explosionTimer")
 
end
 
end
pushCircle:addEventListener( "collision" )

 



[TOPIC: post.html]
#2

roaminggamer

[GLOBAL: userInfoPane.html]
roaminggamer
  • Corona Geek

  • 7,617 posts
  • Corona SDK

  Best Answer

The object stays around one cycle longer than the body, so check directly for a physics function

if( event.other.removeSelf == nil or event.other.applyLinearImpulse == nil ) then return end

-- this part is not needed: not event.other



[topic_controls]
 
[/topic_controls]