Difference between revisions of "Lua:createTimer"
Jump to navigation
Jump to search
(Major overhaul of the post.) |
m (Added related function template.) |
||
| (One intermediate revision by the same user not shown) | |||
| Line 65: | Line 65: | ||
===Persistent Timer Example=== | ===Persistent Timer Example=== | ||
| − | < | + | <syntaxhighlight lang="lua" line> |
local timer = createTimer(nil, false) | local timer = createTimer(nil, false) | ||
| Line 74: | Line 74: | ||
timer.Enabled = true | timer.Enabled = true | ||
| − | </ | + | </syntaxhighlight> |
===Timer With Owner Example=== | ===Timer With Owner Example=== | ||
| − | < | + | <syntaxhighlight lang="lua" line> |
local form = createForm() | local form = createForm() | ||
form.Caption = "Timer Example" | form.Caption = "Timer Example" | ||
| Line 88: | Line 88: | ||
form.show() | form.show() | ||
| − | </ | + | </syntaxhighlight> |
===Stopping A Persistent Timer=== | ===Stopping A Persistent Timer=== | ||
| − | < | + | <syntaxhighlight lang="lua" line> |
local timer = createTimer() | local timer = createTimer() | ||
timer.Interval = 1000 | timer.Interval = 1000 | ||
| Line 106: | Line 106: | ||
end | end | ||
end | end | ||
| − | </ | + | </syntaxhighlight> |
===One-Shot Timer Example=== | ===One-Shot Timer Example=== | ||
| − | < | + | <syntaxhighlight lang="lua" line> |
createTimer(2000, function() | createTimer(2000, function() | ||
print("Executed after 2 seconds") | print("Executed after 2 seconds") | ||
end) | end) | ||
| − | </ | + | </syntaxhighlight> |
===One-Shot Timer With Parameters=== | ===One-Shot Timer With Parameters=== | ||
| − | < | + | <syntaxhighlight lang="lua" line> |
local name = "Player" | local name = "Player" | ||
local health = 100 | local health = 100 | ||
| Line 123: | Line 123: | ||
print(playerName .. " health: " .. hp) | print(playerName .. " health: " .. hp) | ||
end, name, health) | end, name, health) | ||
| − | </ | + | </syntaxhighlight> |
===Delayed Form Update Example=== | ===Delayed Form Update Example=== | ||
| − | < | + | <syntaxhighlight lang="lua" line> |
local form = createForm() | local form = createForm() | ||
form.Caption = "Loading..." | form.Caption = "Loading..." | ||
| Line 134: | Line 134: | ||
form.Caption = "Ready!" | form.Caption = "Ready!" | ||
end) | end) | ||
| − | </ | + | </syntaxhighlight> |
===Notes=== | ===Notes=== | ||
| Line 150: | Line 150: | ||
One-shot timers destroy themselves automatically after firing. Usually, you should create them and let them run. | One-shot timers destroy themselves automatically after firing. Usually, you should create them and let them run. | ||
| − | < | + | <syntaxhighlight lang="lua" line> |
createTimer(1000, function() | createTimer(1000, function() | ||
print("This one-shot timer destroys itself") | print("This one-shot timer destroys itself") | ||
end) | end) | ||
| − | </ | + | </syntaxhighlight> |
====Forgetting To Destroy Persistent Timers==== | ====Forgetting To Destroy Persistent Timers==== | ||
Persistent timers should either have an owner or be destroyed manually when no longer needed. | Persistent timers should either have an owner or be destroyed manually when no longer needed. | ||
| − | < | + | <syntaxhighlight lang="lua" line> |
local timer = createTimer(getMainForm()) | local timer = createTimer(getMainForm()) | ||
| Line 166: | Line 166: | ||
print("This timer is owned by the main form") | print("This timer is owned by the main form") | ||
end | end | ||
| − | </ | + | </syntaxhighlight> |
{{LuaSeeAlso}} | {{LuaSeeAlso}} | ||
| − | + | {{Creation}} | |
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
Latest revision as of 23:55, 26 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[edit]
Persistent Timer[edit]
| 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[edit]
| 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[edit]
Timer — The created Timer object.
Calling Modes[edit]
| 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[edit]
1 local timer = createTimer(nil, false)
2
3 timer.Interval = 1000
4 timer.OnTimer = function(sender)
5 print("Tick: " .. os.date("%H:%M:%S"))
6 end
7
8 timer.Enabled = true
Timer With Owner Example[edit]
1 local form = createForm()
2 form.Caption = "Timer Example"
3
4 local timer = createTimer(form)
5 timer.Interval = 500
6 timer.OnTimer = function(sender)
7 form.Caption = "Time: " .. os.date("%H:%M:%S")
8 end
9
10 form.show()
Stopping A Persistent Timer[edit]
1 local timer = createTimer()
2 timer.Interval = 1000
3
4 local count = 0
5
6 timer.OnTimer = function(sender)
7 count = count + 1
8 print("Count: " .. count)
9
10 if count >= 10 then
11 sender.Enabled = false
12 sender.destroy()
13 end
14 end
One-Shot Timer Example[edit]
1 createTimer(2000, function()
2 print("Executed after 2 seconds")
3 end)
One-Shot Timer With Parameters[edit]
1 local name = "Player"
2 local health = 100
3
4 createTimer(1500, function(playerName, hp)
5 print(playerName .. " health: " .. hp)
6 end, name, health)
Delayed Form Update Example[edit]
1 local form = createForm()
2 form.Caption = "Loading..."
3 form.show()
4
5 createTimer(2000, function()
6 form.Caption = "Ready!"
7 end)
Notes[edit]
- 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[edit]
Destroying One-Shot Timers[edit]
One-shot timers destroy themselves automatically after firing. Usually, you should create them and let them run.
1 createTimer(1000, function()
2 print("This one-shot timer destroys itself")
3 end)
Forgetting To Destroy Persistent Timers[edit]
Persistent timers should either have an owner or be destroyed manually when no longer needed.
1 local timer = createTimer(getMainForm())
2
3 timer.Interval = 1000
4 timer.OnTimer = function(sender)
5 print("This timer is owned by the main form")
6 end