Delays and Cooldowns

From Garry's Mod
(Difference between revisions)
Jump to: navigation, search
(Better examples)
 
(8 intermediate revisions by 2 users not shown)
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.
+
= About =
= Creating an event cooldown =
+
A cooldown will make an event only trigger if a certain amount of time has passed since its last occurrence.
 +
 
 
An event cooldown can be created in several ways, and it is made by setting a condition based on time
 
An event cooldown can be created in several ways, and it is made by setting a condition based on time
 
<pre>function myFunc()
 
<pre>function myFunc()
Line 7: Line 8:
 
end
 
end
 
end</pre>
 
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.
  
Line 17: Line 18:
 
if timeElapsed < delay then -- If the time elapsed since the last occurance is less than 2 seconds
 
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( "The event is on cooldown and has not been triggered" )
print( "Please wait another " .. math.Round( delay - timeElapsed ) .. " second(s)" )
 
 
else
 
else
 
print( "The event has been triggered!" )
 
print( "The event has been triggered!" )
Line 25: Line 25:
  
 
{{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}}
 +
 +
  
 
In the above example, we use {{GlobalFunction|CurTime}} to tell when the last event had occured. Instead, we could use it to determine when the '''next''' event should occur
 
In the above example, we use {{GlobalFunction|CurTime}} to tell when the last event had occured. Instead, we could use it to determine when the '''next''' event should occur
Line 35: Line 37:
 
print( "The event has been triggered!" )
 
print( "The event has been triggered!" )
 
nextOccurance = CurTime() + delay
 
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>
  
== Timers ==
+
= Timers =
 
Another method of setting a delay is using the {{Lib|timer}}.
 
Another method of setting a delay is using the {{Lib|timer}}.
 
<pre>local delay = 2
 
<pre>local delay = 2
Line 55: Line 54:
 
end
 
end
 
end</pre>
 
end</pre>
 +
 +
{{Note|If you would like to know how much time is left until the timer ends, use {{LibraryFunction|timer|Create|}}}}
 +
 +
{{Note|It is recommended to not use timers inside hooks that run every frame/tick such as {{HookFunction|GM|Think}} and {{HookFunction|GM|HUDPaint}}}}
 +
 +
= Real World Time =
 +
If you're dealing with long cooldowns, that should persist between map changes and even server restarts, you'll need to use real world time to set the cooldown. We can use real world time with {{LibraryFunction|os|time}} and {{LibraryFunction|os|date}}
 +
 +
There are different ways to save information between map changes and server restarts. In the following example I'll be using the {{Lib|cookie}} to set a 24-hour cooldown on my function
 +
<pre>local cooldown = 86400 -- 24 hours in seconds (24*60*60)
 +
 +
local function myFunc()
 +
local nextUse = cookie.GetNumber( "myFuncNextUse", 0 )
 +
local time = os.time()
 +
 +
if time < nextUse then
 +
print( "The event is on cooldown and has not been triggered" )
 +
local nextUseString = os.date( "%Y/%m/%d - %H:%M:%S" , nextUse ) -- Format the next use time nicely
 +
print( "The event will be available for use again on: " .. nextUseString )
 +
else
 +
print( "The event has been successfully triggered!" )
 +
cookie.Set( "myFuncNextUse", time + cooldown )
 +
end
 +
end</pre>
 +
 +
[[Category:Modding Tutorials]]
 +
[[Category:Lua Tutorials]]

Latest revision as of 16:59, 21 May 2019

Contents

About

A cooldown will make an event only trigger if a certain amount of time has passed since its last occurrence.

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" )
	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
	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
NOTE

If you would like to know how much time is left until the timer ends, use timer.Create

NOTE

It is recommended to not use timers inside hooks that run every frame/tick such as GM:Think and GM:HUDPaint

Real World Time

If you're dealing with long cooldowns, that should persist between map changes and even server restarts, you'll need to use real world time to set the cooldown. We can use real world time with os.time and os.date

There are different ways to save information between map changes and server restarts. In the following example I'll be using the cookie library to set a 24-hour cooldown on my function

local cooldown = 86400 -- 24 hours in seconds (24*60*60)

local function myFunc()
	local nextUse = cookie.GetNumber( "myFuncNextUse", 0 )
	local time = os.time()

	if time < nextUse then
		print( "The event is on cooldown and has not been triggered" )
		local nextUseString = os.date( "%Y/%m/%d - %H:%M:%S" , nextUse ) -- Format the next use time nicely
		print( "The event will be available for use again on: " .. nextUseString )
	else
		print( "The event has been successfully triggered!" )
		cookie.Set( "myFuncNextUse", time + cooldown )
	end
end
Personal tools
Navigation