Difference between revisions of "Lua:registerCustomTypeLua"

From Cheat Engine
Jump to navigation Jump to search
m
 
(7 intermediate revisions by 5 users not shown)
Line 1: Line 1:
 
[[Category:Lua]]
 
[[Category:Lua]]
'''function''' registerCustomTypeLua(''typename'', ''bytecount'', ''bytestovaluefunction'', ''valuetobytesfunction'')
+
'''function''' registerCustomTypeLua(''TypeName'', ''ByteCount'', ''BytesToValueFunction'', ''ValueToBytesFunction'', [''IsFloat'']) ''':''' CustomType
  
Registers a custom variable type based on lua scripts
+
Registers a custom value type for Cheat Engine memory records, using Lua functions for conversion between bytes and values.
 
 
The bytes to value function should be defined as "function bytestovalue (b1,b2,b3,b4)" and return an integer as result
 
 
 
The value to bytes function should be defined as "function valuetobytes (integer)" and return the bytes it should write
 
  
 +
The bytes-to-value function should be defined as <code>function bytestovalue(b1, b2, ...)</code> and return the interpreted value. 
 +
The value-to-bytes function should be defined as <code>function valuetobytes(value)</code> and return the bytes to write (as multiple return values).
  
 
===Function Parameters===
 
===Function Parameters===
{|width="85%" cellpadding="10%" cellpadding="5%" cellspacing="0" border="0"
+
{|width="85%" cellpadding="10%" cellspacing="0" border="0"
 
!align="left"|Parameter
 
!align="left"|Parameter
 
!align="left"|Type
 
!align="left"|Type
 
!style="width: 80%;background-color:white;" align="left"|Description
 
!style="width: 80%;background-color:white;" align="left"|Description
 
|-
 
|-
|typename
+
|TypeName
|string
+
|String
|Name of the custom type
+
|The name of the custom type.
 +
|-
 +
|ByteCount
 +
|Integer
 +
|The number of bytes this type occupies.
 
|-
 
|-
|bytecount
+
|BytesToValueFunction
|integer
+
|Function
|The number of bytes this variable contains
+
|A Lua function that takes <code>ByteCount</code> arguments and returns the interpreted value.
 
|-
 
|-
|bytestovaluefunction
+
|ValueToBytesFunction
|function
+
|Function
|Declared as function (b1,b2,b3,b4,...). Must return an integer representing what those bytes mean
+
|A Lua function that takes a value and returns <code>ByteCount</code> bytes (as multiple return values).
 
|-
 
|-
|valuetobytesfunction
+
|IsFloat
|function
+
|Boolean (optional)
|Declared as function (integer). Must return a row of bytes representing the given integer
+
|Set to true if the type should be treated as a floating point value.
 
|}
 
|}
  
== See also ==
+
===Returns===
* [[registerCustomTypeAutoAssembler]]
+
CustomType — The registered custom type object.
* [[Lua]]
+
 
 +
===Examples===
 +
<pre>
 +
-- Example: Register a 3-byte integer type
 +
function myBytesToValue(b1, b2, b3)
 +
  return b1 + (b2 << 8) + (b3 << 16)
 +
end
 +
 
 +
function myValueToBytes(val)
 +
  local b1 = val & 0xFF
 +
  local b2 = (val >> 8) & 0xFF
 +
  local b3 = (val >> 16) & 0xFF
 +
  return b1, b2, b3
 +
end
 +
 
 +
local myType = registerCustomTypeLua("3ByteInt", 3, myBytesToValue, myValueToBytes)
 +
</pre>
 +
 
 +
{{LuaSeeAlso}}
 +
 
 +
=== Related Functions ===
 +
* [[Lua:registerCustomTypeAutoAssembler|registerCustomTypeAutoAssembler]]
 +
* [[Lua:getCustomType|getCustomType]]

Latest revision as of 18:41, 11 July 2025

function registerCustomTypeLua(TypeName, ByteCount, BytesToValueFunction, ValueToBytesFunction, [IsFloat]) : CustomType

Registers a custom value type for Cheat Engine memory records, using Lua functions for conversion between bytes and values.

The bytes-to-value function should be defined as function bytestovalue(b1, b2, ...) and return the interpreted value. The value-to-bytes function should be defined as function valuetobytes(value) and return the bytes to write (as multiple return values).

Function Parameters[edit]

Parameter Type Description
TypeName String The name of the custom type.
ByteCount Integer The number of bytes this type occupies.
BytesToValueFunction Function A Lua function that takes ByteCount arguments and returns the interpreted value.
ValueToBytesFunction Function A Lua function that takes a value and returns ByteCount bytes (as multiple return values).
IsFloat Boolean (optional) Set to true if the type should be treated as a floating point value.

Returns[edit]

CustomType — The registered custom type object.

Examples[edit]

-- Example: Register a 3-byte integer type
function myBytesToValue(b1, b2, b3)
  return b1 + (b2 << 8) + (b3 << 16)
end

function myValueToBytes(val)
  local b1 = val & 0xFF
  local b2 = (val >> 8) & 0xFF
  local b3 = (val >> 16) & 0xFF
  return b1, b2, b3
end

local myType = registerCustomTypeLua("3ByteInt", 3, myBytesToValue, myValueToBytes)

See also[edit]

Related Functions[edit]