How to optimize this code to the best performance ?
4 replies, posted
Hello all !!!
How can i optimize this code because it make players randomly crash and when i desactivate, no players crash ..
function ENT:Think()
for i = 1, 10 do
local part = self.Emit:Add(self.Material, self:GetPos())
if part then
part:SetVelocity(Vector(math.random(-180, 180), math.random(-180, 180), 90):GetNormal() * 80)
part:SetStartAlpha(100)
part:SetEndAlpha(100)
part:SetDieTime(15)
part:SetLifeTime(0)
part:SetStartSize(1)
part:SetEndSize(1)
part:SetRoll(math.random(0, 360))
part:SetRollDelta(math.random(-10, 10))
part:SetAirResistance( 100 )
part:SetGravity(Vector(0, 0, -10))
end
end
--self.Emit:Finish() --Seems to cause a RunTime Error(Pure Virtual Function Call or something)
if part then
part:SetNextThink(CurTime() + 1)
end
end
function ENT:OnRemove()
if self.Emit then
self.Emit:Finish()
--self.Emit = nil
end
end
you're adding 10 particles every single think... space out your particles longer
if i change part:SetNextThink(CurTime() + 1) to part:SetNextThink(CurTime() + 30) ?
It's correct ?
I'd more comfortable with timers, rather than ticks & thinks. This is the way you wanna go.
Or even change 0.5 to 0.2
timer.Create( "AWAM.ParticleCreationThingy", 0.5, 10, function()
local part = self.Emit:Add(self.Material, self:GetPos())
if part then
part:SetVelocity(Vector(math.random(-180, 180), math.random(-180, 180), 90):GetNormal() * 80)
part:SetStartAlpha(100)
part:SetEndAlpha(100)
part:SetDieTime(15)
part:SetLifeTime(0)
part:SetStartSize(1)
part:SetEndSize(1)
part:SetRoll(math.random(0, 360))
part:SetRollDelta(math.random(-10, 10))
part:SetAirResistance( 100 )
part:SetGravity(Vector(0, 0, -10))
end
end)
I wouldn't use timers, but your `if part` code isn't running since the `local part` is in another chunk of code. you can probably remove `if part then` all together.
Sorry, you need to Log In to post a reply to this thread.