Difference between revisions of "Lua:Class:Timer"

From Cheat Engine
Jump to navigation Jump to search
(Related Classes)
m
 
(4 intermediate revisions by 3 users not shown)
Line 1: Line 1:
 
[[Category:Lua]]
 
[[Category:Lua]]
Timer '''class''': ('''Inheritance''': ''[[Component]]''->''[[Object]]'')
+
{{Class|'''class''' Timer ''':''' Component}}
  
The timer class is an non visual component that when active triggers an onTimer event every tick. (base on Interval property)
+
The Timer class represents a timer component.
  
== Creation ==
+
A Timer inherits from Component and Object. It can repeatedly call an OnTimer function after a given interval, or it can be created as a one-shot timer that executes a callback once and then destroys itself.
; [[createTimer]](''owner'' OPTIONAL, ''enabled'' OPTIONAL)
 
: Returns a newly created timer object.
 
: If enabled is not given it will be enabled by default (will start as soon as an onTimer event has been assigned)
 
: Owner may be nil, but you will be responsible for destroying it instead of being the responsibility of the owner object)
 
  
Tip: use [[getMainForm]] for the owner of any timer so that it is destroyed when the main form is destroyed (closed).
+
===Inheritance===
createTimer(getMainForm())
+
{|width="85%" cellpadding="10%" cellspacing="0" border="0"
 +
!align="left"|Class
 +
!align="left"|Inherits From
 +
!style="width: 80%;background-color:white;" align="left"|Description
 +
|-
 +
|Timer
 +
|Component
 +
|A timer component that can execute code after a delay or repeatedly at an interval.
 +
|-
 +
|Component
 +
|Object
 +
|Base class for components.
 +
|}
  
== Properties ==
+
===Creation===
; Interval : integer
+
{{CodeBox|'''function''' createTimer(''delay'', ''function'', ''...'') ''':''' Timer}}
: The number of milliseconds (1000 = 1 second) between ticks (executions).
+
{{CodeBox|'''function''' createTimer(''owner'', ''enabled'') ''':''' Timer}}
  
; Enabled : boolean
+
Creates a Timer object.
: The enable state of the Timer.
 
  
; OnTimer : function(timer)
+
The first form creates a one-shot timer that waits for the given delay, executes the given function, and then destroys itself. Do not use the timer object after it has fired.
: The function to call when the timer triggers (tick).
 
  
== Methods ==
+
The second form creates a persistent timer object. If ''enabled'' is omitted, it will be enabled by default and starts running as soon as an OnTimer event has been assigned. The owner may be nil, but in that case you are responsible for destroying the timer manually.
  
; getInterval() : integer
+
===Function Parameters===
: Returns the speed on how often the timer will trigger. In milliseconds (1000 = 1 second).
+
====One-Shot Timer====
 +
{|width="85%" cellpadding="10%" cellspacing="0" border="0"
 +
!align="left"|Parameter
 +
!align="left"|Type
 +
!style="width: 80%;background-color:white;" align="left"|Description
 +
|-
 +
|delay
 +
|Integer
 +
|The delay in milliseconds before the callback is executed.
 +
|-
 +
|function
 +
|Function
 +
|The function to execute when the timer fires.
 +
|-
 +
|...
 +
|Any (optional)
 +
|Additional arguments that are passed to the callback function.
 +
|}
  
; setInterval(''interval'')
+
====Persistent Timer====
: Sets the speed on how often the timer should trigger. In milliseconds (1000 = 1 second).
+
{|width="85%" cellpadding="10%" cellspacing="0" border="0"
 +
!align="left"|Parameter
 +
!align="left"|Type
 +
!style="width: 80%;background-color:white;" align="left"|Description
 +
|-
 +
|owner
 +
|Component or nil (optional)
 +
|The owner of the timer. If nil or omitted, the timer must be destroyed manually.
 +
|-
 +
|enabled
 +
|Boolean (optional)
 +
|If true, the timer is enabled. If omitted, the timer is enabled by default.
 +
|}
  
; getOnTimer() : function(''timer'')
+
===Returns===
: Returns the 'OnTimer' event.
+
Timer — The created Timer object.
  
; setOnTimer(''function(timer)'')
+
===Properties===
: Sets the 'OnTimer' event.
+
{|width="85%" cellpadding="10%" cellspacing="0" border="0"
 +
!align="left"|Property
 +
!align="left"|Type
 +
!style="width: 80%;background-color:white;" align="left"|Description
 +
|-
 +
|Interval
 +
|Integer
 +
|The number of milliseconds between timer executions. 1000 equals one second.
 +
|-
 +
|Enabled
 +
|Boolean
 +
|If true, the timer is active. If false, the timer is paused.
 +
|-
 +
|OnTimer
 +
|Function
 +
|The function to call when the timer triggers. The timer object is passed as the parameter.
 +
|}
  
; getEnabled() : boolean
+
===Methods===
: Returns the enable state of the Timer.
+
{|width="85%" cellpadding="10%" cellspacing="0" border="0"
 +
!align="left"|Method
 +
!align="left"|Return Type
 +
!style="width: 80%;background-color:white;" align="left"|Description
 +
|-
 +
|getInterval()
 +
|Integer
 +
|Returns the timer interval in milliseconds.
 +
|-
 +
|setInterval(interval)
 +
|void
 +
|Sets how often the timer should trigger, in milliseconds.
 +
|-
 +
|getOnTimer()
 +
|Function
 +
|Returns the current OnTimer function.
 +
|-
 +
|setOnTimer(function(timer))
 +
|void
 +
|Sets the function that is called when the timer triggers.
 +
|-
 +
|getEnabled()
 +
|Boolean
 +
|Returns whether the timer is currently enabled.
 +
|-
 +
|setEnabled(boolean)
 +
|void
 +
|Enables or disables the timer.
 +
|}
  
; setEnabled(''state'')
+
===Examples===
: Sets the enable state of the Timer.
+
<pre>
 +
local timer = createTimer(nil, false)
  
== Examples ==
+
timer.Interval = 1000
local timer = createTimer(getMainForm())
+
timer.OnTimer = function(timer)
timer.Interval = 100
+
  print("Tick")
timer.OnTimer = function(timer)
+
end
  -- code setting done state.
 
  if DoneState == true then
 
    timer.destroy()
 
  end
 
end
 
  
local mainForm = getMainForm()
+
timer.Enabled = true
local function timer_tick(timer)
+
</pre>
  -- code setting done state.
 
  if DoneState == true then
 
    timer.destroy()
 
  end
 
end
 
local someTimer = createTimer(mainForm, false)
 
someTimer.Interval = 100
 
someTimer.OnTimer = timer_tick
 
someTimer.Enabled = true
 
  
 +
<pre>
 +
local timer = createTimer()
 +
timer.Interval = 1000
  
{{LuaSeeAlso}}
+
local count = 0
 +
 
 +
timer.OnTimer = function(timer)
 +
  count = count + 1
 +
  print("Count: " .. tostring(count))
 +
 
 +
  if count >= 5 then
 +
    timer.Enabled = false
 +
    timer.destroy()
 +
  end
 +
end
 +
</pre>
 +
 
 +
<pre>
 +
createTimer(2000, function()
 +
  print("This runs once after 2 seconds")
 +
end)
 +
</pre>
 +
 
 +
<pre>
 +
createTimer(1500, function(name, value)
 +
  print(name .. ": " .. tostring(value))
 +
end, "Health", 100)
 +
</pre>
 +
 
 +
<pre>
 +
local form = createForm()
 +
form.Caption = "Timer Example"
 +
 
 +
local timer = createTimer(form)
 +
timer.Interval = 500
 +
timer.OnTimer = function(timer)
 +
  form.Caption = os.date("%H:%M:%S")
 +
end
  
=== Related Functions ===
+
form.show()
* [[Lua:createTimer|createTimer]]
+
</pre>
  
=== Related Classes ===
+
{{LuaSeeAlso}}
* [[Lua:Class:Component|Component]]
 

Latest revision as of 19:21, 23 June 2026

{} Class

class Timer : Component

The Timer class represents a timer component.

A Timer inherits from Component and Object. It can repeatedly call an OnTimer function after a given interval, or it can be created as a one-shot timer that executes a callback once and then destroys itself.

Inheritance[edit]

Class Inherits From Description
Timer Component A timer component that can execute code after a delay or repeatedly at an interval.
Component Object Base class for components.

Creation[edit]

<> Reference

function createTimer(delay, function, ...) : Timer

<> Reference

function createTimer(owner, enabled) : Timer

Creates a Timer object.

The first form creates a one-shot timer that waits for the given delay, executes the given function, and then destroys itself. Do not use the timer object after it has fired.

The second form creates a persistent timer object. If enabled is omitted, it will be enabled by default and starts running as soon as an OnTimer event has been assigned. The owner may be nil, but in that case you are responsible for destroying the timer manually.

Function Parameters[edit]

One-Shot Timer[edit]

Parameter Type Description
delay Integer The delay in milliseconds before the callback is executed.
function Function The function to execute when the timer fires.
... Any (optional) Additional arguments that are passed to the callback function.

Persistent Timer[edit]

Parameter Type Description
owner Component or nil (optional) The owner of the timer. If nil or omitted, the timer must be destroyed manually.
enabled Boolean (optional) If true, the timer is enabled. If omitted, the timer is enabled by default.

Returns[edit]

Timer — The created Timer object.

Properties[edit]

Property Type Description
Interval Integer The number of milliseconds between timer executions. 1000 equals one second.
Enabled Boolean If true, the timer is active. If false, the timer is paused.
OnTimer Function The function to call when the timer triggers. The timer object is passed as the parameter.

Methods[edit]

Method Return Type Description
getInterval() Integer Returns the timer interval in milliseconds.
setInterval(interval) void Sets how often the timer should trigger, in milliseconds.
getOnTimer() Function Returns the current OnTimer function.
setOnTimer(function(timer)) void Sets the function that is called when the timer triggers.
getEnabled() Boolean Returns whether the timer is currently enabled.
setEnabled(boolean) void Enables or disables the timer.

Examples[edit]

local timer = createTimer(nil, false)

timer.Interval = 1000
timer.OnTimer = function(timer)
  print("Tick")
end

timer.Enabled = true
local timer = createTimer()
timer.Interval = 1000

local count = 0

timer.OnTimer = function(timer)
  count = count + 1
  print("Count: " .. tostring(count))

  if count >= 5 then
    timer.Enabled = false
    timer.destroy()
  end
end
createTimer(2000, function()
  print("This runs once after 2 seconds")
end)
createTimer(1500, function(name, value)
  print(name .. ": " .. tostring(value))
end, "Health", 100)
local form = createForm()
form.Caption = "Timer Example"

local timer = createTimer(form)
timer.Interval = 500
timer.OnTimer = function(timer)
  form.Caption = os.date("%H:%M:%S")
end

form.show()

See Also[edit]

Main Pages