⏱️ 计时器
在指定的时间间隔执行代码。

信息
最短的可能间隔等于本地刻速率(Tick Rate)——通常为 33ms。 在服务器端,该数值可能会根据 Config.toml 的设置而有所不同。
🎒 示例
-- 创建一个间隔,每 2 秒调用一次函数
local my_interval = Timer.SetInterval(function(param1, param2)
Console.Log("每 2 秒触发一次!Param1:" .. param1 .. "。Param2:" .. param2)
end, 2000, "awesome param 1", 456)
-- 取消间隔
Timer.ClearInterval(my_interval)
-- 创建一个超时,在 5 秒后调用 my_function 一次 - 传递一个参数
Timer.SetTimeout(function(my_param)
Console.Log("nanos " .. my_param)
end, 5000, "world")
(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)
🗿 静态函数
| Returns | Name | Description | |
|---|---|---|---|
Bind | 将一个计时器绑定到任何 Actor。当该 Actor 被销毁时,计时器将自动被清除 | ||
ClearInterval | 停止执行在 SetInterval() 中指定的函数 | ||
ClearTimeout | 停止执行在 SetTimeout() 中指定的函数 | ||
| integer | GetElapsedTime | 返回自上一个刻以来已过去的时间 | |
| integer | GetRemainingTime | 返回距离下一个刻所剩余的时间 | |
| boolean | IsValid | 检查一个计时器当前是否处于活动状态或正在等待触发 | |
Pause | 暂停计时器 | ||
ResetElapsedTime | 重置计时器以使其重新从头开始 | ||
Resume | 恢复计时器 | ||
| integer | SetInterval | 与 SetTimeout() 相同,但会连续循环地重复执行该函数 | |
SetRemainingTime | 设置距离下一个刻所剩余的时间 | ||
| integer | SetTimeout | 在等待指定的毫秒数后,执行一个函数 |
Optimal

Bind
将一个计时器绑定到任何 Actor。当该 Actor 被销毁时,计时器将自动被清除
Timer.Bind(timer_id, actor)
Parameters
| Type | Parameter | Default | Description |
|---|---|---|---|
| integer | timer_id | Required parameter | 计时器 ID |
| Base Actor | actor | Required parameter | 要绑定的 Actor |
Timer.Bind Examples
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)
Optimal

ClearInterval
停止执行在 SetInterval() 中指定的函数
Timer.ClearInterval(interval_id)
Parameters
| Type | Parameter | Default | Description |
|---|---|---|---|
| integer | interval_id | Required parameter | SetInterval() 返回的 ID 值用作此方法的参数 |
Optimal

ClearTimeout
停止执行在 SetTimeout() 中指定的函数
Timer.ClearTimeout(timeout_id)
Parameters
| Type | Parameter | Default | Description |
|---|---|---|---|
| integer | timeout_id | Required parameter | SetTimeout() 返回的 ID 值用作此方法的参数 |
Optimal

GetElapsedTime
返回自上一个刻以来已过去的时间
local elapsed_time = Timer.GetElapsedTime(timer_id)
Parameters
| Type | Parameter | Default | Description |
|---|---|---|---|
| integer | timer_id | Required parameter | 计时器 ID |
Returns
| Type | Description |
|---|---|
| integer | elapsed_time |
Optimal

GetRemainingTime
返回距离下一个刻所剩余的时间
local remaining_time = Timer.GetRemainingTime(timer_id)
Parameters
| Type | Parameter | Default | Description |
|---|---|---|---|
| integer | timer_id | Required parameter | 计时器 ID |
Returns
| Type | Description |
|---|---|
| integer | remaining_time |
See also SetRemainingTime.
Optimal

IsValid
检查一个计时器当前是否处于活动状态或正在等待触发
local is_valid = Timer.IsValid(timer_id)
Parameters
| Type | Parameter | Default | Description |
|---|---|---|---|
| integer | timer_id | Required parameter | 计时器 ID |
Returns
| Type | Description |
|---|---|
| boolean | is_valid |
Optimal

Pause
暂停计时器
Timer.Pause(timer_id)
Parameters
| Type | Parameter | Default | Description |
|---|---|---|---|
| integer | timer_id | Required parameter | 计时器 ID |
Optimal

ResetElapsedTime
重置计时器以使其重新从头开始
Timer.ResetElapsedTime(timer_id)
Parameters
| Type | Parameter | Default | Description |
|---|---|---|---|
| integer | timer_id | Required parameter | 计时器 ID |
Optimal

Resume
恢复计时器
Timer.Resume(timer_id)
Parameters
| Type | Parameter | Default | Description |
|---|---|---|---|
| integer | timer_id | Required parameter | 计时器 ID |
Optimal

SetInterval
与 SetTimeout() 相同,但会连续循环地重复执行该函数
local ret = Timer.SetInterval(callback, milliseconds?, parameters...?)
Parameters
| Type | Parameter | Default | Description |
|---|---|---|---|
| function | callback | Required parameter | 将要执行的回调函数。 返回 false 以阻止其继续被调用。 |
| integer | milliseconds? | 0 | 计时器在每次执行指定函数之间需要延迟的时间(毫秒) |
| any | parameters...? | nil | 传递给该函数的额外参数 |
Returns
| Type | Description |
|---|---|
| integer | interval_id |
Moderate

SetRemainingTime
设置距离下一个刻所剩余的时间
Timer.SetRemainingTime(timer_id, time)
Parameters
| Type | Parameter | Default | Description |
|---|---|---|---|
| integer | timer_id | Required parameter | 计时器 ID |
| integer | time | Required parameter | 剩余的时间(毫秒) |
See also GetRemainingTime.
Optimal

SetTimeout
在等待指定的毫秒数后,执行一个函数
local ret = Timer.SetTimeout(callback, milliseconds?, parameters...?)
Parameters
| Type | Parameter | Default | Description |
|---|---|---|---|
| function | callback | Required parameter | 将要执行的回调函数 |
| integer | milliseconds? | 0 | 在执行函数前需要等待的时间(毫秒) |
| any | parameters...? | nil | 传递给该函数的额外参数 |
Returns
| Type | Description |
|---|---|
| integer | timeout_id |
🚀 事件
This class doesn't have own events.

