View unanswered posts | View active topics It is currently Fri Dec 27, 2024 8:58 am



Reply to topic  [ 6 posts ] 
 Problems using timers to make proximity fuzes 
Author Message
User avatar

Joined: Fri Jan 26, 2007 8:27 pm
Posts: 40
Location: England
Reply with quote
Post 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...


Sat Jul 31, 2010 6:26 pm
Profile
User avatar

Joined: Mon Jun 15, 2009 4:02 pm
Posts: 905
Reply with quote
Post Re: Problems using timers to make proximity fuzes
Uhh, why do you have two function Creates and two function Updates?
Also, you have about fifty too many ends.
I'll edit this post with the fixed script in a bit.

Edit:
Code:
function Create(self)
   math.randomseed(self.Age);
   self.checkInterval = math.random(25,50);
end

function Update(self)
   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.detonationTimer = Timer();
            if self.detonationTimer:IsPastSimMS(self.checkInterval) then
               self:GibThis();
            end
         end
      end
   end
end

I'm fairly confident that this should work, but I don't have enough time to comment it for you, so sorry about the lack of comments.


Sat Jul 31, 2010 6:46 pm
Profile WWW
REAL AMERICAN HERO
User avatar

Joined: Sat Jan 27, 2007 10:25 pm
Posts: 5655
Reply with quote
Post Re: Problems using timers to make proximity fuzes
that oughta work, pete, though it'll still blow up in the user's face some of the time

self.ID on a fired projectile isn't going to be any use at all


Sat Jul 31, 2010 7:40 pm
Profile
User avatar

Joined: Mon Jun 15, 2009 4:02 pm
Posts: 905
Reply with quote
Post Re: Problems using timers to make proximity fuzes
Can you set the team on a MOSParticle or A MOSRotating?
If not, I guess you just have another timer so that it waits for a couple of ms before being able to engage. Or just look at self.Age.


Sat Jul 31, 2010 7:44 pm
Profile WWW
User avatar

Joined: Fri Jan 26, 2007 8:27 pm
Posts: 40
Location: England
Reply with quote
Post Re: Problems using timers to make proximity fuzes
Thanks for your help Pete, that looks much cleaner. As I said, I'm not familiar with Lua syntax or most of the functions so I was guessing what I was doing. I'll try this out and see what the effect is.

Oh and it won't blow up in my face; I've tested this already with simpler versions of the idea. The weapon it's fired from has such a long barrel the projectile starts quite a distance away from the actor.


Sat Jul 31, 2010 8:56 pm
Profile
User avatar

Joined: Mon Jun 15, 2009 4:02 pm
Posts: 905
Reply with quote
Post Re: Problems using timers to make proximity fuzes
Syntax viewtopic.php?f=73&t=14737
Functions viewtopic.php?f=73&t=14636 (scroll down)

Also, if you want to change the randomness, change the two numbers in math.random(25,50).
A number between them will be generated.
That might be obvious to you, or it might not, but there you go either way.


Sat Jul 31, 2010 9:25 pm
Profile WWW
Display posts from previous:  Sort by  
Reply to topic   [ 6 posts ] 

Who is online

Users browsing this forum: No registered users


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by STSoftware for PTF.
[ Time : 0.079s | 13 Queries | GZIP : Off ]