Difference between revisions of "Lua:Class:Strings"

From Cheat Engine
Jump to navigation Jump to search
(filled out a proper class template for the Strings class)
 
(Major overhaul of the post.)
Line 1: Line 1:
 
[[Category:Lua]]
 
[[Category:Lua]]
Strings '''class''': ('''Inheritance''': ''[[Lua:Class:Object|Object]]'')
+
{{Class|'''class''' Strings ''':''' Object}}
  
Mostly an abstract class.
+
The Strings class represents a list of strings.
  
== Properties ==
+
A Strings object inherits from Object. It is mostly an abstract base class and is commonly used by other objects that expose string-list behavior.
; Text : string
 
: All the strings as one string.
 
  
; Count : number
+
===Inheritance===
: The number of strings in this list.
+
{|width="85%" cellpadding="10%" cellspacing="0" border="0"
 +
!align="left"|Class
 +
!align="left"|Inherits From
 +
!style="width: 80%;background-color:white;" align="left"|Description
 +
|-
 +
|Strings
 +
|Object
 +
|A string-list object. Mostly used as an abstract base class.
 +
|}
  
; String[] : string
+
===Properties===
: Array to access one specific string in the list.
+
{|width="100%" cellpadding="10%" cellspacing="0" border="0"
 +
!align="left"|Property
 +
!align="left"|Type
 +
!style="width: 80%;background-color:white;" align="left"|Description
 +
|-
 +
|LineBreak
 +
|String
 +
|The character or characters to count as a line break.
 +
|-
 +
|Text
 +
|String
 +
|All strings in the list as one string.
 +
|-
 +
|Count
 +
|Integer
 +
|The number of strings in the list.
 +
|-
 +
|String[]
 +
|String
 +
|Array access to a specific string in the list.
 +
|-
 +
|Data[]
 +
|Integer
 +
|Array access to the data value of a specific string in the list.
 +
|-
 +
|[]
 +
|String
 +
|Alias for String[].
 +
|}
  
== Methods ==
+
===Methods===
; clear()
+
{|width="100%" cellpadding="10%" cellspacing="0" border="0"
: Deletes all strings in the list.
+
!align="left"|Method
 +
!align="left"|Return Type
 +
!style="width: 70%;background-color:white;" align="left"|Description
 +
|-
 +
|clear()
 +
|void
 +
|Deletes all strings in the list.
 +
|-
 +
|add(string, data)
 +
|Integer
 +
|Adds a string to the list and optionally associates an integer data value with it. Returns the index of the added string.
 +
|-
 +
|addText(strings)
 +
|void
 +
|Adds multiple strings at once.
 +
|-
 +
|delete(index)
 +
|void
 +
|Deletes the string at the specified index.
 +
|-
 +
|getText()
 +
|String
 +
|Returns all strings as one large string.
 +
|-
 +
|setText(string)
 +
|void
 +
|Sets the strings of the object to the given text. The input can be multiline.
 +
|-
 +
|indexOf(string)
 +
|Integer
 +
|Returns the index of the specified string. Returns -1 if it is not found.
 +
|-
 +
|insert(index, string)
 +
|void
 +
|Inserts a string at the specified index and moves the following items down.
 +
|-
 +
|getCount()
 +
|Integer
 +
|Returns the number of strings in the list.
 +
|-
 +
|remove(string)
 +
|void
 +
|Removes the given string from the list.
 +
|-
 +
|loadFromFile(filename, ignoreencoding)
 +
|void
 +
|Loads the strings from a text file. If ignoreencoding is false, the file is loaded using the best encoding the loader can guess. The default for ignoreencoding is true.
 +
|-
 +
|saveToFile(filename)
 +
|void
 +
|Saves the strings to a text file.
 +
|-
 +
|getString(index)
 +
|String
 +
|Returns the string at the given index.
 +
|-
 +
|setString(index, string)
 +
|void
 +
|Replaces the string at the given index.
 +
|-
 +
|getData(index)
 +
|Integer
 +
|Returns the integer data value stored for the string at the given index.
 +
|-
 +
|setData(index, integer)
 +
|void
 +
|Sets the integer data value stored for the string at the given index.
 +
|-
 +
|beginUpdate()
 +
|void
 +
|Stops updates from triggering other events. This can prevent flashing while multiple changes are being made.
 +
|-
 +
|endUpdate()
 +
|void
 +
|Ends an update block started with beginUpdate().
 +
|}
  
; add(string)
+
===Examples===
: Adds a string to the list.
+
<pre>
 +
local strings = createStringList()
  
; delete(index)
+
strings.add("First")
: Deletes a string from the list at the index given.
+
strings.add("Second")
 +
strings.add("Third")
  
; getText() &#58; string
+
print("Count: " .. tostring(strings.Count))
: Returns all the strings as one string.
+
print("First item: " .. strings[0])
  
; setText()
+
strings.destroy()
: Sets the strings of the list to the given text (can be multi-line).
+
</pre>
  
; indexOf(string) &#58; index
+
<pre>
: Returns the index of the first instance of the specified string.
+
local strings = createStringList()
: Returns -1 if not found.
 
  
; insert(index, string)
+
strings.add("Health", 100)
: Inserts a string at a specific spot moving the items after it.
+
strings.add("Mana", 50)
  
; getCount() &#58; number
+
print(strings.getString(0))
: Returns the number of strings in the list.
+
print(strings.getData(0))
  
; remove(string)
+
strings.setData(0, 150)
: Removes the first instance of the given string from the list.
 
  
; loadFromFile(filename) &#58; boolean
+
print(strings.Data[0])
: Loads the strings from a text file.
 
  
; saveToFile(filename) &#58; boolean
+
strings.destroy()
: Saves the strings to a text file.
+
</pre>
  
; getString(index) &#58; string
+
<pre>
: Gets the string at the given index.
+
local strings = createStringList()
  
; setString(index, string)
+
strings.Text = [[First line
: Replaces the string at the given index.
+
Second line
 +
Third line]]
 +
 
 +
print(strings.getText())
 +
 
 +
strings.destroy()
 +
</pre>
 +
 
 +
<pre>
 +
local strings = createStringList()
 +
 
 +
strings.beginUpdate()
 +
 
 +
for i = 1, 100 do
 +
  strings.add("Item " .. tostring(i))
 +
end
 +
 
 +
strings.endUpdate()
 +
 
 +
print("Items added: " .. tostring(strings.Count))
 +
 
 +
strings.destroy()
 +
</pre>
 +
 
 +
<pre>
 +
local strings = createStringList()
 +
 
 +
strings.add("Line 1")
 +
strings.add("Line 2")
 +
 
 +
strings.saveToFile("C:\\\\Temp\\\\strings.txt")
 +
strings.clear()
 +
strings.loadFromFile("C:\\\\Temp\\\\strings.txt")
 +
 
 +
print(strings.Text)
 +
 
 +
strings.destroy()
 +
</pre>
 +
 
 +
===Notes===
 +
* Strings is mostly an abstract base class.
 +
* Objects that expose string-list behavior may use this class or a descendant of it.
 +
* Index-based access starts at 0.
 +
* Use beginUpdate() and endUpdate() when making many changes at once.
 +
* When using standalone string-list objects, destroy them when they are no longer needed.
  
 
{{LuaSeeAlso}}
 
{{LuaSeeAlso}}
 
=== Related Classes ===
 
* [[Lua:Class:Object|Object]]
 

Revision as of 00:30, 25 June 2026

{} Class

class Strings : Object

The Strings class represents a list of strings.

A Strings object inherits from Object. It is mostly an abstract base class and is commonly used by other objects that expose string-list behavior.

Inheritance

Class Inherits From Description
Strings Object A string-list object. Mostly used as an abstract base class.

Properties

Property Type Description
LineBreak String The character or characters to count as a line break.
Text String All strings in the list as one string.
Count Integer The number of strings in the list.
String[] String Array access to a specific string in the list.
Data[] Integer Array access to the data value of a specific string in the list.
[] String Alias for String[].

Methods

Method Return Type Description
clear() void Deletes all strings in the list.
add(string, data) Integer Adds a string to the list and optionally associates an integer data value with it. Returns the index of the added string.
addText(strings) void Adds multiple strings at once.
delete(index) void Deletes the string at the specified index.
getText() String Returns all strings as one large string.
setText(string) void Sets the strings of the object to the given text. The input can be multiline.
indexOf(string) Integer Returns the index of the specified string. Returns -1 if it is not found.
insert(index, string) void Inserts a string at the specified index and moves the following items down.
getCount() Integer Returns the number of strings in the list.
remove(string) void Removes the given string from the list.
loadFromFile(filename, ignoreencoding) void Loads the strings from a text file. If ignoreencoding is false, the file is loaded using the best encoding the loader can guess. The default for ignoreencoding is true.
saveToFile(filename) void Saves the strings to a text file.
getString(index) String Returns the string at the given index.
setString(index, string) void Replaces the string at the given index.
getData(index) Integer Returns the integer data value stored for the string at the given index.
setData(index, integer) void Sets the integer data value stored for the string at the given index.
beginUpdate() void Stops updates from triggering other events. This can prevent flashing while multiple changes are being made.
endUpdate() void Ends an update block started with beginUpdate().

Examples

local strings = createStringList()

strings.add("First")
strings.add("Second")
strings.add("Third")

print("Count: " .. tostring(strings.Count))
print("First item: " .. strings[0])

strings.destroy()
local strings = createStringList()

strings.add("Health", 100)
strings.add("Mana", 50)

print(strings.getString(0))
print(strings.getData(0))

strings.setData(0, 150)

print(strings.Data[0])

strings.destroy()
local strings = createStringList()

strings.Text = [[First line
Second line
Third line]]

print(strings.getText())

strings.destroy()
local strings = createStringList()

strings.beginUpdate()

for i = 1, 100 do
  strings.add("Item " .. tostring(i))
end

strings.endUpdate()

print("Items added: " .. tostring(strings.Count))

strings.destroy()
local strings = createStringList()

strings.add("Line 1")
strings.add("Line 2")

strings.saveToFile("C:\\\\Temp\\\\strings.txt")
strings.clear()
strings.loadFromFile("C:\\\\Temp\\\\strings.txt")

print(strings.Text)

strings.destroy()

Notes

  • Strings is mostly an abstract base class.
  • Objects that expose string-list behavior may use this class or a descendant of it.
  • Index-based access starts at 0.
  • Use beginUpdate() and endUpdate() when making many changes at once.
  • When using standalone string-list objects, destroy them when they are no longer needed.

Main Pages

Core Lua documentation entry points

Lua
Script Engine