Difference between revisions of "Lua:Class:Component"
Jump to navigation
Jump to search
(→Related Functions) |
(Major overhaul of the post.) |
||
| (2 intermediate revisions by one other user not shown) | |||
| Line 1: | Line 1: | ||
[[Category:Lua]] | [[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 == | ||
| − | |||
| − | |||
| − | ; Component[ | + | {|width="85%" cellpadding="10%" cellspacing="0" border="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) | ||
| − | + | button.Name = "OldName" | |
| − | |||
| − | + | print(button.getName()) | |
| − | |||
| − | == | + | button.setName("NewName") |
| − | + | ||
| − | + | 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> | ||
| − | + | ===Get the owner of a component=== | |
| − | + | <syntaxhighlight lang="lua" line highlight="4,6"> | |
| + | local form = createForm(false) | ||
| + | local button = createButton(form) | ||
| − | + | local owner = button.Owner | |
| − | |||
| − | + | if owner == button.getOwner() then | |
| − | : | + | print("Owner: " .. tostring(owner.Name)) |
| + | end | ||
| − | + | form.destroy() | |
| − | + | </syntaxhighlight> | |
| − | + | ===Check whether a component has an owner=== | |
| − | + | <syntaxhighlight lang="lua" line highlight="4"> | |
| + | local form = createForm(false) | ||
| + | local button = createButton(form) | ||
| − | + | if button.getOwner() ~= nil then | |
| − | + | print("The button has an owner") | |
| + | end | ||
| − | + | form.destroy() | |
| − | + | </syntaxhighlight> | |
{{LuaSeeAlso}} | {{LuaSeeAlso}} | ||
| − | + | {{Forms}} | |
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
Latest revision as of 19:59, 26 June 2026
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.
Contents
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()