Zum Hauptinhalt springen
Version: bleeding-edge 🩸

⏱️ Timer

Execute code at specified time intervals

🗿Static ClassClient & Server🧑‍💻API Source

info

The shortest interval possible is equal to the local Tick Rate - usually at 33ms. On the Server this can vary depending on the Config.toml setting.

🎒 Examples​

-- creates a Interval to call a function at every 2 seconds
local param1, param2 = "awesome param 1", 456

local my_interval = Timer.SetInterval(function()
-- param1 and param2 are captured by the function itself
Console.Log("Triggered each 2 seconds! Param1: " .. param1 .. ". Param2: " .. param2)
end, 2000)

-- cancels the Interval
Timer.ClearInterval(my_interval)

-- creates a Timeout to call a function in 5 seconds, once
local my_param = "world"

Timer.SetTimeout(function()
Console.Log("nanos " .. my_param)
end, 5000)
(Timer.Bind) Binding a Timer to an Entity
local character = Character(Vector(), Rotator(), "nanos-world::SK_Male")

local my_timer = Timer.SetTimeout(function(_character)
-- Do something with _character
-- Ex: Destroy the character after 10 seconds
_character:Destroy()
end, 10000, character)

-- Binds the Timer to the Character
-- This will ensure it will never trigger if the character is destroyed before it
-- With this you don't need to validate if the '_character' parameter is valid
Timer.Bind(my_timer, character)

🗿 Static Functions​

BindBinds a Timer to any Actor. The timer will be automatically cleared when the Actor is destroyed
ClearIntervalStops the execution of the function specified in SetInterval()
ClearTimeoutStops the execution of the function specified in SetTimeout()
GetElapsedTime: integerReturns the time elapsed since the last tick
GetRemainingTime: integerReturns the time remaining to the next tick
IsValid: booleanChecks if a Timer is currently active or waiting to be triggered
PausePauses the Timer
ResetElapsedTimeResets a Timer to restart from beginning
ResumeResumes the Timer
SetInterval: integerSame as SetTimeout(), but repeats the execution of the function continuously
SetRemainingTimeSets the time remaining to the next tick
SetTimeout: integerExecutes a function, after waiting a specified number of milliseconds
Unbind: booleanUnbinds a Timer from an Actor it was bound to with Timer.Bind, without clearing or resetting the Timer

BindOptimal​

Binds a Timer to any Actor. The timer will be automatically cleared when the Actor is destroyed

Timer.Bind(timer_id, actor)
Parameters
NameTypeBeschreibung
timer_idintegerThe Timer ID
actorBase ActorActor to be bound
Examples (1)
Binding a Timer to an Entity
local character = Character(Vector(), Rotator(), "nanos-world::SK_Male")

local my_timer = Timer.SetTimeout(function(_character)
-- Do something with _character
-- Ex: Destroy the character after 10 seconds
_character:Destroy()
end, 10000, character)

-- Binds the Timer to the Character
-- This will ensure it will never trigger if the character is destroyed before it
-- With this you don't need to validate if the '_character' parameter is valid
Timer.Bind(my_timer, character)

ClearIntervalOptimal​

Stops the execution of the function specified in SetInterval()

Timer.ClearInterval(interval_id)
Parameters
NameTypeBeschreibung
interval_idintegerThe ID value returned by SetInterval() is used as the parameter for this method

ClearTimeoutOptimal​

Stops the execution of the function specified in SetTimeout()

Timer.ClearTimeout(timeout_id)
Parameters
NameTypeBeschreibung
timeout_idintegerThe ID value returned by SetTimeout() is used as the parameter for this method

GetElapsedTimeOptimal​

Returns the time elapsed since the last tick

local elapsed_time = Timer.GetElapsedTime(timer_id)
Parameters
NameTypeBeschreibung
timer_idintegerThe Timer ID
Returns
TypeBeschreibung
integerTime elapsed since the last tick

GetRemainingTimeOptimal​

Returns the time remaining to the next tick

local remaining_time = Timer.GetRemainingTime(timer_id)
Parameters
NameTypeBeschreibung
timer_idintegerThe Timer ID
Returns
TypeBeschreibung
integerTime remaining to the next tick

See also SetRemainingTime.

IsValidOptimal​

Checks if a Timer is currently active or waiting to be triggered

local is_valid = Timer.IsValid(timer_id)
Parameters
NameTypeBeschreibung
timer_idintegerThe Timer ID
Returns
TypeBeschreibung
booleanWhether a Timer is currently active or waiting to be triggered

PauseOptimal​

Pauses the Timer

Timer.Pause(timer_id)
Parameters
NameTypeBeschreibung
timer_idintegerThe Timer ID

ResetElapsedTimeOptimal​

Resets a Timer to restart from beginning

Timer.ResetElapsedTime(timer_id)
Parameters
NameTypeBeschreibung
timer_idintegerThe Timer ID

ResumeOptimal​

Resumes the Timer

Timer.Resume(timer_id)
Parameters
NameTypeBeschreibung
timer_idintegerThe Timer ID

SetIntervalOptimal​

Same as SetTimeout(), but repeats the execution of the function continuously

local ret = Timer.SetInterval(callback, milliseconds?, parameters...?)
Parameters
NameTypeBeschreibung
callbackfunctionThe callback that will be executed.
Return false to stop it from being called
milliseconds?integerThe time in milliseconds the timer should delay in between executions of the specified function Default 0
parameters...?anyAdditional parameters to pass to the function Default nil
Returns
TypeBeschreibung
integerthe interval_id

SetRemainingTimeModerate​

Sets the time remaining to the next tick

Timer.SetRemainingTime(timer_id, time)
Parameters
NameTypeBeschreibung
timer_idintegerThe Timer ID
timeintegerDie verbleibende Zeit in Millisekunden

See also GetRemainingTime.

SetTimeoutOptimal​

Executes a function, after waiting a specified number of milliseconds

local ret = Timer.SetTimeout(callback, milliseconds?, parameters...?)
Parameters
NameTypeBeschreibung
callbackfunctionThe callback that will be executed
milliseconds?integerThe time in milliseconds to wait before executing the function Default 0
parameters...?anyAdditional parameters to pass to the function Default nil
Returns
TypeBeschreibung
integerthe timeout_id

UnbindOptimal​

Unbinds a Timer from an Actor it was bound to with Timer.Bind, without clearing or resetting the Timer

local ret = Timer.Unbind(timer_id, actor)
Parameters
NameTypeBeschreibung
timer_idintegerThe Timer ID
actorBase ActorActor to be unbound
Returns
TypeBeschreibung
booleantrue if the Timer was bound to this Actor and got unbound

🚀 Events​

This class doesn't have own events.