Difference between revisions of "Lua:Class:Component"

From Cheat Engine
Jump to navigation Jump to search
(Major overhaul of the post.)
 
(7 intermediate revisions by one other user not shown)
Line 1: Line 1:
Component '''class''': ('''Inheritance''': ''[[Object]]'')
+
[[Category:Lua]]
 +
{{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[int] : 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)
 +
 
 +
form.destroy()
 +
</syntaxhighlight>
 +
 
 +
===Read and change the component name===
 +
<syntaxhighlight lang="lua" line highlight="4,6,8">
 +
local form = createForm(false)
 +
local button = createButton(form)
  
; ComponentByName[string] &#58; Component &#58; ''Readonly''
+
button.Name = "OldName"
: Returns a component based on the name.
 
  
; Name &#58; string
+
print(button.getName())
: The name of the component.
 
  
; Tag &#58; integer
+
button.setName("NewName")
: Free to use storage space. (Usefull for id's)
 
  
; Owner &#58; Component
+
print(button.Name)
: Returns the owner of this object. Nil if it has none.
 
  
== Methods ==
+
form.destroy()
; getComponentCount() &#58; integer
+
</syntaxhighlight>
: Returns the number of components attached to his component.
+
 
 +
===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())
  
; getComponent(index) &#58; Component
+
form.destroy()
: Returns the specific component.
+
</syntaxhighlight>
  
; findComponentByName(name) &#58; Component
+
===Get the owner of a component===
: Returns the component with this name.
+
<syntaxhighlight lang="lua" line highlight="4,6">
 +
local form = createForm(false)
 +
local button = createButton(form)
  
; getName() &#58; string
+
local owner = button.Owner
: Return the name.
 
  
; setName(newname)
+
if owner == button.getOwner() then
: Changes the name.
+
  print("Owner: " .. tostring(owner.Name))
 +
end
  
; getTag() &#58; integer
+
form.destroy()
: Get the tag value.
+
</syntaxhighlight>
  
; setTag(tagvalue)
+
===Check whether a component has an owner===
: Sets an integer value. You can use this for ID's.
+
<syntaxhighlight lang="lua" line highlight="4">
 +
local form = createForm(false)
 +
local button = createButton(form)
  
; getOwner() &#58; Component
+
if button.getOwner() ~= nil then
: Returns the owner of this component
+
  print("The button has an owner")
 +
end
  
== See also ==
+
form.destroy()
* [[Lua]]
+
</syntaxhighlight>
* [[Help_File:Script engine|Script engine]]
 
  
=== Related Functions ===
+
{{LuaSeeAlso}}
* [[createClass]]
 
* [[inheritsFromObject]]
 
* [[inheritsFromComponent]]
 
* [[inheritsFromControl]]
 
* [[inheritsFromWinControl]]
 
  
=== Related Classes ===
+
{{Forms}}
* [[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