Commands/Lua

Reference for console commands, Lua hooks and Lua commands in CS2D.

CS2D Command CS2D Console Commands

Lua Hook Lua Hooks

Lua Command Lua Commands

Category: all (86)

Lua Command timer

Categories

Parameters

  • time
  • "function"
  • ["parameter"] (optional)
  • [count] (optional)

Info

Creates a timer which will call the Lua function "function" after a certain time in milliseconds (time). Moreover it can pass an optional string parameter ("parameter") to this function. The timer calls the function once by default. However you can call it several times by entering the optional count parameter (count). Using 0 or negative count values will make the timer call the function infinite times or until it is removed via freetimer.

Note: Time is specified in ms (milliseconds, 1000 ms = 1 sec)

Note: This will throw an "attempt to call a nil value" Lua error in the console when the specified function ("function") does not exist. "function" must be a string which equals the name of an existing Lua function.

Note: CS2D is single threaded and runs with a locked maximum frame rate. This means that the FPS lock also limits the precision of timers. Be aware that - depending on the FPS - timers might be executed a few milliseconds later than specified: capped @ 60 FPS, 1000 ms / 60 FPS = 16.6 ms timer precision (in best case). This imprecision may also sum up when executing a timer more often (count<=0 or count>1).

Attention: Creating many timers with a high frequency (low time value) and a high/infinite count can slow down the game if the executed functions are complex. Use freetimer to remove unused timers!

Attention: CS2D does not check if equal timers already exist. It will just create a new timer whenever you call the timer command. This can lead to multiple exactly equal timers.


Sample 1: Executing a function once with 5 secs delay and without parameter
timer(5000,"dothislater")
function dothislater()
   msg("This text is displayed 5 seconds after using timer")
end


Sample 2: Executing a function 10 times with a 1 second delay
timer(1000,"tick","",10)
function tick()
   msg("*tick*")
end


Sample 3: Delayed execution with parameter
timer(3000,"delayedtext","Hi, this is displayed delayed using a timer")
function delayedtext(text)
   msg(text)
end


Sample 4: Using the parse Lua function to execute CS2D commands with delay (note how ' and " are used for string encapsulation!)
timer(1000,"parse",'sv_msg "Server shutdown in 5 secs"')
timer(2000,"parse",'sv_msg "... 4 secs"')
timer(3000,"parse",'sv_msg "... 3 secs"')
timer(4000,"parse",'sv_msg "... 1 sec"')
timer(5000,"parse",'disconnect')