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) :...")
 
m (Added related function template.)
 
(2 intermediate revisions by the same user not shown)
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"
=== Persistent Timer Examples ===
+
!align="left"|Mode
 
+
!align="left"|Syntax
'''Basic recurring timer (runs every second):'''
+
!style="width: 80%;background-color:white;" align="left"|Description
local timer = createTimer()
+
|-
timer.Interval = 1000  -- 1 second
+
|Persistent Timer
timer.OnTimer = function()
+
|createTimer(owner, enabled)
  print("Tick: " .. os.date("%H:%M:%S"))
+
|Creates a timer that fires repeatedly until it is disabled or destroyed.
end
+
|-
 
+
|One-Shot Timer
'''Timer with owner (destroyed when form closes):'''
+
|createTimer(interval, callback, ...)
local form = createForm()
+
|Creates a timer that fires once, executes the callback, and then destroys itself.
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):'''
 
local timer = createTimer(getMainForm())
 
timer.Interval = 1000
 
timer.OnTimer = function()
 
  print("Running until CE closes")
 
end
 
  
'''Create disabled timer and enable later:'''
+
===Persistent Timer Example===
local timer = createTimer(nil, false)
+
<syntaxhighlight lang="lua" line>
timer.Interval = 2000
+
local timer = createTimer(nil, false)
timer.OnTimer = function()
 
  print("2 seconds passed")
 
end
 
-- Enable when needed
 
timer.Enabled = true
 
  
'''Manual cleanup when done:'''
+
timer.Interval = 1000
local timer = createTimer()
+
timer.OnTimer = function(sender)
timer.Interval = 100
+
  print("Tick: " .. os.date("%H:%M:%S"))
local count = 0
+
end
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:'''
+
timer.Enabled = true
local timer = createTimer()
+
</syntaxhighlight>
timer.Interval = 1000
 
timer.Enabled = false
 
timer.OnTimer = function()
 
  print("Active!")
 
end
 
 
-- Start
 
timer.Enabled = true
 
-- Stop
 
timer.Enabled = false
 
  
 +
===Timer With Owner Example===
 +
<syntaxhighlight lang="lua" line>
 +
local form = createForm()
 +
form.Caption = "Timer Example"
  
=== One-Shot Timer Examples ===
+
local timer = createTimer(form)
 +
timer.Interval = 500
 +
timer.OnTimer = function(sender)
 +
  form.Caption = "Time: " .. os.date("%H:%M:%S")
 +
end
  
'''Simple delay (auto-destroys after firing):'''
+
form.show()
createTimer(2000, function()
+
</syntaxhighlight>
  print("Executed after 2 seconds")
 
end)
 
  
'''One-shot timer with parameters:'''
+
===Stopping A Persistent Timer===
local name = "Player"
+
<syntaxhighlight lang="lua" line>
local health = 100
+
local timer = createTimer()
+
timer.Interval = 1000
createTimer(1500, function(playerName, hp)
 
  print(playerName .. " health: " .. hp)
 
end, name, health)
 
  
'''Delayed execution in sequence:'''
+
local count = 0
print("Starting...")
 
createTimer(1000, function()
 
  print("Step 1: After 1 second")
 
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:'''
+
timer.OnTimer = function(sender)
local function showMessage(msg)
+
  count = count + 1
  createTimer(1000, function()
+
  print("Count: " .. count)
    print("Delayed message: " .. msg)
 
  end)
 
end
 
 
showMessage("Hello World")
 
  
'''Delayed form update:'''
+
  if count >= 10 then
local form = createForm()
+
    sender.Enabled = false
form.Caption = "Loading..."
+
    sender.destroy()
+
  end
createTimer(2000, function()
+
end
  form.Caption = "Ready!"
+
</syntaxhighlight>
end)
 
  
 +
===One-Shot Timer Example===
 +
<syntaxhighlight lang="lua" line>
 +
createTimer(2000, function()
 +
  print("Executed after 2 seconds")
 +
end)
 +
</syntaxhighlight>
  
=== Advanced Timer Patterns ===
+
===One-Shot Timer With Parameters===
 +
<syntaxhighlight lang="lua" line>
 +
local name = "Player"
 +
local health = 100
  
'''Countdown timer:'''
+
createTimer(1500, function(playerName, hp)
local countdown = 10
+
  print(playerName .. " health: " .. hp)
local timer = createTimer()
+
end, name, health)
timer.Interval = 1000
+
</syntaxhighlight>
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:'''
+
===Delayed Form Update Example===
local timer = createTimer()
+
<syntaxhighlight lang="lua" line>
timer.Interval = 500
+
local form = createForm()
timer.OnTimer = function()
+
form.Caption = "Loading..."
  local addr = getAddress("player.health")
+
form.show()
  if addr then
 
    local health = readInteger(addr)
 
    if health < 50 then
 
      print("Warning: Low health!")
 
    end
 
  end
 
end
 
  
'''Auto-refresh display:'''
+
createTimer(2000, function()
local form = createForm()
+
  form.Caption = "Ready!"
local label = createLabel(form)
+
end)
label.Caption = ""
+
</syntaxhighlight>
 
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:'''
+
===Notes===
local canExecute = true
+
* The interval is specified in milliseconds.
local timer = createTimer(nil, false)
+
* A value of 1000 means one second.
timer.Interval = 1000
+
* Persistent timers keep firing until disabled or destroyed.
timer.OnTimer = function()
+
* One-shot timers destroy themselves after the callback has executed.
  canExecute = true
+
* Do not manually destroy a one-shot timer after it has fired.
  timer.Enabled = false
+
* Use an owner such as a form or getMainForm() when the timer should be cleaned up automatically.
end
+
* Timer callbacks execute in Cheat Engine's main thread.
+
* Timer accuracy depends on system load and the operating system timer resolution.
function performAction()
 
  if canExecute then
 
    print("Action executed!")
 
    canExecute = false
 
    timer.Enabled = true
 
  else
 
    print("Please wait...")
 
  end
 
end
 
  
 +
===Common Mistakes===
 +
====Destroying One-Shot Timers====
 +
One-shot timers destroy themselves automatically after firing. Usually, you should create them and let them run.
  
== Persistent vs One-Shot Comparison ==
+
<syntaxhighlight lang="lua" line>
 +
createTimer(1000, function()
 +
  print("This one-shot timer destroys itself")
 +
end)
 +
</syntaxhighlight>
  
{|width="85%" cellpadding="5%" cellspacing="0" border="1"
+
====Forgetting To Destroy Persistent Timers====
!style="width: 30%;" align="left"|Feature
+
Persistent timers should either have an owner or be destroyed manually when no longer needed.
!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
 
|}
 
  
 +
<syntaxhighlight lang="lua" line>
 +
local timer = createTimer(getMainForm())
  
== Notes ==
+
timer.Interval = 1000
 
+
timer.OnTimer = function(sender)
* '''Interval''' is in milliseconds (1000 = 1 second)
+
  print("This timer is owned by the main form")
* Timers inherit from [[Lua:Class:Component|Component]] class
+
end
* '''One-shot timers''' automatically free themselves - do not call destroy()
+
</syntaxhighlight>
* '''Persistent timers''' must be destroyed manually or by owner
 
* Use '''getMainForm()''' as owner to auto-destroy timer when CE closes
 
* Timer callbacks execute in the main CE thread
 
* Setting '''Enabled = false''' pauses the timer without destroying it
 
* Timer accuracy depends on system load (~15ms resolution on Windows)
 
 
 
 
 
== Common Mistakes ==
 
 
 
'''Don't store one-shot timer reference:'''
 
-- WRONG: One-shot timer will self-destruct
 
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:'''
 
-- WRONG: Memory leak if timer never destroyed
 
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
 
 
 
  
 
{{LuaSeeAlso}}
 
{{LuaSeeAlso}}
  
=== Related Functions ===
+
{{Creation}}
* [[Lua:sleep|sleep]] - Blocking delay alternative
 
* [[Lua:createThread|createThread]] - Non-blocking threading
 
 
 
=== Related Classes ===
 
* [[Lua:Class:Timer|Timer]] - Timer class documentation
 
* [[Lua:Class:Component|Component]] - Base class for Timer
 

Latest revision as of 23:55, 26 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[edit]

Persistent Timer[edit]

<> 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[edit]

<> 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[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

Main Pages

Core Lua documentation entry points

Lua
Script Engine

Basic Visual Controls

Text / Input Controls

Value / Progress Controls

Graphics / Resources

Strings / Streams

Timers / Hotkeys

Scanning / Found Lists

Other Creation Helpers