Lua Basics

From Cheat Engine
Jump to navigation Jump to search


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 this 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
    Tutorials.LuaBasics.01.png
  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 (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.
    Tutorials.LuaBasics.02.png
  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.
    luaCall(print('I have the power!'))
  4. Then there is making use of the {$lua} tag in an Auto Assembler script. See previous luaCall description for how to open the AA form.
    Tutorials.LuaBasics.04.png
    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 {$lua} 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 "{$lua}\nprint('hi')\n{$asm}"
    since CE seems to want the {$...} tags without whitespace but actually having it without whitespace in a multiline string (
    [[like this]]
    ) causes the original lua block to be terminated prematurely :D
    With 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.
{$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]
Tutorials.LuaBasics.06.png


Lua Engine[edit]

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.

Tutorials.LuaBasics.03.png


Cheat table lua script[edit]

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

Tutorials.LuaBasics.05.png

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[edit]

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

package.path

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:

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

Output:

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


See also[edit]

External links[edit]

Syntax Highlighter[edit]