lua - 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
解决方案
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 函数,只需复制其中的内容并将其添加到您现有的函数中。
推荐阅读
- angular - 来自服务的响应不会显示在 Angular 应用程序上,但会显示在 console.log 中
- java - 布局输出错误:: RecyclerView: No Adapter attach; 跳过布局
- flutter - Flutter Navigator:断言失败... !_debugLocked 在 Navigator.pushNamed() 上不正确
- timestamp - 工作表excel中的自动时间戳问题
- html - 使用 Bootstrap(使用 React)基于父 DOM 的最小宽度/高度创建方形 Flexbox
- macos - 在终端中设置 Flutter 会产生一个 command not found 消息
- node.js - electron-builder生成的app镜像怎么知道点击后要运行什么?
- python - 我如何让 .configure 在 for 循环中工作
- vue.js - Toast UI 中的自定义渲染器
- powershell - ADO - 在发布任务中传递变量的问题