Problems using timers to make proximity fuzes
I'm encountering problems during my first forays into Lua code. I don't know much about it though I'm pretty experienced with .ini scripting.
I'm trying to make a shell for an anti-aircraft gun that detonates when it comes close to an actor. Most of the code I've used has been cannibalised from other people's mods because I don't understand how to construct it well myself yet.
Making a shell detonate when it is X distance away from an actor is quite easy, but I want to make one which will detonate after a short (preferrably random but I don't know how to do this) delay once it has come near an actor and become 'activated' - this way the incoming shells do not all detonate at the same distance from the dropship they're being fired at. Adding this delay and adding an element of randomness are the things I'm having trouble with, and here's the code I have so far:
Code:
function Create(self)
self.detonationTimer = Timer();
self.checkInterval = 50;
end
function Create(self)
self.delayTimer = Timer();
self.checkInterval = 25;
end
function Update(self)
if self.detonationTimer:IsPastSimMS(self.checkInterval) then
for actor in MovableMan.Actors do
if actor.ID ~= self.ID then
local avgx = actor.Pos.X - self.Pos.X;
local avgy = actor.Pos.Y - self.Pos.Y;
local dist = math.sqrt(avgx ^ 2 + avgy ^ 2);
if dist < 125 then
self.delayTimer:StartSimTimeMS();
end
end
end
self.detonationTimer:Reset();
end
end
function Update(self)
if self.delayTimer:IsPastSimMS(200) then
self:GibThis();
end
end
self.delayTimer:Reset();
end
end
Can any of you wise Lua people help me with this? The CC Wiki isn't very helpful or complete with regard to Lua coding...