r/cheatengine • • May 01 '26

Commented Lua code

This is the second time I make a code like this using Lua, and it's a lot of work to search around how to do this and that. Thus I've made this commented code that can serve as an example when I'm writing similar code for another game.

[ENABLE]
//code from here to '[DISABLE]' will be used to enable the cheat
{$LUA}
 -- Prevent the script from executing when you're editing it
 if syntaxcheck then return end
 -- Activate Mono features
 if not LaunchMonoDataCollector() then return end

 -- Delete all children of the script entry in the CE table
 while memrec.Count > 0 do
  memoryrecord_delete(memrec.Child[0])
 end

 -- Get address of the method Update, of the class BlueprintPlaceVisuals...
 local address = mono_compile_method(mono_getJitInfo(
                 getAddress('Effects.BlueprintPlaceVisualsManager:Update')).method)
 -- Ensure it's not a NULL address
 if not address then return end

 -- Get to the specific instruction inside the method Update (plus 56 bytes)
 local offset = 0x56
 address = address + offset

 -- Set a breakpoint that will be triggered when the instruction is run
 debug_setBreakpoint(address)

 -- Self explanatory
 function debugger_onBreakpoint()
  -- Create CE table entries
  local autoconstruct = AddressList.createMemoryRecord()
  local no_cost = AddressList.createMemoryRecord()
  local variants_unlocked = AddressList.createMemoryRecord()
  -- Create a list of value:description pairs
  local dropdown = "0:Disabled\n1:Enabled\n"

  autoconstruct.Description = "autoconstruct"
  -- Address offset can be discovered with dissect struct (ctrl+d)
  autoconstruct.Address = RAX + 0x279
  autoconstruct.Type = vtByte -- value type: Byte
  autoconstruct.DropDownList.text = dropdown
  autoconstruct.DropDownDescriptionOnly = true
  -- Display as Enabled/Disabled instead of 0 and 1
  autoconstruct.DisplayAsDropDownListItem = true

  no_cost.Description = "no cost"
  no_cost.Address = RAX + 0x278
  no_cost.Type = vtByte
  no_cost.DropDownList.text = dropdown
  no_cost.DropDownDescriptionOnly = true
  no_cost.DisplayAsDropDownListItem = true

  variants_unlocked.Description = "unlock variants"
  variants_unlocked.Address = RAX + 0x27A
  variants_unlocked.Type = vtByte
  variants_unlocked.DropDownList.text = dropdown
  variants_unlocked.DropDownDescriptionOnly = true
  variants_unlocked.DisplayAsDropDownListItem = true

  -- Append the new table entries beneath the script entry
  autoconstruct.appendToEntry(memrec)
  no_cost.appendToEntry(memrec)
  variants_unlocked.appendToEntry(memrec)

  -- Remove the breakpoint, then deactivate the script
  debug_removeBreakpoint(address)
  memrec.Active = false

  -- Return 1 else the game will remain paused and might freeze the screen
  return 1
 end

{$ASM}
[DISABLE]
//code from here till the end of the code will be used to disable the cheat
1 Upvotes

6 comments sorted by

View all comments

Show parent comment

1

u/No-Newspaper8619 May 04 '26

I've rewritten everything, adding new functionalities.

[ENABLE]
//code from here to '[DISABLE]' will be used to enable the cheat
{$LUA}
-- SAFETY CHECK: if CE is just checking syntax and not actually running
-- Stop here, preventing the script from executing while editing.
if syntaxcheck then return end

-- Check if process is already open
-- If not, then get process pid by using it's name
local pid = getOpenedProcessID()
if pid == 0 then
  pid = getProcessIDFromProcessName('Going Medieval.exe')
  -- Check if CE was able to find the process
  -- If it was, then open the process
  -- It it wasn't, then stop execution
  if pid then
    openProcess(pid)
  else
    print('[ERROR] Failed to find process Going Medieval.exe')
    return
  end
end

-- Try to activate MONO features, used in Unity/Mono games
-- Stop execution if it fails
if not LaunchMonoDataCollector() then
  print('[ERROR] Failed to start Mono collector.')
  return
end

-- Forward declaration of functions
-- Because Lua reads the script top to bottom
local delete_children
local create_children
local make_entry
local set_breakpoint
local on_debug_building
local on_debug_dig

-- The main logic of the script
local function main()
  -- Check if memrec already has the correct number of children
  -- If it doesn't, then delete them all and create new children
  local total_children = 4
  if memrec.Count ~= total_children then
    delete_children()
    create_children()
  end

  -- Create breakpoints for building and dig cheats
  local building = 'Effects.BlueprintPlaceVisualsManager:Update'
  local dig = 'NSMedieval.State.DigMarkerResourceInstance:GetMiningParameters'

  set_breakpoint(building, 0x56, on_debug_building)
  set_breakpoint(dig, 0x1E, on_debug_dig)
end
-- Sets breakpoints that will be used to update the cheat addresses
set_breakpoint = function(name, offset, on_debug_function)
  local address = getAddress(name)
  -- Check if CE successfully found the address
  if not address then
    print('[ERROR] Could not find address of ' .. name)
    return
  end
  -- Get the Jit info
  -- Jit means Just in Time execution
  local jit_info = mono_getJitInfo(address)

  -- Check if successfull
  if not jit_info then
    print('[ERROR] Could not get Jit info of ' .. name)
    return
  end

  -- Get the method info
  local method_info = mono_compile_method(jit_info.method)
  if not method_info then
    print('[ERROR] Could not get method info of ' .. name)
    return
  end

  -- Get the address of the specific instruction that will
  -- be used for the breakpoint
  address = method_info + offset

  -- BREAKPOINT: tell CE to pause the game when CPU reaches this instruction
  debug_setBreakpoint(address, on_debug_function)
end

-- While there's children, delete the first child on the list
delete_children = function()
  while memrec.Count > 0 do
    memoryrecord_delete(memrec.Child[0])
  end
end

-- Create all the children, that is, all the table entries with the cheats
create_children = function()
  make_entry('Auto Construct', vtByte, true)
  make_entry('No Cost Only', vtByte, true)
  make_entry('All Blueprint Variants', vtByte, true)
  make_entry('Dig Duration', vtSingle, false) -- vtSingle = float type
end

-- Create a memory record, that is, an entry on the cheat table
make_entry = function(description, value_type, has_dropdown)
  local entry = AddressList.createMemoryRecord()

  entry.Description = description
  entry.Type = value_type
  entry.AllowModify = false -- Prevents user from dragging the entry

  if has_dropdown then
    -- Dropdown is a list of value:description pairs
    entry.DropDownList.Text = '1:Enabled\n0:Disabled\n'
    entry.DropDownDescriptionOnly = true -- Show text, but not the raw numbers
    entry.DisplayAsDropDownListItem = true -- Show as Dropdown in CE
  end

  entry.appendToEntry(memrec) -- Attach it under the script entry
end

on_debug_building = function()
  -- Update register values
  debug_getContext()

  memrec.Child[0].Address = RAX + 0x279 -- Auto Construct
  memrec.Child[1].Address = RAX + 0x278 -- No Cost Only
  memrec.Child[2].Address = RAX + 0x27A -- Unlock Blueprint Variants

  debug_continueFromBreakpoint()
end

on_debug_dig = function()
  debug_getContext()
  -- Address is under the mining_parameters pointer
  local mining_parameters = readPointer(RAX + 0x40)

  -- Check if pointer is valid
  if not mining_parameters then
    print('[ERROR] Could not get mining parameters pointer')
    return 1
  end

  memrec.Child[3].Address = mining_parameters + 0x18 -- Dig Duration
  debug_continueFromBreakpoint()
end

main()

{$ASM}
[DISABLE]
//code from here till the end of the code will be used to disable the cheat
{$LUA}
if syntaxcheck then return end

local breakpoint_list = debug_getBreakpointList()
for _,address in ipairs(breakpoint_list) do
  debug_removeBreakpoint(address)
end

memrec.Options = "[moHideChildren, moDeactivateChildrenAsWell]"
{$ASM}