Pre Collision event.contact == nil

I’m working on a game where I have a platform where a character is allowed to pass through depending on the type of platform. So I have used pre collision to see if the contact should happen. However every once in a while, and this is rare so its hard to replicate but every once in a while it seems that event.contact == nil, I have even tried adding in a check to see if event.contact at the beginning but once in a while I keep getting errors. 

local function passHit( self, event ) local other = event.other if event.contact then if self.data.side == "bottom" then if self.y + 24 \> other.y then event.contact.isEnabled = false end elseif self.data.side == "top" then if self.y - 24 \< other.y then event.contact.isEnabled = false end elseif self.data.side == "left" then if self.x - 24 \< other.x then event.contact.isEnabled = false end else if self.x + 24 \> other.x then event.contact.isEnabled = false end end end end

Hi @lofy,

Considering the inconsistency, could this be caused by the checking of exact numbers (+24, -24, etc.)? I don’t know the exact sizes of the objects you’re working with, but remember that collisions rarely (probably never) occur at the exact same position… there will always be a tiny fractional bit of variation caused by mathematical calculations in the physics engine.

Brent

I’m sorry maybe I wasn’t clear, theres not an issue with the actual gameplay. Its just that sometimes I’ll get a runtime error that spits out that event.contact was nil and therefore I can’t set event.contact.isEnabled = false. I just don’t understand how the event in a preCollision event can be nil. 

I’m not 100% sure why this happens, except that pre-collision events literally generate many (dozens of) responses within the span of a few millseconds.

So is there some way to handle these nil events, so that it doesn’t cause a runtime error within my game?

How is it giving you a Runtime error? Doesn’t the line “if event.contact then” make those rare instances of a nil event.contact get ignored?

So I’ve finally been able to replicate it every once in a while. For some reason the event exists, but the contact within the event is nil. I was still getting the error when I was just using a check for event, and since I couldn’t replicate it I wasn’t sure if adding the check for event.contact actually would solve the problem, but it does. Interestingly as well, the x,y location of this event when the .contact == nil is 0,0. 

Interesting observations. Again, I’m not sure exactly why it happens in rare cases, just that there’s a considerable number of things happening in a preCollision event all within literally a blink of an eye time span. If your conditional check helps filter out those rare cases, I would advise using it. :slight_smile:

Hi @lofy,

Considering the inconsistency, could this be caused by the checking of exact numbers (+24, -24, etc.)? I don’t know the exact sizes of the objects you’re working with, but remember that collisions rarely (probably never) occur at the exact same position… there will always be a tiny fractional bit of variation caused by mathematical calculations in the physics engine.

Brent

I’m sorry maybe I wasn’t clear, theres not an issue with the actual gameplay. Its just that sometimes I’ll get a runtime error that spits out that event.contact was nil and therefore I can’t set event.contact.isEnabled = false. I just don’t understand how the event in a preCollision event can be nil. 

I’m not 100% sure why this happens, except that pre-collision events literally generate many (dozens of) responses within the span of a few millseconds.

So is there some way to handle these nil events, so that it doesn’t cause a runtime error within my game?

How is it giving you a Runtime error? Doesn’t the line “if event.contact then” make those rare instances of a nil event.contact get ignored?

So I’ve finally been able to replicate it every once in a while. For some reason the event exists, but the contact within the event is nil. I was still getting the error when I was just using a check for event, and since I couldn’t replicate it I wasn’t sure if adding the check for event.contact actually would solve the problem, but it does. Interestingly as well, the x,y location of this event when the .contact == nil is 0,0. 

Interesting observations. Again, I’m not sure exactly why it happens in rare cases, just that there’s a considerable number of things happening in a preCollision event all within literally a blink of an eye time span. If your conditional check helps filter out those rare cases, I would advise using it. :slight_smile: