Garry的Mod Lua:如何进行延迟/冷却?

时间:2020-04-19 19:51:16

标签: lua garrys-mod

我将IN_USE设置为主要攻击方式,而不是故意设置SWEP:PrimaryAttack。但是这样做,使它成为了我可以进行垃圾邮件攻击的地方,因此,我正在寻找延迟/冷却的方法。我环顾了CurTime和其他事物;但是,我已经有一个IF then Else语句,并且不确定如何在其中使用CurTime编码。

function SWEP:Think()
    if self.Owner:KeyDown(IN_USE) && self.Owner:IsPlayer() then
        local Angles = self.Owner:GetAngles()

        self:SendAnim()   
        self:SetWeaponHoldType( "melee" )
        timer.Simple(0.1, function() 
            if not IsValid(self) or not self.Owner:Alive() then return end self.Weapon:EmitSound( "weapons/iceaxe/iceaxe_swing1.wav" ) self.Weapon:PrimarySlash() self.Owner:SetAnimation( PLAYER_ATTACK1 ) end )
        timer.Simple(0.35, function() 
            if not IsValid(self) or not self.Owner:Alive() then return end self.Weapon:EmitSound( "weapons/iceaxe/iceaxe_swing1.wav" ) self.Weapon:PrimarySlash() end)
        timer.Simple(0.5, function() if not IsValid(self) or not self.Owner:Alive() then return end self:SetWeaponHoldType( "knife" ) end)
    end

1 个答案:

答案 0 :(得分:0)

function SWEP:Initialize()
    self.NextUseTime = CurTime()
    self.UseDelay = 1.5
end

function SWEP:Think()
    if self.Owner:KeyDown(IN_USE) && self.Owner:IsPlayer() && ( self.NextUseTime - CurTime() <= 0 ) then
        local Angles = self.Owner:GetAngles()

        self:SendAnim()   
        self:SetWeaponHoldType( "melee" )
        timer.Simple(0.1, function() 
            if not IsValid(self) or not self.Owner:Alive() then return end self.Weapon:EmitSound( "weapons/iceaxe/iceaxe_swing1.wav" ) self.Weapon:PrimarySlash() self.Owner:SetAnimation( PLAYER_ATTACK1 ) end )
        timer.Simple(0.35, function() 
            if not IsValid(self) or not self.Owner:Alive() then return end self.Weapon:EmitSound( "weapons/iceaxe/iceaxe_swing1.wav" ) self.Weapon:PrimarySlash() end)
        timer.Simple(0.5, function() if not IsValid(self) or not self.Owner:Alive() then return end self:SetWeaponHoldType( "knife" ) end)

        self.NextUseTime = CurTime() + self.UseDelay
    end

如果您已经具有SWEP:Initialize函数,只需复制内容并将其添加到现有函数中即可。