Data Realms Fan Forums http://45.55.195.193/ |
|
Problems using timers to make proximity fuzes http://45.55.195.193/viewtopic.php?f=73&t=19390 |
Page 1 of 1 |
Author: | Entropy [ Sat Jul 31, 2010 6:26 pm ] |
Post subject: | 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... |
Author: | Petethegoat [ Sat Jul 31, 2010 6:46 pm ] |
Post subject: | 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. |
Author: | Grif [ Sat Jul 31, 2010 7:40 pm ] |
Post subject: | 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 |
Author: | Petethegoat [ Sat Jul 31, 2010 7:44 pm ] |
Post subject: | 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. |
Author: | Entropy [ Sat Jul 31, 2010 8:56 pm ] |
Post subject: | 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. |
Author: | Petethegoat [ Sat Jul 31, 2010 9:25 pm ] |
Post subject: | 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. |
Page 1 of 1 | All times are UTC [ DST ] |
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group http://www.phpbb.com/ |