Delays and Cooldowns

From Garry's Mod
(Difference between revisions)
Jump to: navigation, search
(Added some timer stuff)
(Better examples)
Line 1: Line 1:
 
A delay (or cooldown) is a way to make an event only trigger if a certain amount of time has passed since its last occurrence.
 
A delay (or cooldown) is a way to make an event only trigger if a certain amount of time has passed since its last occurrence.
 
+
= Creating an event cooldown =
 +
An event cooldown can be created in several ways, and it is made by setting a condition based on time
 +
<pre>function myFunc()
 +
if funcNotOnCooldown then
 +
-- Do stuff
 +
end
 +
end</pre>
 
== CurTime ==
 
== CurTime ==
 
{{GlobalFunction|CurTime}} is a useful tool for setting a delay for an event. The function returns the uptime of the server in seconds, which means we can use it to keep track of time elapsed by saving the returned value, then calling it again.
 
{{GlobalFunction|CurTime}} is a useful tool for setting a delay for an event. The function returns the uptime of the server in seconds, which means we can use it to keep track of time elapsed by saving the returned value, then calling it again.
  
Here's an example of an anti chat spam system made using CurTime
+
Here's an example of a cooldown made using CurTime
 
<pre>local delay = 2
 
<pre>local delay = 2
hook.Add( "PlayerSay", "CheckForAntiSpam", function( ply, text )
+
local lastOccurance = -delay -- Ensure the first trigger attempt will work
if not ply.lastChatMessage then ply.lastChatMessage = -delay end -- This is to ensure the first time trigger works properly
+
local function myFunc()
if CurTime() - ply.lastChatMessage < delay then -- If the time passed since the last message sent is less than 2 seconds
+
local timeElapsed = CurTime() - lastOccurance
ply:ChatPrint( "You must wait " .. delay .. " seconds after sending a chat message to send another one" ) -- Let the player know he can't send the message
+
if timeElapsed < delay then -- If the time elapsed since the last occurance is less than 2 seconds
return "" -- Deny the message
+
print( "The event is on cooldown and has not been triggered" )
else -- If it's been more than 2 seconds since the last message was sent
+
print( "Please wait another " .. math.Round( delay - timeElapsed ) .. " second(s)" )
ply.lastChatMessage = CurTime() -- Update lastChatMessage
+
else
 +
print( "The event has been triggered!" )
 +
lastOccurance = CurTime()
 
end
 
end
end )</pre>
+
end</pre>
  
 
{{Note|If your event isn't related to in-game events; consider using {{GlobalFunction|RealTime}} instead, which will always be synced to real-world time rather than server time}}
 
{{Note|If your event isn't related to in-game events; consider using {{GlobalFunction|RealTime}} instead, which will always be synced to real-world time rather than server time}}
Line 21: Line 29:
 
<pre>local delay = 5
 
<pre>local delay = 5
 
local nextOccurance = 0
 
local nextOccurance = 0
hook.Add( "Think", "CurTimeDelay", function()
+
 
if CurTime() > nextOccurance then -- If the time has passed the nextOccurance time
+
local function myFunc()
print( "This message will repeat every 5 seconds." )
+
local timeLeft = nextOccurance - CurTime()
nextOccurance = CurTime() + delay  
+
if timeLeft < 0 then -- If the time has passed the nextOccurance time
 +
print( "The event has been triggered!" )
 +
nextOccurance = CurTime() + delay
 +
else
 +
print( "The event is on cooldown and has not been triggered" )
 +
print( "Please wait " .. math.Round( timeLeft ) .. " second(s)" )
 
end
 
end
 
end )</pre>
 
end )</pre>
Line 32: Line 45:
 
<pre>local delay = 2
 
<pre>local delay = 2
 
local shouldOccur = true
 
local shouldOccur = true
hook.Add( "PlayerSay", "CheckForAntiSpam", function( ply, text )
+
 
 +
local function myFunc()
 
if shouldOccur then
 
if shouldOccur then
 +
print( "The event has been triggered!" )
 
shouldOccur = false
 
shouldOccur = false
 
timer.Simple( delay, function() shouldOccur = true end )
 
timer.Simple( delay, function() shouldOccur = true end )
 
else
 
else
return ""
+
print( "The event is still on cooldown" )
 
end
 
end
end )</pre>
+
end</pre>

Revision as of 15:45, 17 May 2017

A delay (or cooldown) is a way to make an event only trigger if a certain amount of time has passed since its last occurrence.

Creating an event cooldown

An event cooldown can be created in several ways, and it is made by setting a condition based on time

function myFunc()
	if funcNotOnCooldown then
		-- Do stuff
	end
end

CurTime

CurTime is a useful tool for setting a delay for an event. The function returns the uptime of the server in seconds, which means we can use it to keep track of time elapsed by saving the returned value, then calling it again.

Here's an example of a cooldown made using CurTime

local delay = 2
local lastOccurance = -delay -- Ensure the first trigger attempt will work
local function myFunc()
	local timeElapsed = CurTime() - lastOccurance
	if timeElapsed < delay then -- If the time elapsed since the last occurance is less than 2 seconds
		print( "The event is on cooldown and has not been triggered" )
		print( "Please wait another " .. math.Round( delay - timeElapsed ) .. " second(s)" )
	else
		print( "The event has been triggered!" )
		lastOccurance = CurTime()
	end
end
NOTE

If your event isn't related to in-game events; consider using RealTime instead, which will always be synced to real-world time rather than server time

In the above example, we use CurTime to tell when the last event had occured. Instead, we could use it to determine when the next event should occur

local delay = 5
local nextOccurance = 0

local function myFunc()
	local timeLeft = nextOccurance - CurTime()
	if timeLeft < 0 then -- If the time has passed the nextOccurance time
		print( "The event has been triggered!" )
		nextOccurance = CurTime() + delay
	else
		print( "The event is on cooldown and has not been triggered" )
		print( "Please wait " .. math.Round( timeLeft ) .. " second(s)" )
	end
end )

Timers

Another method of setting a delay is using the timer library.

local delay = 2
local shouldOccur = true

local function myFunc()
	if shouldOccur then
		print( "The event has been triggered!" )
		shouldOccur = false
		timer.Simple( delay, function() shouldOccur = true end )
	else
		print( "The event is still on cooldown" )
	end
end
Personal tools
Navigation