Liam0102 / Star-Wars-Vehicles

Star Wars Vehicles addon for Garrys Mod (Code only)
3 stars 2 forks source link

Collision Rules Issue #15

Closed dhkatz closed 6 years ago

dhkatz commented 6 years ago

Current in the cannon_blast and torpedo_blast entities there is an issue with collision changing: https://github.com/Liam0102/Star-Wars-Vehicles/blob/008d2db4b347c2bcb554ce13dd494384b82eff94/lua/entities/cannon_blast.lua#L44-L60

In the PhysicsCollide function, removing the entitiy in the same frame in which it collided seems to cause issues.

This results in the following in-game error:

Changing collision rules within a callback is likely to cause crashes!

After reading up a bit, I think it may be wise to look into performing this collision change (the removal) in the next frame.

function ENT:PhysicsCollide(data, physobj)
    for i = 1, math.Round(self.Damage / 100) do
        local pos = self:GetPos() + self:GetForward() * math.random(-self.Damage / 2, self.Damage / 2) + self:GetRight() * math.random(-self.Damage / 2, self.Damage / 2)
        local fx = EffectData()
        fx:SetOrigin(pos)
        util.Effect("Explosion", fx, true, true)
    end

    for k, v in pairs(ents.FindInSphere(self:GetPos(), self.Damage)) do
        local dist = (self:GetPos() - v:GetPos()):Length()
        local dmg = math.Clamp((self.Damage or 600) - dist, 0, (self.Damage or 600))
        v:TakeDamage(dmg)
    end

    timer.Simple(0, function()
        if (IsValid(self)) then
            self:Remove()
        end
    end)
    --Remove the next frame..
end

This removes the in-game error. The same would need to be done in the torpedo_blast.lua PhysicsCollide as well.