• Correct way to make a rocket explode on impact
    6 replies, posted
I recently wrote a dumb simple rocket launcher SWEP. But when the projectile explodes I get the message "Changing collision rules within a callback is likely to cause crashes!" Here's the explody-bits code:     function ENT:Explode()         local pos = self.Entity:GetPos()                  self.Entity:EmitSound(explodeSounds[math.random(#explodeSounds)])                  util.BlastDamage( self.Entity, self.Owner, pos, self.radius, self.damage )                  local effectdata = EffectData()         effectdata:SetOrigin( pos )         util.Effect( "Explosion", effectdata, true, true )                  self.Entity:Remove()     end     function ENT:Touch( entity )         if (entity:IsPlayer() or entity:IsVehicle()) then             self:Explode()         end     end     function ENT:PhysicsCollide()         self:Explode()     end My first guess is that it's taking issue with the place that self.Entity:Remove() is being called from. But seriously, what's the correct way to write a SENT that explodes on impact?
Make self:Explode() in timer.Simple(0,function() ... end)
Man, I'd thought of that but it's so damn inelegant! That kind of solution seriously offends me, you know?
Maybe you could do self.explode = true inside the collision hook, then see if true in the next ENT:Think and then remove the entity there.
This is what I've said. Timer for 0 second runs in next tick
Or you could also use SafeRemoveEntityDelayed
Btw, depending on the rocket type, it doesn't always explode on impact, it can explode when the distance from it's target is close enough.
Sorry, you need to Log In to post a reply to this thread.