1

我在我的 lua reload 部分有这个设置,我很想为它设置一个冷却功能,就像初级火力和二级火力一样。有没有办法做到这一点?这是我的代码。

function SWEP:Reload()
    if Chaos == 0 then
        Chaos = 1
        self.Owner:SetModel("models/_tails_ models/characters/sonic heroes/super_sonic/supersonic.mdl")
        self.Weapon:EmitSound( "weapons/now.wav" )
    elseif Chaos == 1 then
        Chaos = 0
        self.Owner:SetModel("models/_tails_ models/characters/sonic heroes/sonic/sonic.mdl")
    end
end
4

1 回答 1

3

os.time()应该做的伎俩。您可以查看 Lua 网站上的文档

仅在某个时间后才允许某些事情发生背后的逻辑是检查自上次使用该函数以来经过的时间。从逻辑上讲,它会是——

timeElapsed = lastTimeOfUse - timeNow

如果timeElapsed > cooldownPeriod然后允许事件发生并设置lastTimeOfUse = timeNow.


如果您的意思是重新加载功能仅在60秒 (将其更改为任何内容)后才起作用,请尝试以下操作:-

-- Settings
cooldown = 60 -- Cooldown period in Seconds

-- Reload function with cooldown
local lastReloadTime=0;
function SWEP:Reload()
    if ((os.time()-lastReloadTime)>cooldown) then -- Allows only after cooldown time is over
        if Chaos == 0 then
            Chaos = 1
            self.Owner:SetModel("models/_tails_ models/characters/sonic heroes/super_sonic/supersonic.mdl")
            self.Weapon:EmitSound( "weapons/now.wav" )
        elseif Chaos == 1 then
            Chaos = 0
            self.Owner:SetModel("models/_tails_ models/characters/sonic heroes/sonic/sonic.mdl")
        end
        lastReloadTime=os.time() -- Sets this time as last using time of Reload
    end
end

根据您的评论,如果您想将声音循环到特定时间,这样的事情应该可以工作

 -- Settings
durationOfPlayback = 3 -- for how long you want to play the sound in seconds

-- Specifications
durationOfSoundFile = 1 -- length of sound file in seconds

-- Sound playback for a specific time cooldown
noOfTimesToPlay = math.floor(durationOfPlayback/durationOfSoundFile)
function SWEP:Reload()
    ...
    for i = 1, noOfTimesToPlay do
    {
            self.Weapon:EmitSound( "weapons/now.wav" )
            lastSoundTime=os.time()

            --This line will make the loop wait till 1 playback is complete
            while((os.time()-lastSoundTime)<durationOfSoundFile) do end
    }
    ...
end
于 2014-09-02T21:02:07.137 回答