Difference between revisions of "Lua:Class:Timer"

From Cheat Engine
Jump to navigation Jump to search
(Major overhaul of the post.)
m
 
Line 1: Line 1:
 
[[Category:Lua]]
 
[[Category:Lua]]
{{CodeBox|'''class''' Timer ''':''' Component}}
+
{{Class|'''class''' Timer ''':''' Component}}
  
 
The Timer class represents a timer component.
 
The Timer class represents a timer 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