timer.Create

From Garry's Mod
Jump to: navigation, search
 timer.Create( )

Contents

Description

Creates a new timer that will repeat its function given amount of times. This function also requires the timer to be named, which allows you to control it after it was created via the timer library.

For a simple one-time timer with no identifiers, see timer.Simple.

NOTE

Timers use CurTime for timing.

WARNING

Timers won't advance while the client is timing out from the server.

Arguments

string identifier

Identifier of the timer to create. Must be unique. If a timer already exists with the same identifier, that timer will be updated to the new settings and reset.

Arguments

number delay

The delay interval in seconds. If the delay is too small, the timer will fire on the next frame/tick.

Arguments

number repetitions

The number of times to repeat the timer. Enter 0 for infinite repetitions.

Arguments

function func

Function called when timer has finished the countdown.

Examples

Example

Creates a timer that has a 1 second delay and is only ran once ('UniqueName1'), a timer that has a 2 second delay and is ran continuously ('UniqueName2'), etc.

This shows the different ways you can interact with functions.

local function PrintSomething( text )
	print( text )
end

local function PrintNoArguments()
	print( "fun with timers!" )
end

local function CreateSomeTimers( )
	timer.Create( "UniqueName1", 1, 1, function() print("inside") end )
	timer.Create( "UniqueName2", 2, 0, function() PrintSomething("outside") end )
	timer.Create( "UniqueName3", 5, 1, PrintNoArguments )
end
hook.Add( "Initialize", "Timer Example", CreateSomeTimers )

Output:

inside -- 1 second

outside -- 2 seconds

outside -- 4 seconds

fun with timers! -- 5 seconds

outside -- 6 seconds

outside -- 8 seconds

Examples

Example

Creates a timer that has 0.01 second delay, to demonstrate that the "minimum" delay of a timer is locked at the tickrate period (1/66 seconds).

(As the example below shows, by setting the delay rate to 1/100 (0.01 seconds), the difference in time between the iterations of the timer should be 0.01 seconds, but instead, it is 0.149 (1/66) seconds).

local tick = {} 
local tick_key = 1 

local function MinimumTimerDelay()	
	local current_time = CurTime() 
	if tick_key > 1 then 
		print( "Timer Iteration #" .. tick_key - 1 .. " had a delay of " .. current_time - tick[tick_key - 1])
	end
	tick[tick_key] = current_time 
	tick_key = tick_key + 1	
end

local function Timer()
	timer.Create("Timer Delay", (1/100), 10, MinimumTimerDelay ) 
end

hook.Add("Initialize", "Commence Timers", Timer)

Output:

Timer Iteration #1 had a delay of 0.014999389648438

Timer Iteration #2 had a delay of 0.014999389648438

Timer Iteration #3 had a delay of 0.014999389648438

Timer Iteration #4 had a delay of 0.0150146484375

Timer Iteration #5 had a delay of 0.014999389648438

Timer Iteration #6 had a delay of 0.014999389648438

Timer Iteration #7 had a delay of 0.014999389648438

Timer Iteration #8 had a delay of 0.014999389648438

Timer Iteration #9 had a delay of 0.014999389648438
Personal tools
Navigation