简体   繁体   English

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

[英]Garry's Mod Lua: How to make delay/cooldown?

I have IN_USE set up as my primary attack instead of SWEP:PrimaryAttack on purpose.我故意将 IN_USE 设置为我的主要攻击,而不是 SWEP:PrimaryAttack。 But doing that, it has made it where I can spam attack and So i'm looking for a delay/cooldown to it.但是这样做,它使我可以进行垃圾邮件攻击,所以我正在寻找它的延迟/冷却时间。 I've looked around at CurTime and things;我环顾了 CurTime 和其他东西; however, I already have an IF then Else statement and unsure how to use CurTime coding into it.但是,我已经有一个 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
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

In case you already have a SWEP:Initialize function, just copy the contents across and add it to your existing function.如果您已经有一个 SWEP:Initialize function,只需复制内容并将其添加到您现有的 function。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM