Difference between revisions of "Lua:createTimer"
Jump to navigation
Jump to search
Mr millchick (talk | contribs) (Created page with "Category:Lua '''function''' createTimer(''owner'' OPTIONAL, ''enabled'' OPTIONAL) : '''Timer''' '''function''' createTimer(''interval'', ''callback'', ''...'' OPTIONAL) :...") |
(Major overhaul of the post.) |
||
| Line 1: | Line 1: | ||
[[Category:Lua]] | [[Category:Lua]] | ||
| − | '''function''' createTimer(''owner'' | + | {{CodeBox|'''function''' createTimer(''owner'', ''enabled'') ''':''' Timer}} |
| + | {{CodeBox|'''function''' createTimer(''interval'', ''callback'', ''...'') ''':''' Timer}} | ||
| − | + | Creates a [[Lua:Class:Timer|Timer]] object. | |
| − | + | This function has two calling modes. The first mode creates a persistent timer that keeps running until it is disabled or destroyed. The second mode creates a one-shot timer that executes a callback once and then destroys itself. | |
| − | + | ===Function Parameters=== | |
| − | + | ====Persistent Timer==== | |
| + | {{CodeBox|'''function''' createTimer(''owner'', ''enabled'') ''':''' Timer}} | ||
| − | + | {|width="85%" cellpadding="10%" cellspacing="0" border="0" | |
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | {|width="85%" cellpadding="10 | ||
!align="left"|Parameter | !align="left"|Parameter | ||
!align="left"|Type | !align="left"|Type | ||
| − | !style="width: 80%;" align="left"|Description | + | !style="width: 80%;background-color:white;" align="left"|Description |
|- | |- | ||
|owner | |owner | ||
| − | |Component or nil | + | |Component or nil (optional) |
| − | | | + | |The owner of the timer. When the owner is destroyed, the timer is destroyed too. Pass nil or omit this parameter for a standalone timer. |
|- | |- | ||
|enabled | |enabled | ||
| − | | | + | |Boolean (optional) |
| − | | | + | |If true, the timer starts enabled. If false, the timer is created disabled. |
|} | |} | ||
| − | === | + | ====One-Shot Timer==== |
| + | {{CodeBox|'''function''' createTimer(''interval'', ''callback'', ''...'') ''':''' Timer}} | ||
| − | {|width="85%" cellpadding="10 | + | {|width="85%" cellpadding="10%" cellspacing="0" border="0" |
!align="left"|Parameter | !align="left"|Parameter | ||
!align="left"|Type | !align="left"|Type | ||
| − | !style="width: 80%;" align="left"|Description | + | !style="width: 80%;background-color:white;" align="left"|Description |
|- | |- | ||
|interval | |interval | ||
| − | | | + | |Integer |
| − | | | + | |The delay in milliseconds before the callback is executed. |
|- | |- | ||
|callback | |callback | ||
| − | | | + | |Function |
| − | | | + | |The function to execute when the timer fires. |
|- | |- | ||
|... | |... | ||
| − | | | + | |Any (optional) |
| − | | | + | |Additional arguments that are passed to the callback function. |
|} | |} | ||
| + | ===Returns=== | ||
| + | Timer — The created [[Lua:Class:Timer|Timer]] object. | ||
| − | == | + | ===Calling Modes=== |
| + | {|width="85%" cellpadding="10%" cellspacing="0" border="0" | ||
| + | !align="left"|Mode | ||
| + | !align="left"|Syntax | ||
| + | !style="width: 80%;background-color:white;" align="left"|Description | ||
| + | |- | ||
| + | |Persistent Timer | ||
| + | |createTimer(owner, enabled) | ||
| + | |Creates a timer that fires repeatedly until it is disabled or destroyed. | ||
| + | |- | ||
| + | |One-Shot Timer | ||
| + | |createTimer(interval, callback, ...) | ||
| + | |Creates a timer that fires once, executes the callback, and then destroys itself. | ||
| + | |} | ||
| − | === Persistent Timer | + | ===Persistent Timer Example=== |
| + | <pre> | ||
| + | local timer = createTimer(nil, false) | ||
| − | + | timer.Interval = 1000 | |
| − | + | timer.OnTimer = function(sender) | |
| − | + | print("Tick: " .. os.date("%H:%M:%S")) | |
| − | + | end | |
| − | |||
| − | |||
| − | + | timer.Enabled = true | |
| − | + | </pre> | |
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | + | ===Timer With Owner Example=== | |
| − | + | <pre> | |
| − | + | local form = createForm() | |
| − | + | form.Caption = "Timer Example" | |
| − | |||
| − | |||
| − | + | local timer = createTimer(form) | |
| − | + | timer.Interval = 500 | |
| − | + | timer.OnTimer = function(sender) | |
| − | + | form.Caption = "Time: " .. os.date("%H:%M:%S") | |
| − | + | end | |
| − | |||
| − | |||
| − | |||
| − | + | form.show() | |
| − | + | </pre> | |
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | + | ===Stopping A Persistent Timer=== | |
| − | + | <pre> | |
| − | + | local timer = createTimer() | |
| − | + | timer.Interval = 1000 | |
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| + | local count = 0 | ||
| − | == | + | timer.OnTimer = function(sender) |
| + | count = count + 1 | ||
| + | print("Count: " .. count) | ||
| − | + | if count >= 10 then | |
| − | + | sender.Enabled = false | |
| − | + | sender.destroy() | |
| − | + | end | |
| + | end | ||
| + | </pre> | ||
| − | + | ===One-Shot Timer Example=== | |
| − | + | <pre> | |
| − | + | createTimer(2000, function() | |
| − | + | print("Executed after 2 seconds") | |
| − | + | end) | |
| − | + | </pre> | |
| − | |||
| − | + | ===One-Shot Timer With Parameters=== | |
| − | + | <pre> | |
| − | + | local name = "Player" | |
| − | + | local health = 100 | |
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | + | createTimer(1500, function(playerName, hp) | |
| − | + | print(playerName .. " health: " .. hp) | |
| − | + | end, name, health) | |
| − | + | </pre> | |
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| + | ===Delayed Form Update Example=== | ||
| + | <pre> | ||
| + | local form = createForm() | ||
| + | form.Caption = "Loading..." | ||
| + | form.show() | ||
| − | = | + | createTimer(2000, function() |
| + | form.Caption = "Ready!" | ||
| + | end) | ||
| + | </pre> | ||
| − | * | + | ===Notes=== |
| − | * | + | * The interval is specified in milliseconds. |
| − | * | + | * A value of 1000 means one second. |
| − | * | + | * Persistent timers keep firing until disabled or destroyed. |
| − | * Use | + | * One-shot timers destroy themselves after the callback has executed. |
| − | * Timer callbacks execute in | + | * Do not manually destroy a one-shot timer after it has fired. |
| − | + | * Use an owner such as a form or getMainForm() when the timer should be cleaned up automatically. | |
| − | * Timer accuracy depends on system load | + | * Timer callbacks execute in Cheat Engine's main thread. |
| + | * Timer accuracy depends on system load and the operating system timer resolution. | ||
| + | ===Common Mistakes=== | ||
| + | ====Destroying One-Shot Timers==== | ||
| + | One-shot timers destroy themselves automatically after firing. Usually, you should create them and let them run. | ||
| − | + | <pre> | |
| + | createTimer(1000, function() | ||
| + | print("This one-shot timer destroys itself") | ||
| + | end) | ||
| + | </pre> | ||
| − | + | ====Forgetting To Destroy Persistent Timers==== | |
| − | + | Persistent timers should either have an owner or be destroyed manually when no longer needed. | |
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | + | <pre> | |
| − | + | local timer = createTimer(getMainForm()) | |
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| + | timer.Interval = 1000 | ||
| + | timer.OnTimer = function(sender) | ||
| + | print("This timer is owned by the main form") | ||
| + | end | ||
| + | </pre> | ||
{{LuaSeeAlso}} | {{LuaSeeAlso}} | ||
| − | === Related Functions === | + | ===Related Functions=== |
| − | * [[Lua:sleep|sleep]] | + | * [[Lua:sleep|sleep]] |
| − | * [[Lua:createThread|createThread]] | + | * [[Lua:createThread|createThread]] |
| − | === Related Classes === | + | ===Related Classes=== |
| − | * [[Lua:Class:Timer|Timer]] | + | * [[Lua:Class:Timer|Timer]] |
| − | * [[Lua:Class:Component|Component]] | + | * [[Lua:Class:Component|Component]] |
Revision as of 19:19, 23 June 2026
Creates a Timer object.
This function has two calling modes. The first mode creates a persistent timer that keeps running until it is disabled or destroyed. The second mode creates a one-shot timer that executes a callback once and then destroys itself.
Contents
Function Parameters
Persistent Timer
| Parameter | Type | Description |
|---|---|---|
| owner | Component or nil (optional) | The owner of the timer. When the owner is destroyed, the timer is destroyed too. Pass nil or omit this parameter for a standalone timer. |
| enabled | Boolean (optional) | If true, the timer starts enabled. If false, the timer is created disabled. |
One-Shot Timer
| Parameter | Type | Description |
|---|---|---|
| interval | Integer | The delay in milliseconds before the callback is executed. |
| callback | Function | The function to execute when the timer fires. |
| ... | Any (optional) | Additional arguments that are passed to the callback function. |
Returns
Timer — The created Timer object.
Calling Modes
| Mode | Syntax | Description |
|---|---|---|
| Persistent Timer | createTimer(owner, enabled) | Creates a timer that fires repeatedly until it is disabled or destroyed. |
| One-Shot Timer | createTimer(interval, callback, ...) | Creates a timer that fires once, executes the callback, and then destroys itself. |
Persistent Timer Example
local timer = createTimer(nil, false)
timer.Interval = 1000
timer.OnTimer = function(sender)
print("Tick: " .. os.date("%H:%M:%S"))
end
timer.Enabled = true
Timer With Owner Example
local form = createForm()
form.Caption = "Timer Example"
local timer = createTimer(form)
timer.Interval = 500
timer.OnTimer = function(sender)
form.Caption = "Time: " .. os.date("%H:%M:%S")
end
form.show()
Stopping A Persistent Timer
local timer = createTimer()
timer.Interval = 1000
local count = 0
timer.OnTimer = function(sender)
count = count + 1
print("Count: " .. count)
if count >= 10 then
sender.Enabled = false
sender.destroy()
end
end
One-Shot Timer Example
createTimer(2000, function()
print("Executed after 2 seconds")
end)
One-Shot Timer With Parameters
local name = "Player" local health = 100 createTimer(1500, function(playerName, hp) print(playerName .. " health: " .. hp) end, name, health)
Delayed Form Update Example
local form = createForm() form.Caption = "Loading..." form.show() createTimer(2000, function() form.Caption = "Ready!" end)
Notes
- The interval is specified in milliseconds.
- A value of 1000 means one second.
- Persistent timers keep firing until disabled or destroyed.
- One-shot timers destroy themselves after the callback has executed.
- Do not manually destroy a one-shot timer after it has fired.
- Use an owner such as a form or getMainForm() when the timer should be cleaned up automatically.
- Timer callbacks execute in Cheat Engine's main thread.
- Timer accuracy depends on system load and the operating system timer resolution.
Common Mistakes
Destroying One-Shot Timers
One-shot timers destroy themselves automatically after firing. Usually, you should create them and let them run.
createTimer(1000, function()
print("This one-shot timer destroys itself")
end)
Forgetting To Destroy Persistent Timers
Persistent timers should either have an owner or be destroyed manually when no longer needed.
local timer = createTimer(getMainForm())
timer.Interval = 1000
timer.OnTimer = function(sender)
print("This timer is owned by the main form")
end