Difference between revisions of "Lua:createTimer"

From Cheat Engine
Jump to navigation Jump to search
(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'' OPTIONAL, ''enabled'' OPTIONAL) : '''Timer'''
+
{{CodeBox|'''function''' createTimer(''owner'', ''enabled'') ''':''' Timer}}
 +
{{CodeBox|'''function''' createTimer(''interval'', ''callback'', ''...'') ''':''' Timer}}
  
'''function''' createTimer(''interval'', ''callback'', ''...'' OPTIONAL) : '''Timer'''
+
Creates a [[Lua:Class:Timer|Timer]] object.
  
Creates a [[Lua:Class:Timer|Timer]] object that fires repeatedly or once at specified intervals.
+
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.
  
* '''Returns:''' A [[Lua:Class:Timer|Timer]] UserData object
+
===Function Parameters===
* '''Modes:''' Two distinct calling modes with different behaviors
+
====Persistent Timer====
 +
{{CodeBox|'''function''' createTimer(''owner'', ''enabled'') ''':''' Timer}}
  
 
+
{|width="85%" cellpadding="10%" cellspacing="0" border="0"
== Two Calling Modes ==
 
 
 
'''Mode 1: Persistent Timer''' - Creates a recurring timer that continues until destroyed or disabled
 
createTimer(owner, enabled)
 
 
 
'''Mode 2: One-Shot Timer''' - Creates a timer that fires once and auto-destroys
 
createTimer(interval, callback, ...)
 
 
 
 
 
== Function Parameters ==
 
 
 
=== Mode 1: Persistent Timer ===
 
 
 
{|width="85%" cellpadding="10%" cellpadding="5%" 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
 
|-
 
|-
 
|owner
 
|owner
|Component or nil
+
|Component or nil (optional)
|'''Optional.''' Parent component that owns this timer. When the owner is destroyed, the timer is destroyed too. Pass '''nil''' or omit for standalone timer. Default: '''nil'''
+
|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
+
|Boolean (optional)
|'''Optional.''' Whether the timer starts active immediately. Default: '''true'''
+
|If true, the timer starts enabled. If false, the timer is created disabled.
 
|}
 
|}
  
=== Mode 2: One-Shot Timer ===
+
====One-Shot Timer====
 +
{{CodeBox|'''function''' createTimer(''interval'', ''callback'', ''...'') ''':''' Timer}}
  
{|width="85%" cellpadding="10%" cellpadding="5%" cellspacing="0" border="0"
+
{|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
+
|Integer
|Timer delay in '''milliseconds''' before callback fires once
+
|The delay in milliseconds before the callback is executed.
 
|-
 
|-
 
|callback
 
|callback
|function
+
|Function
|Function to execute when the timer fires. Receives any additional varargs as parameters
+
|The function to execute when the timer fires.
 
|-
 
|-
 
|...
 
|...
|'''varargs'''
+
|Any (optional)
|'''Optional.''' Additional arguments to pass to the callback function
+
|Additional arguments that are passed to the callback function.
 
|}
 
|}
  
 +
===Returns===
 +
Timer — The created [[Lua:Class:Timer|Timer]] object.
  
== Usage Examples ==
+
===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 Examples ===
+
===Persistent Timer Example===
 +
<pre>
 +
local timer = createTimer(nil, false)
  
'''Basic recurring timer (runs every second):'''
+
timer.Interval = 1000
local timer = createTimer()
+
timer.OnTimer = function(sender)
timer.Interval = 1000 -- 1 second
+
  print("Tick: " .. os.date("%H:%M:%S"))
timer.OnTimer = function()
+
end
  print("Tick: " .. os.date("%H:%M:%S"))
 
end
 
  
'''Timer with owner (destroyed when form closes):'''
+
timer.Enabled = true
local form = createForm()
+
</pre>
local timer = createTimer(form)
 
timer.Interval = 500
 
timer.OnTimer = function()
 
  form.Caption = "Time: " .. os.date("%H:%M:%S")
 
end
 
  
'''Timer owned by main form (auto-cleanup when CE closes):'''
+
===Timer With Owner Example===
local timer = createTimer(getMainForm())
+
<pre>
timer.Interval = 1000
+
local form = createForm()
timer.OnTimer = function()
+
form.Caption = "Timer Example"
  print("Running until CE closes")
 
end
 
  
'''Create disabled timer and enable later:'''
+
local timer = createTimer(form)
local timer = createTimer(nil, false)
+
timer.Interval = 500
timer.Interval = 2000
+
timer.OnTimer = function(sender)
timer.OnTimer = function()
+
  form.Caption = "Time: " .. os.date("%H:%M:%S")
  print("2 seconds passed")
+
end
end
 
-- Enable when needed
 
timer.Enabled = true
 
  
'''Manual cleanup when done:'''
+
form.show()
local timer = createTimer()
+
</pre>
timer.Interval = 100
 
local count = 0
 
timer.OnTimer = function()
 
  count = count + 1
 
  print("Count: " .. count)
 
  if count >= 10 then
 
    timer.Enabled = false
 
    timer.destroy()
 
    print("Timer stopped and destroyed")
 
  end
 
end
 
  
'''Toggle timer on/off:'''
+
===Stopping A Persistent Timer===
local timer = createTimer()
+
<pre>
timer.Interval = 1000
+
local timer = createTimer()
timer.Enabled = false
+
timer.Interval = 1000
timer.OnTimer = function()
 
  print("Active!")
 
end
 
 
-- Start
 
timer.Enabled = true
 
-- Stop
 
timer.Enabled = false
 
  
 +
local count = 0
  
=== One-Shot Timer Examples ===
+
timer.OnTimer = function(sender)
 +
  count = count + 1
 +
  print("Count: " .. count)
  
'''Simple delay (auto-destroys after firing):'''
+
  if count >= 10 then
createTimer(2000, function()
+
    sender.Enabled = false
  print("Executed after 2 seconds")
+
    sender.destroy()
end)
+
  end
 +
end
 +
</pre>
  
'''One-shot timer with parameters:'''
+
===One-Shot Timer Example===
local name = "Player"
+
<pre>
local health = 100
+
createTimer(2000, function()
+
  print("Executed after 2 seconds")
createTimer(1500, function(playerName, hp)
+
end)
  print(playerName .. " health: " .. hp)
+
</pre>
end, name, health)
 
  
'''Delayed execution in sequence:'''
+
===One-Shot Timer With Parameters===
print("Starting...")
+
<pre>
createTimer(1000, function()
+
local name = "Player"
  print("Step 1: After 1 second")
+
local health = 100
end)
 
createTimer(2000, function()
 
  print("Step 2: After 2 seconds")
 
end)
 
createTimer(3000, function()
 
  print("Step 3: After 3 seconds")
 
end)
 
  
'''One-shot timer with closure:'''
+
createTimer(1500, function(playerName, hp)
local function showMessage(msg)
+
  print(playerName .. " health: " .. hp)
  createTimer(1000, function()
+
end, name, health)
    print("Delayed message: " .. msg)
+
</pre>
  end)
 
end
 
 
showMessage("Hello World")
 
 
 
'''Delayed form update:'''
 
local form = createForm()
 
form.Caption = "Loading..."
 
 
createTimer(2000, function()
 
  form.Caption = "Ready!"
 
end)
 
 
 
 
 
=== Advanced Timer Patterns ===
 
 
 
'''Countdown timer:'''
 
local countdown = 10
 
local timer = createTimer()
 
timer.Interval = 1000
 
timer.OnTimer = function()
 
  print("Countdown: " .. countdown)
 
  countdown = countdown - 1
 
  if countdown < 0 then
 
    timer.Enabled = false
 
    timer.destroy()
 
    print("Countdown complete!")
 
  end
 
end
 
 
 
'''Periodic memory check:'''
 
local timer = createTimer()
 
timer.Interval = 500
 
timer.OnTimer = function()
 
  local addr = getAddress("player.health")
 
  if addr then
 
    local health = readInteger(addr)
 
    if health < 50 then
 
      print("Warning: Low health!")
 
    end
 
  end
 
end
 
 
 
'''Auto-refresh display:'''
 
local form = createForm()
 
local label = createLabel(form)
 
label.Caption = ""
 
 
local timer = createTimer(form)
 
timer.Interval = 100
 
timer.OnTimer = function()
 
  local addr = getAddress("game.score")
 
  if addr then
 
    label.Caption = "Score: " .. readInteger(addr)
 
  end
 
end
 
 
 
'''Rate-limited action:'''
 
local canExecute = true
 
local timer = createTimer(nil, false)
 
timer.Interval = 1000
 
timer.OnTimer = function()
 
  canExecute = true
 
  timer.Enabled = false
 
end
 
 
function performAction()
 
  if canExecute then
 
    print("Action executed!")
 
    canExecute = false
 
    timer.Enabled = true
 
  else
 
    print("Please wait...")
 
  end
 
end
 
 
 
 
 
== Persistent vs One-Shot Comparison ==
 
 
 
{|width="85%" cellpadding="5%" cellspacing="0" border="1"
 
!style="width: 30%;" align="left"|Feature
 
!align="left"|Persistent Timer
 
!align="left"|One-Shot Timer
 
|-
 
|'''Creation'''
 
|createTimer() or createTimer(owner, enabled)
 
|createTimer(interval, callback, ...)
 
|-
 
|'''Behavior'''
 
|Fires repeatedly until disabled
 
|Fires once and auto-destroys
 
|-
 
|'''Cleanup'''
 
|Must call destroy() manually
 
|Destroys itself automatically
 
|-
 
|'''Control'''
 
|Can enable/disable with .Enabled
 
|Cannot control after creation
 
|-
 
|'''Callback Setup'''
 
|Set .OnTimer property after creation
 
|Callback passed during creation
 
|-
 
|'''Typical Use'''
 
|Monitoring, periodic updates, UI refresh
 
|Delays, deferred execution, timeouts
 
|}
 
  
 +
===Delayed Form Update Example===
 +
<pre>
 +
local form = createForm()
 +
form.Caption = "Loading..."
 +
form.show()
  
== Notes ==
+
createTimer(2000, function()
 +
  form.Caption = "Ready!"
 +
end)
 +
</pre>
  
* '''Interval''' is in milliseconds (1000 = 1 second)
+
===Notes===
* Timers inherit from [[Lua:Class:Component|Component]] class
+
* The interval is specified in milliseconds.
* '''One-shot timers''' automatically free themselves - do not call destroy()
+
* A value of 1000 means one second.
* '''Persistent timers''' must be destroyed manually or by owner
+
* Persistent timers keep firing until disabled or destroyed.
* Use '''getMainForm()''' as owner to auto-destroy timer when CE closes
+
* One-shot timers destroy themselves after the callback has executed.
* Timer callbacks execute in the main CE thread
+
* Do not manually destroy a one-shot timer after it has fired.
* Setting '''Enabled = false''' pauses the timer without destroying it
+
* Use an owner such as a form or getMainForm() when the timer should be cleaned up automatically.
* Timer accuracy depends on system load (~15ms resolution on Windows)
+
* 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.
  
== Common Mistakes ==
+
<pre>
 +
createTimer(1000, function()
 +
  print("This one-shot timer destroys itself")
 +
end)
 +
</pre>
  
'''Don't store one-shot timer reference:'''
+
====Forgetting To Destroy Persistent Timers====
-- WRONG: One-shot timer will self-destruct
+
Persistent timers should either have an owner or be destroyed manually when no longer needed.
local t = createTimer(1000, function() print("Hi") end)
 
-- t becomes invalid after firing
 
 
-- RIGHT: Just create and forget
 
createTimer(1000, function() print("Hi") end)
 
  
'''Don't forget to destroy persistent timers:'''
+
<pre>
-- WRONG: Memory leak if timer never destroyed
+
local timer = createTimer(getMainForm())
function startMonitoring()
 
  local t = createTimer()
 
  t.Interval = 1000
 
  t.OnTimer = function() print("Check") end
 
end
 
 
-- RIGHT: Store reference and destroy when done
 
local monitorTimer = nil
 
function startMonitoring()
 
  monitorTimer = createTimer()
 
  monitorTimer.Interval = 1000
 
  monitorTimer.OnTimer = function() print("Check") end
 
end
 
function stopMonitoring()
 
  if monitorTimer then
 
    monitorTimer.destroy()
 
    monitorTimer = nil
 
  end
 
end
 
  
 +
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]] - Blocking delay alternative
+
* [[Lua:sleep|sleep]]
* [[Lua:createThread|createThread]] - Non-blocking threading
+
* [[Lua:createThread|createThread]]
  
=== Related Classes ===
+
===Related Classes===
* [[Lua:Class:Timer|Timer]] - Timer class documentation
+
* [[Lua:Class:Timer|Timer]]
* [[Lua:Class:Component|Component]] - Base class for Timer
+
* [[Lua:Class:Component|Component]]

Revision as of 19:19, 23 June 2026

<> Lua API Reference

function createTimer(owner, enabled) : Timer

<> Lua API Reference

function createTimer(interval, callback, ...) : Timer

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.

Function Parameters

Persistent Timer

<> Lua API Reference

function createTimer(owner, enabled) : 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

<> Lua API Reference

function createTimer(interval, callback, ...) : 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

Main Pages

Core Lua documentation entry points

Lua
Script Engine

Related Functions

Related Classes