Finding if a gun shoots, without using self:IsActivated()
Author
Message
Kettenkrad
Joined: Mon Oct 25, 2010 5:51 am Posts: 1198 Location: Sydney
Finding if a gun shoots, without using self:IsActivated()
Hello, brilliant, talented, wondrous .lua scripters of DRLFF. I seek guidance in the ways of .lua.
So, what I've got is a gun that sort of overheats, thanks to snippets of Asklar and Coops's code. In order to efficiently achieve the desired effect, I need to find when the gun (emits? creates? the particle) shoots. self.IsActivated() has failed me, as the fire command can be held down for many, many frames. If any of you could point me in the right direction I'd love to go back and tinker with the script.
EDIT: And if possible I would prefer the gun to retain it's unlimited ammo.
I have tried: (In no particular order) - Using a 'break' (failed miserably) - Finding if the bullet itself is created and within range (partially worked) - Using IsActivated() (no dice) - Giving the mag 1 round and checking if the gun reloads. (nope)
And various other methods. As you can clearly see, much of this script was made by me! I'm very proud of my horrible job.
Code:
-- Thanks to Asklar for the meter and Coops for a few things.
for parent in MovableMan.Actors do if SceneMan:ShortestDistance(self.Pos, parent.Pos , true).Magnitude < 45 then self.parent = ToAHuman(parent) else self.parent = nil end end
if self:IsActivated() then self.Shot = self.Shot + 45 self.coolDown = false else self.coolDown = true end
if self.Shot >= self.MaxShot then self:Deactivate() self.Shot = self.MaxShot self.Overheat = true end
if self.Overheat == true then self.Effect = CreateAEmitter("Overheat") self.Effect.Pos = self.Pos + Vector(4,-3) MovableMan:AddParticle(self.Effect) if self.Shot > 1 then self:Deactivate() self.coolDown = true else self.Overheat = false end end
if self.Shot <= 1 then self.coolDown = false end
if (self.coolDown == true and self.Shot > 1) then self.Shot = self.Shot - 1 end
function_update if self.roundcount < originalroundcount then <------fire checked overheat = overheat + originalroundcount - self.roundcount originalroundcount = self.roundcount cooldowntimer:Reset() elseif self.roundcount < originalroundcount then <------reload originalroundcount = self.roundcount end
if cooldowntimer:IsPastSimMS(cooldowntime) and overheat >= 0 then overheat = overheat - 1 <------cooldown end
if overheat >= overheatlimit then (make the gun unable to shoot) end
how about this
Thu Oct 06, 2011 12:41 pm
Kettenkrad
Joined: Mon Oct 25, 2010 5:51 am Posts: 1198 Location: Sydney
Re: Finding if a gun shoots, without using self:IsActivated()
Oh right, and the gun has infinite ammo, sorry.
Kidding, I'll give it a shot, thanks.
EDIT: I'd prefer if it retained it's unlimited ammo. Any way to get around this?
Last edited by Kettenkrad on Thu Oct 06, 2011 12:58 pm, edited 1 time in total.
Thu Oct 06, 2011 12:44 pm
Xery
Joined: Sat Feb 26, 2011 10:29 am Posts: 163
Re: Finding if a gun shoots, without using self:IsActivated()
Oh, and how about the self.parent:GetController():IsState(14)? Is it the same as IsActivated()?
Thu Oct 06, 2011 12:54 pm
Kettenkrad
Joined: Mon Oct 25, 2010 5:51 am Posts: 1198 Location: Sydney
Re: Finding if a gun shoots, without using self:IsActivated()
From what I've seen, most likely. I'll have a look.
EDIT: Nope, same effect.
Thu Oct 06, 2011 12:59 pm
CaveCricket48
Joined: Tue Jun 12, 2007 11:52 pm Posts: 13144 Location: Here
Re: Finding if a gun shoots, without using self:IsActivated()
You could just do complete Lua firing, instead of a hybrid between INI and Lua. That way, you'd have control over bullet spawning and you can use timers to make sure everything is synchornized nicely.
But, if you don't want to go full Lua, you could set your gun's Sharpness to 0 in INI. Then, have a script on the bullets that find the parent gun and change that sharpness when they're first created. Then you can just check the gun's sharpness if it fired a bullet or not and reset the Sharpness back to 0 after you're done doing your stuff.
Re: Finding if a gun shoots, without using self:IsActivated()
Look at my recoil script of the autocannon from the trapper mod, it uses the self.IsActivated(), but detects exactly when the particle is fired using timers and RateOfFire to find out when a shot is actually fired.
Here is the code stripped to its basics
Code:
function Create(self) self.ShootTimer = Timer() self.Clicked = false end function Update(self) if self:IsActivated() then if self.ShootTimer:IsPastSimMS(60000/self.RateOfFire) and self.Clicked then self.ShootTimer:Reset() -- add stuff here self.Clicked = false end else self.Clicked = true end end
Remove the "self.Clicked" part if the gun is full automatic
Thu Oct 06, 2011 7:29 pm
Kettenkrad
Joined: Mon Oct 25, 2010 5:51 am Posts: 1198 Location: Sydney
Re: Finding if a gun shoots, without using self:IsActivated()
Thank you both, I ended up using akblabla's method, it required less work on my part
Works wonderfully now.
Fri Oct 07, 2011 1:52 am
Azukki
Joined: Sat Nov 03, 2007 9:44 pm Posts: 1916 Location: Flint Hills
Re: Finding if a gun shoots, without using self:IsActivated()
Maybe it's too late for this to be useful to you, but maybe it will be for someone else sooner or later. I think it's a little simpler than the other methods shown.
I just check if self.Magazine.RoundCount has changed, using a variable to store the previous value. If it has changed, and it's not full, then it must have just been fired.
Code:
if self.Magazine then if self:IsReloading() then self.justfired = 0 else if self.magcount ~= self.Magazine.RoundCount and self.Magazine.IsFull == false then self.justfired = 1 else self.justfired = 0 end self.magcount = self.Magazine.RoundCount end else self.justfired = 0 end
Edit: It seems I poorly skimmed through the thread and have made a fool of myself, whoops.
Last edited by Azukki on Tue Oct 11, 2011 12:38 am, edited 1 time in total.
Tue Oct 11, 2011 12:25 am
CaveCricket48
Joined: Tue Jun 12, 2007 11:52 pm Posts: 13144 Location: Here
Re: Finding if a gun shoots, without using self:IsActivated()
Magazine RoundCount checking has already been suggested, but
Kettenkrad wrote:
EDIT: And if possible I would prefer the gun to retain it's unlimited ammo.
Tue Oct 11, 2011 12:35 am
robolee
Joined: Fri May 11, 2007 4:30 pm Posts: 1040 Location: England
Re: Finding if a gun shoots, without using self:IsActivated()
What I ended up using in my portal gun script (edited for your own use):
Code:
self.Parent = MovableMan:GetMOFromID(MovableMan:GetRootMOID(self.RootID)); if MovableMan:IsActor(self.Parent) then self.Parent = ToActor(self.Parent); if (not(self.wait)) then if self.Parent:GetController():IsState(Controller.PRESS_PRIMARY) then self.fire=true; end end end if (self.wait) and (self.timer:IsPastSimMS(500)) then --change timer to match rate of fire self.wait=false; end if (self.fire) then self.timer:Reset(); self.wait=true; self.fire=false; end
Basically there are two variables that control the shooting, self.fire and self.wait, both of them are initialized to false, so if the shoot button is pressed, self.fire is set to true and the firing code is run, then fire is set to false and wait is set to true, when wait is set the fire function cannot run until the timer has reached a threshold and it repeats. It's not the most accurate on timing but it's decent enough.
You will have to change Controller.PRESS_PRIMARY to Controller.PRIMARY_HELD or something like that, I can't remember.
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