Difference between revisions of "Lua:Class:Timer"

From Cheat Engine
Jump to navigation Jump to search
m (Reverted edits by 211.13.46.219 (Talk) to last revision by Dark Byte)
m
 
(15 intermediate revisions by 4 users not shown)
Line 1: Line 1:
'''Timer Class''' (Inheritance Component->object)
+
[[Category:Lua]]
  The timer class is an non visual component that when active triggers an onTimer event every few milliseconds, based on the given interval
+
{{Class|'''class''' Timer ''':''' Component}}
  
 +
The Timer class represents a timer component.
  
'''createTimer'''(owner, enabled OPT)
+
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.
  Creates a 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
 
  
 +
===Inheritance===
 +
{|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.
 +
|}
  
'''timer_setInterval'''(timer, interval)  
+
===Creation===
  Sets the speed on how often the timer should trigger. In milliseconds (1000=1 second)
+
{{CodeBox|'''function''' createTimer(''delay'', ''function'', ''...'') ''':''' Timer}}
 +
{{CodeBox|'''function''' createTimer(''owner'', ''enabled'') ''':''' Timer}}
  
 +
Creates a Timer object.
  
'''timer_getEnabled'''(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.
  Returns true if the timer is enabled otherwise false
 
  
 +
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.
  
'''timer_setEnabled'''(timer, boolean)
+
===Function Parameters===
  Lets you enable of disable the timer
+
====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.
 +
|}
  
 +
====Persistent Timer====
 +
{|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.
 +
|}
  
'''timer_onTimer'''(timer, function)
+
===Returns===
 +
Timer — The created Timer object.
  
  function (sender)
+
===Properties===
 +
{|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.
 +
|}
  
Example 1:
+
===Methods===
 +
{|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.
 +
|}
  
   function rep()
+
===Examples===
   print "hello world"
+
<pre>
 +
local timer = createTimer(nil, false)
 +
 
 +
timer.Interval = 1000
 +
timer.OnTimer = function(timer)
 +
   print("Tick")
 +
end
 +
 
 +
timer.Enabled = true
 +
</pre>
 +
 
 +
<pre>
 +
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
 
+
end
  local t = createTimer(nil) -- it will create a Timer object and assign it to variable t.
+
</pre>
   timer_onTimer(t, rep-- The function repeat will be called every 3 seconds.
+
 
  timer_setInterval(t,3000) --Call repeat() every 3 seconds
+
<pre>
  timer_setEnabled(t, true) -- Set timer to true. If false is passed as argument then it will disable timer object.
+
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
 +
 
 +
form.show()
 +
</pre>
 +
 
 +
{{LuaSeeAlso}}

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