Difference between revisions of "Lua:onAutoGuess"

From Cheat Engine
Jump to navigation Jump to search
(Major overhaul of the post.)
 
Line 1: Line 1:
 
[[Category:Lua]]
 
[[Category:Lua]]
'''function''' onAutoGuess(''function'')
+
{{CodeBox|'''function''' onAutoGuess(''callback'') ''':''' void}}
  
Registers an function to be called whenever autoguess is used to predict a variable type
+
Registers a callback function that is called whenever Cheat Engine's auto guess feature is used to predict a variable type.
  
function declaration:
+
The callback can override the guessed variable type by returning a different value type. If the guess should not be changed, return the provided <code>ceguess</code> value.
  function (address, guessbyce): Return the variable type you want it to be. If no change, just return guessbyce
 
  
 +
===Function Parameters===
 +
{|width="85%" cellpadding="10%" cellspacing="0" border="0"
 +
!align="left"|Parameter
 +
!align="left"|Type
 +
!style="width: 80%;background-color:white;" align="left"|Description
 +
|-
 +
|callback
 +
|Function
 +
|The function to call when Cheat Engine auto-guesses a variable type.
 +
|}
  
 +
===Returns===
 +
This function does not return a value.
  
 
+
===Callback Parameters===
===Function Parameters===
+
{|width="85%" cellpadding="10%" cellspacing="0" border="0"
{|width="85%" cellpadding="10%" cellpadding="5%" 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
 
|-
 
|-
|function
+
|address
|function
+
|Integer
|A function that returns a variable type. It's function parameters are (address, guessbyce)
+
|The address for which Cheat Engine is guessing the variable type.
 +
|-
 +
|ceguess
 +
|ValueType
 +
|The variable type guessed by Cheat Engine.
 +
|}
 +
 
 +
===Callback Return Value===
 +
{|width="85%" cellpadding="10%" cellspacing="0" border="0"
 +
!align="left"|Return Type
 +
!style="width: 80%;background-color:white;" align="left"|Description
 +
|-
 +
|ValueType
 +
|Return the variable type that should be used. Return <code>ceguess</code> if the guessed type should not be changed.
 +
|}
 +
 
 +
===Common Value Types===
 +
{|width="85%" cellpadding="10%" cellspacing="0" border="0"
 +
!align="left"|Constant
 +
!style="width: 80%;background-color:white;" align="left"|Description
 +
|-
 +
|vtByte
 +
|Byte value.
 +
|-
 +
|vtWord
 +
|2-byte value.
 +
|-
 +
|vtDword
 +
|4-byte integer value.
 +
|-
 +
|vtQword
 +
|8-byte integer value.
 +
|-
 +
|vtSingle
 +
|4-byte floating-point value.
 +
|-
 +
|vtDouble
 +
|8-byte floating-point value.
 +
|-
 +
|vtString
 +
|String value.
 +
|-
 +
|vtPointer
 +
|Pointer value.
 
|}
 
|}
 +
 +
===Examples===
 +
 +
====Keep Cheat Engine's guessed type====
 +
<syntaxhighlight lang="lua" line highlight="1">
 +
onAutoGuess(function(address, ceguess)
 +
  return ceguess
 +
end)
 +
</syntaxhighlight>
 +
 +
====Force all guesses to 4-byte====
 +
<syntaxhighlight lang="lua" line highlight="1">
 +
onAutoGuess(function(address, ceguess)
 +
  return vtDword
 +
end)
 +
</syntaxhighlight>
 +
 +
====Override guesses for a specific address range====
 +
<syntaxhighlight lang="lua" line highlight="1">
 +
onAutoGuess(function(address, ceguess)
 +
  local startAddress = getAddress("game.exe+1000")
 +
  local endAddress = getAddress("game.exe+2000")
 +
 +
  if address >= startAddress and address <= endAddress then
 +
    return vtSingle
 +
  end
 +
 +
  return ceguess
 +
end)
 +
</syntaxhighlight>
 +
 +
====Treat pointer-looking addresses as pointers====
 +
<syntaxhighlight lang="lua" line highlight="1">
 +
onAutoGuess(function(address, ceguess)
 +
  local value = readPointer(address)
 +
 +
  if value ~= nil and value ~= 0 then
 +
    return vtPointer
 +
  end
 +
 +
  return ceguess
 +
end)
 +
</syntaxhighlight>
 +
 +
====Log auto guess activity====
 +
<syntaxhighlight lang="lua" line highlight="1">
 +
onAutoGuess(function(address, ceguess)
 +
  print(string.format("Auto guess at %X: %s", address, tostring(ceguess)))
 +
 +
  return ceguess
 +
end)
 +
</syntaxhighlight>
 +
 +
====Use a named callback function====
 +
<syntaxhighlight lang="lua" line highlight="9">
 +
local function autoGuessOverride(address, ceguess)
 +
  if ceguess == vtDword then
 +
    return vtQword
 +
  end
 +
 +
  return ceguess
 +
end
 +
 +
onAutoGuess(autoGuessOverride)
 +
</syntaxhighlight>
 +
 +
====Only override unknown or unwanted guesses====
 +
<syntaxhighlight lang="lua" line highlight="1">
 +
onAutoGuess(function(address, ceguess)
 +
  if ceguess == nil then
 +
    return vtDword
 +
  end
 +
 +
  return ceguess
 +
end)
 +
</syntaxhighlight>
  
 
{{LuaSeeAlso}}
 
{{LuaSeeAlso}}

Latest revision as of 19:47, 26 June 2026

<> Lua API Reference

function onAutoGuess(callback) : void

Registers a callback function that is called whenever Cheat Engine's auto guess feature is used to predict a variable type.

The callback can override the guessed variable type by returning a different value type. If the guess should not be changed, return the provided ceguess value.

Function Parameters[edit]

Parameter Type Description
callback Function The function to call when Cheat Engine auto-guesses a variable type.

Returns[edit]

This function does not return a value.

Callback Parameters[edit]

Parameter Type Description
address Integer The address for which Cheat Engine is guessing the variable type.
ceguess ValueType The variable type guessed by Cheat Engine.

Callback Return Value[edit]

Return Type Description
ValueType Return the variable type that should be used. Return ceguess if the guessed type should not be changed.

Common Value Types[edit]

Constant Description
vtByte Byte value.
vtWord 2-byte value.
vtDword 4-byte integer value.
vtQword 8-byte integer value.
vtSingle 4-byte floating-point value.
vtDouble 8-byte floating-point value.
vtString String value.
vtPointer Pointer value.

Examples[edit]

Keep Cheat Engine's guessed type[edit]

1 onAutoGuess(function(address, ceguess)
2   return ceguess
3 end)

Force all guesses to 4-byte[edit]

1 onAutoGuess(function(address, ceguess)
2   return vtDword
3 end)

Override guesses for a specific address range[edit]

 1 onAutoGuess(function(address, ceguess)
 2   local startAddress = getAddress("game.exe+1000")
 3   local endAddress = getAddress("game.exe+2000")
 4 
 5   if address >= startAddress and address <= endAddress then
 6     return vtSingle
 7   end
 8 
 9   return ceguess
10 end)

Treat pointer-looking addresses as pointers[edit]

1 onAutoGuess(function(address, ceguess)
2   local value = readPointer(address)
3 
4   if value ~= nil and value ~= 0 then
5     return vtPointer
6   end
7 
8   return ceguess
9 end)

Log auto guess activity[edit]

1 onAutoGuess(function(address, ceguess)
2   print(string.format("Auto guess at %X: %s", address, tostring(ceguess)))
3 
4   return ceguess
5 end)

Use a named callback function[edit]

1 local function autoGuessOverride(address, ceguess)
2   if ceguess == vtDword then
3     return vtQword
4   end
5 
6   return ceguess
7 end
8 
9 onAutoGuess(autoGuessOverride)

Only override unknown or unwanted guesses[edit]

1 onAutoGuess(function(address, ceguess)
2   if ceguess == nil then
3     return vtDword
4   end
5 
6   return ceguess
7 end)

Main Pages

Core Lua documentation entry points

Lua
Script Engine