CE Lua Basics

Section's for general approaches on hacking various options in games. No online-related discussions/posts OR warez!
Post Reply
TimFun13
Expert Cheater
Expert Cheater
Posts: 1354
Joined: Fri Mar 03, 2017 12:31 am
Reputation: 6

CE Lua Basics

Post by TimFun13 »

[Link]

CE Lua Basics

Lua interaction is done a few ways in Cheat Engine.

  1. You have the Lua Engine that you can access from the memory view form by selecting Tools then selecting Lua Engine. You can debug the scripts written here by setting a breakpoint by clicking next to the line numbers. This is mostly used for testing since it is not saved with the cheat table in any way. Also it is easy to do a lot of work and then have CE crash losing it all, so make sure to grab [Link] autorun script. One other useful feature is that you can return a table and CE will attempt to print it for you whereas lua's default for print is to do nothing and tostring just returns something like table: 0000000004195640

    [Link]
  2. There is a lua script associated with your cheat table that you access from the Cheat Engine main form menu by selecting Table then select Show Cheat Table Lua Script. This is the script that CE (probably, settings dependent) prompts you to execute when you open a CT/CETrainer file with a lua script. You can also load and execute other lua scripts from here.

    [Link]
  3. There is the luaCall Auto Assembler command, that you pass a single line Lua script to (you can use ; for new lines!). You can open an auto assembler form from the memory viewer Tools->Auto Assemble menu option (shortcut `Ctrl+A`) or from the Cheat Engine main form by pressing Ctrl+Alt+A.

    Code: Select all

    luaCall(print('I have the power!'))
  4. Then there is making use of the [Link] tag in an [Link] script. See previous luaCall description for how to open the AA form.

    [Link]

    Note this code is executed before any of the AA code, and that's for the following purpose:If you return a string from the [Link] tag it will be treated as Auto Assembly code in the location of that lua block. And yes, you can return a string that contains another lua block but you have to do something like return [ICODE]{$lua}

    print('hi')

    {$asm}[/ICODE] since CE seems to want the {$...} tags without whitespace but actually having it without whitespace in a multiline string [ICODE][[like this]][/ICODE] causes the original lua block to be terminated prematurely :DWith this (since CE 6.7) you have access to the memrec and syntaxcheck variables, memrec is the memory record the script is in (can be nil when assigning the script to the table or executing the code without assigning it) while syntaxcheck is true if Cheat Engine is running the script to check the syntax of the AA code (such as when editing the script) and false when it's actually enabling the memory record script.

Code: Select all

{$lua}
-- code before either enable/disable section runs for both just like with AA code
if syntaxcheck then return end
if memrec then print(memrec.Description) end
-- the check is not really necessary but it is technically correct to have it
------------------------------ ENABLE ------------------------------
-- yes [ENABLE]/[DISABLE] are valid inside of a lua block
[ENABLE]
------------------------------ DISABLE ------------------------------
[DISABLE]
[Link]





Lua Engine

The Lua Engine form contains a text box for lua's standard output (print calls use the standard output) as well as an interactive script box that you can directly execute lua script. You can open or save scripts from here.



[Link]







Cheat table lua script

From the Cheat Engine main form press Ctrl+Alt+L, to open the cheat table lua script form.



[Link]



This script is associated with the cheat table. By default when opening a cheat table file Cheat Engine will prompt you that the table has a table lua script and asking you if you want to execute it.



Note: You can change what Cheat Engine does with the cheat table Lua script in the Cheat Engine general settings.



From the cheat table lua script form menu you can select file then select new window to open new script windows.



Script windows

You can debug the scripts written here by setting a breakpoint by clicking next to the line numbers.



You can have as many script windows open as you want, simply click File then New Window. If you save these scripts as lua files in the same directory as your cheat table or any directory included in the [ICODE]package.path[/ICODE]

string. You can run them from other scripts using lua's require, which will only run a script the first time it's required (or if package.loaded.filename is set to nil), and dofile, which will run a script every time you call it. Note that you do not use the extension with require, ".lua" is assumed, but you do need it with dofile.

Code:

Code: Select all

require('Script1')
dofile('Script1.lua')
require('Script1')
Output:

Code: Select all

Script1 Executing...
Script1 Executing...
---
---
Script1 Executing...


See also
  • [Link]
  • [Link]
  • [Link]
External links
  • [Link]
  • [Link]
  • [Link]
  • [Link]
  • [Link]
Last edited by TimFun13 on Tue May 01, 2018 12:41 am, edited 3 times in total.

User avatar
Face007
Fearless Donors
Fearless Donors
Posts: 40
Joined: Sat Sep 07, 2019 7:09 am
Reputation: 89

Re: CE Lua Basics

Post by Face007 »

Thanks a lot for your tutorial

Post Reply

Who is online

Users browsing this forum: No registered users