Lua:executeCodeEx

From Cheat Engine
Revision as of 05:04, 21 June 2026 by Leunsel (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

⚠️ Work Needed

This page still needs significant improvements.

Please help by expanding or correcting this article where possible.

For suggestions or discussion, see the talk page.

<> Lua API Reference

function executeCodeEx(callmethod, timeout, address, param1, ...) : integer or nil

Executes code at the specified address.

The callmethod parameter determines the calling convention. The timeout parameter determines how long Cheat Engine waits for a result. Function parameters can either be passed directly, in which case Cheat Engine will try to guess the type, or as explicit parameter tables containing a type and value field.

If timeout is 0, Cheat Engine will not wait for a result. In that case, the call memory will not be freed automatically, which can cause a memory leak.

When calling compiled C++ instance methods, keep in mind that the object instance is passed implicitly by the compiler. The instance is not written as a normal argument in the C++ source code, but the compiled function still expects it. On 64-bit Windows, this pointer is commonly passed in RCX, so it must be provided as the first parameter.

For example, a method such as CheatManager::Fly may need to be called by passing the CheatManager instance as the first argument. (Reference provided by SunBeam)

Function Parameters[edit]

Parameter Type Description
callmethod Integer The calling convention to use. 0 uses stdcall, and 1 uses cdecl.
timeout Integer or nil The number of milliseconds to wait for a result. nil or -1 waits indefinitely. 0 does not wait for a result.
address Integer or CEAddressString The address to execute.
param1 Any or Table (optional) The first parameter to pass to the executed code. This can be a direct value or a table containing a type and value field.
... Any or Table (optional) Additional parameters to pass to the executed code.

Call Method Values[edit]

Value Calling Convention Description
0 stdcall Uses the stdcall calling convention.
1 cdecl Uses the cdecl calling convention.

Timeout Values[edit]

Value Meaning Description
nil Wait indefinitely Waits indefinitely for the executed code to finish.
Wait indefinitely Waits indefinitely for the executed code to finish.
0 Do not wait Does not wait for a result. The call memory will not be freed automatically, which can cause a memory leak.
> 0 Timed wait Waits up to the specified number of milliseconds for a result.

Parameter Table Fields[edit]

Field Type Description
type Integer The explicit type of the parameter value.
value Any The value to pass to the executed code.

Parameter Type Values[edit]

Value Type Description
0 Integer or pointer A 32-bit or 64-bit integer value. This can also be used for pointers.
1 Float A 32-bit floating point value.
2 Double A 64-bit floating point value.
3 ASCII string An ASCII string. The string will be converted to a pointer to that string.
4 Wide string A wide string. The string will be converted to a pointer to that string.

Returns[edit]

integer or nil — The result of the executed code when Cheat Engine waits for completion, or nil when no result is available.

Examples[edit]

local address = getAddress("SomeFunction")

local result = executeCodeEx(
  0,
  1000,
  address,
  {type = 0, value = 123}
)

print("Result: " .. tostring(result))
local address = getAddress("SomeFunction")

-- Let Cheat Engine guess the parameter types
local result = executeCodeEx(0, 1000, address, 123, 456)

print("Result: " .. tostring(result))
local address = getAddress("SomeFunction")

-- Pass an ASCII string parameter
local result = executeCodeEx(
  0,
  1000,
  address,
  {type = 3, value = "Hello World"}
)

print("Result: " .. tostring(result))
local flyAddress = getAddress("CheatManager.Fly")
local cheatManagerInstance = getAddress("CheatManagerInstance")

-- Pass the class instance as the first argument
executeCodeEx(0, nil, flyAddress, cheatManagerInstance)

Main Pages

Core Lua documentation entry points

Lua
Script Engine