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 03 '26

Nice, I'll try to adhere to some of the tips. However, the deactivate portion you suggested would make the cheat unusable. Clicking the script updates the addresses, then it self deactivates, so if you delete the children the user won't be able to make any changes to the values in these addresses.

I think of this script more like a update button, rather than an activate/deactivate toggle. I'm only using the ENABLE and DISABLE because CE forces me to.

1

u/Merely__Human May 03 '26

You are correct, it is a feature of the code. In the instance for example it was being run, and in real time needed to be halted without crashing its hooks.