Difference between revisions of "Lua:Class:Component"

From Cheat Engine
Jump to navigation Jump to search
(Major overhaul of the post.)
 
(4 intermediate revisions by one other user not shown)
Line 1: Line 1:
 
[[Category:Lua]]
 
[[Category:Lua]]
Component '''class''': ('''Inheritance''': ''[[Object]]'')
+
{{Class|'''class''' Component ''':''' Object}}
  
Base class for components.
+
The Component class is the base class for Lua-exposed components.
 +
 
 +
Components can own other components and provide a name, tag value, owner reference, and access to their child components.
 +
 
 +
===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
 +
|-
 +
|Component
 +
|Object
 +
|Base class for Lua-exposed components.
 +
|-
 +
|Object
 +
|None
 +
|Base class for Lua-exposed objects.
 +
|}
  
 
== Properties ==
 
== Properties ==
; ComponentCount : integer : ''Readonly''
 
: Number of child components.
 
  
; Component[''index''] : Component : ''Readonly''
+
{|width="85%" cellpadding="10%" cellspacing="0" border="0"
: Array containing the child components. Starts at 0.
+
!align="left"|Property
 +
!align="left"|Type
 +
!style="width: 80%;background-color:white;" align="left"|Description
 +
|-
 +
|ComponentCount
 +
|Integer
 +
|The number of child components. Read-only.
 +
|-
 +
|Component[index]
 +
|Component
 +
|Array containing the child components. The index starts at 0. Read-only.
 +
|-
 +
|ComponentByName[name]
 +
|Component
 +
|Returns a child component by name. Read-only.
 +
|-
 +
|Name
 +
|String
 +
|The name of the component.
 +
|-
 +
|Tag
 +
|Integer
 +
|Free-to-use integer storage space. Useful for IDs or custom state.
 +
|-
 +
|Owner
 +
|Component
 +
|The owner of this component. Nil if it has no owner.
 +
|}
 +
 
 +
== 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
 +
|-
 +
|getComponentCount()
 +
|Integer
 +
|Returns the number of components attached to this component.
 +
|-
 +
|getComponent(index)
 +
|Component
 +
|Returns the child component at the specified index.
 +
|-
 +
|findComponentByName(name)
 +
|Component
 +
|Returns the child component with the specified name.
 +
|-
 +
|getName()
 +
|String
 +
|Returns the component name.
 +
|-
 +
|setName(newname)
 +
|void
 +
|Changes the component name.
 +
|-
 +
|getTag()
 +
|Integer
 +
|Returns the component tag value.
 +
|-
 +
|setTag(tagvalue)
 +
|void
 +
|Sets the component tag value.
 +
|-
 +
|getOwner()
 +
|Component
 +
|Returns the owner of this component.
 +
|}
 +
 
 +
== Examples ==
 +
 
 +
===Read the component count===
 +
<syntaxhighlight lang="lua" line highlight="5,6">
 +
local form = createForm(false)
 +
 
 +
createButton(form)
 +
createLabel(form)
 +
 
 +
print("ComponentCount: " .. tostring(form.ComponentCount))
 +
print("getComponentCount(): " .. tostring(form.getComponentCount()))
 +
 
 +
form.destroy()
 +
</syntaxhighlight>
 +
 
 +
===Access child components by index===
 +
<syntaxhighlight lang="lua" line highlight="7,8">
 +
local form = createForm(false)
 +
 
 +
local button = createButton(form)
 +
button.Name = "ButtonA"
 +
 
 +
if form.ComponentCount > 0 then
 +
  local component = form.Component[0]
 +
  print(component.Name)
 +
end
 +
 
 +
form.destroy()
 +
</syntaxhighlight>
 +
 
 +
===Enumerate child components===
 +
<syntaxhighlight lang="lua" line highlight="8,9">
 +
local form = createForm(false)
 +
 
 +
createButton(form).Name = "ButtonA"
 +
createLabel(form).Name = "LabelA"
 +
 
 +
for i = 0, form.ComponentCount - 1 do
 +
  local component = form.getComponent(i)
 +
  print(i .. ": " .. component.Name)
 +
end
 +
 
 +
form.destroy()
 +
</syntaxhighlight>
 +
 
 +
===Find a component by name===
 +
<syntaxhighlight lang="lua" line highlight="7,8">
 +
local form = createForm(false)
 +
 
 +
local button = createButton(form)
 +
button.Name = "ApplyButton"
 +
 
 +
local byProperty = form.ComponentByName["ApplyButton"]
 +
local byMethod = form.findComponentByName("ApplyButton")
 +
 
 +
print(byProperty == byMethod)
  
; ComponentByName[''name''] &#58; Component &#58; ''Readonly''
+
form.destroy()
: Returns a component based on the name.
+
</syntaxhighlight>
  
; Name &#58; string
+
===Read and change the component name===
: The name of the component.
+
<syntaxhighlight lang="lua" line highlight="4,6,8">
 +
local form = createForm(false)
 +
local button = createButton(form)
  
; Tag &#58; integer
+
button.Name = "OldName"
: Free to use storage space. (Usefull for id's)
 
  
; Owner &#58; Component
+
print(button.getName())
: Returns the owner of this object. Nil if it has none.
 
  
== Methods ==
+
button.setName("NewName")
; getComponentCount() &#58; integer
+
 
: Returns the number of components attached to his component.
+
print(button.Name)
 +
 
 +
form.destroy()
 +
</syntaxhighlight>
 +
 
 +
===Use Tag as custom integer storage===
 +
<syntaxhighlight lang="lua" line highlight="4,5,7,8">
 +
local form = createForm(false)
 +
local button = createButton(form)
 +
 
 +
button.Tag = 1001
 +
print(button.Tag)
 +
 
 +
button.setTag(2002)
 +
print(button.getTag())
 +
 
 +
form.destroy()
 +
</syntaxhighlight>
  
; getComponent(''index'') &#58; Component
+
===Get the owner of a component===
: Returns the specific component.
+
<syntaxhighlight lang="lua" line highlight="4,6">
 +
local form = createForm(false)
 +
local button = createButton(form)
  
; findComponentByName(''name'') &#58; Component
+
local owner = button.Owner
: Returns the component with this name.
 
  
; getName() &#58; string
+
if owner == button.getOwner() then
: Return the name.
+
  print("Owner: " .. tostring(owner.Name))
 +
end
  
; setName(''newName'')
+
form.destroy()
: Changes the name.
+
</syntaxhighlight>
  
; getTag() &#58; integer
+
===Check whether a component has an owner===
: Get the tag value.
+
<syntaxhighlight lang="lua" line highlight="4">
 +
local form = createForm(false)
 +
local button = createButton(form)
  
; setTag(''tagValue'')
+
if button.getOwner() ~= nil then
: Sets an integer value. You can use this for ID's.
+
  print("The button has an owner")
 +
end
  
; getOwner() &#58; Component
+
form.destroy()
: Returns the owner of this component
+
</syntaxhighlight>
  
 
{{LuaSeeAlso}}
 
{{LuaSeeAlso}}
  
=== Related Functions ===
+
{{Forms}}
* [[createClass]]
 
* [[inheritsFromObject]]
 
* [[inheritsFromComponent]]
 
* [[inheritsFromControl]]
 
* [[inheritsFromWinControl]]
 
 
 
=== Related Classes ===
 
* [[Object]]
 
* [[Control]]
 
* [[WinControl]]
 
* [[Application]]
 
* [[Form]]
 

Latest revision as of 19:59, 26 June 2026

{} Class

class Component : Object

The Component class is the base class for Lua-exposed components.

Components can own other components and provide a name, tag value, owner reference, and access to their child components.

Inheritance[edit]

Class Inherits From Description
Component Object Base class for Lua-exposed components.
Object None Base class for Lua-exposed objects.

Properties[edit]

Property Type Description
ComponentCount Integer The number of child components. Read-only.
Component[index] Component Array containing the child components. The index starts at 0. Read-only.
ComponentByName[name] Component Returns a child component by name. Read-only.
Name String The name of the component.
Tag Integer Free-to-use integer storage space. Useful for IDs or custom state.
Owner Component The owner of this component. Nil if it has no owner.

Methods[edit]

Method Return Type Description
getComponentCount() Integer Returns the number of components attached to this component.
getComponent(index) Component Returns the child component at the specified index.
findComponentByName(name) Component Returns the child component with the specified name.
getName() String Returns the component name.
setName(newname) void Changes the component name.
getTag() Integer Returns the component tag value.
setTag(tagvalue) void Sets the component tag value.
getOwner() Component Returns the owner of this component.

Examples[edit]

Read the component count[edit]

1 local form = createForm(false)
2 
3 createButton(form)
4 createLabel(form)
5 
6 print("ComponentCount: " .. tostring(form.ComponentCount))
7 print("getComponentCount(): " .. tostring(form.getComponentCount()))
8 
9 form.destroy()

Access child components by index[edit]

 1 local form = createForm(false)
 2 
 3 local button = createButton(form)
 4 button.Name = "ButtonA"
 5 
 6 if form.ComponentCount > 0 then
 7   local component = form.Component[0]
 8   print(component.Name)
 9 end
10 
11 form.destroy()

Enumerate child components[edit]

 1 local form = createForm(false)
 2 
 3 createButton(form).Name = "ButtonA"
 4 createLabel(form).Name = "LabelA"
 5 
 6 for i = 0, form.ComponentCount - 1 do
 7   local component = form.getComponent(i)
 8   print(i .. ": " .. component.Name)
 9 end
10 
11 form.destroy()

Find a component by name[edit]

 1 local form = createForm(false)
 2 
 3 local button = createButton(form)
 4 button.Name = "ApplyButton"
 5 
 6 local byProperty = form.ComponentByName["ApplyButton"]
 7 local byMethod = form.findComponentByName("ApplyButton")
 8 
 9 print(byProperty == byMethod)
10 
11 form.destroy()

Read and change the component name[edit]

 1 local form = createForm(false)
 2 local button = createButton(form)
 3 
 4 button.Name = "OldName"
 5 
 6 print(button.getName())
 7 
 8 button.setName("NewName")
 9 
10 print(button.Name)
11 
12 form.destroy()

Use Tag as custom integer storage[edit]

 1 local form = createForm(false)
 2 local button = createButton(form)
 3 
 4 button.Tag = 1001
 5 print(button.Tag)
 6 
 7 button.setTag(2002)
 8 print(button.getTag())
 9 
10 form.destroy()

Get the owner of a component[edit]

 1 local form = createForm(false)
 2 local button = createButton(form)
 3 
 4 local owner = button.Owner
 5 
 6 if owner == button.getOwner() then
 7   print("Owner: " .. tostring(owner.Name))
 8 end
 9 
10 form.destroy()

Check whether a component has an owner[edit]

1 local form = createForm(false)
2 local button = createButton(form)
3 
4 if button.getOwner() ~= nil then
5   print("The button has an owner")
6 end
7 
8 form.destroy()

Main Pages

Core Lua documentation entry points

Lua
Script Engine

Form Related Classes