Garry's Mod Lua: 如何制作延迟/冷却?

我已经将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
点赞
用户12307227
用户12307227
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函数,只需复制内容并添加到您现有的函数中。

2020-04-28 00:11:43