Difference between revisions of "Tutorials:Lua:Basics"

From Cheat Engine
Jump to navigation Jump to search
(Deleted "probably," because "(settings dependent)" is true and don't need "probably,".)
 
(7 intermediate revisions by 2 users not shown)
Line 5: Line 5:
 
   Theme:  Monokai Sublime
 
   Theme:  Monokai Sublime
 
-->
 
-->
 +
[[Category:Tutorial]]
 +
[[Category:Lua]]
 +
{{DISPLAYTITLE:Lua Basics}}
 
Lua interaction is done a few ways in Cheat Engine.  
 
Lua interaction is done a few ways in Cheat Engine.  
  
# You have the ''Lua Engine'' that you can access from the memory view form by selecting ''tools'' then selecting ''lua engine''.
+
# 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 [http://www.cheatengine.org/forum/viewtopic.php?p=5714676 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''
 
#:[[File:Tutorials.LuaBasics.01.png|border]]
 
#:[[File:Tutorials.LuaBasics.01.png|border]]
# There is a lua script associated with your cheat table that you access form the Cheat Engine main form menu by selecting ''table'' then select ''show cheat table lua script''. You can also load and execute other lua scripts.
+
# 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.
 
#:[[File:Tutorials.LuaBasics.02.png|border]]
 
#:[[File:Tutorials.LuaBasics.02.png|border]]
# Then there is making use of the [[Auto Assembler:LUA ASM|{$lua}]] tag in an [[Cheat_Engine:Auto Assembler|Auto Assembler]] script. You can open an auto assembler form from the Cheat Engine main form by pressing ''Ctrl+Alt+A''.
+
# 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''.
 +
#:<pre>luaCall(print('I have the power!'))</pre>
 +
# Then there is making use of the [[Auto Assembler:LUA ASM|{$lua}]] tag in an [[Cheat_Engine:Auto Assembler|Auto Assembler]] script. See previous '''luaCall''' description for how to open the AA form.
 
#:[[File:Tutorials.LuaBasics.04.png|border]]
 
#:[[File:Tutorials.LuaBasics.04.png|border]]
 +
#: 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 [[Auto Assembler:LUA ASM|{$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 <pre style="display:inline;padding:0">return "{$lua}\nprint('hi')\n{$asm}"</pre> since CE seems to want the {$...} tags without whitespace but actually having it without whitespace in a multiline string (<pre style="display:inline;padding:0">[[like this]]</pre>) 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.
 +
 +
<blockquote><blockquote><pre>{$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]
 +
</pre></blockquote></blockquote>
 +
::[[File:Tutorials.LuaBasics.06.png|border]]
 +
  
  
 
== Lua Engine ==
 
== Lua Engine ==
Form the Cheat Engine main form press ''Shift+Ctrl+Alt+L'' to open the Lua Engine form. 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.
+
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.
  
 
[[File:Tutorials.LuaBasics.03.png|border]]
 
[[File:Tutorials.LuaBasics.03.png|border]]
Line 22: Line 44:
  
 
== Cheat table lua script ==
 
== Cheat table lua script ==
From the Cheat Engine main form press ''Ctrl+L'', to open the cheat table lua script form.
+
From the Cheat Engine main form press ''Ctrl+Alt+L'', to open the cheat table lua script form.
  
 
[[File:Tutorials.LuaBasics.05.png|border]]
 
[[File:Tutorials.LuaBasics.05.png|border]]
Line 31: Line 53:
  
 
From the cheat table lua script form menu you can select ''file'' then select ''new window'' to open new script windows.
 
From the cheat table lua script form menu you can select ''file'' then select ''new window'' to open new script windows.
 
  
 
== Script windows ==
 
== Script windows ==
You can have as many script windows open as you want.  If you save these scripts as lua files in the same directory as your cheat table. You can run them from other scripts using lua's ''require'', which will only run a script the first time it's required, 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.
+
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 <pre style="display:inline;padding:0">package.path</pre> 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:
Line 40: Line 62:
 
require("Script1")
 
require("Script1")
 
dofile("Script1.lua")
 
dofile("Script1.lua")
 +
print('---')
 +
require("Script1")
 +
print('---')
 +
package.loaded.Script1 = nil
 
require("Script1")
 
require("Script1")
 
</pre> -->
 
</pre> -->
<p style="white-space:pre;font-family:'Lucida Console';display:block;overflow-x:auto;padding:0.5em;background:rgb(35, 36, 31);color:rgb(248, 248, 242);"><span style="color:rgb(166, 226, 46);">require</span><span style="color:rgb(248, 248, 242);">(<span style="color:rgb(230, 219, 116);">'Script1'</span>)</span>
+
<p style="line-height:1.15em;white-space:pre;font-family:'Lucida Console';display:block;overflow-x:auto;padding:0.5em;background:rgb(35, 36, 31);color:rgb(248, 248, 242);"><span style="color:rgb(166, 226, 46);">require</span><span style="color:rgb(248, 248, 242);">(<span style="color:rgb(230, 219, 116);">'Script1'</span>)</span>
 
<span style="color:rgb(166, 226, 46);">dofile</span><span style="color:rgb(248, 248, 242);">(<span style="color:rgb(230, 219, 116);">'Script1.lua'</span>)</span>
 
<span style="color:rgb(166, 226, 46);">dofile</span><span style="color:rgb(248, 248, 242);">(<span style="color:rgb(230, 219, 116);">'Script1.lua'</span>)</span>
 
<span style="color:rgb(166, 226, 46);">require</span><span style="color:rgb(248, 248, 242);">(<span style="color:rgb(230, 219, 116);">'Script1'</span>)</span>
 
<span style="color:rgb(166, 226, 46);">require</span><span style="color:rgb(248, 248, 242);">(<span style="color:rgb(230, 219, 116);">'Script1'</span>)</span>
Line 50: Line 76:
 
<!-- <pre>
 
<!-- <pre>
 
Script1 Executing...  
 
Script1 Executing...  
 +
Script1 Executing...
 +
---
 +
---
 
Script1 Executing...  
 
Script1 Executing...  
 
</pre> -->
 
</pre> -->
<p style="white-space:pre;font-family:'Lucida Console';display:block;overflow-x:auto;padding:0.5em;background:rgb(35, 36, 31);color:rgb(248, 248, 242);">Script1 Executing...  
+
<p style="line-height:1.15em;white-space:pre;font-family:'Lucida Console';display:block;overflow-x:auto;padding:0.5em;background:rgb(35, 36, 31);color:rgb(248, 248, 242);">Script1 Executing...  
 +
Script1 Executing...
 +
---
 +
---
 
Script1 Executing...  
 
Script1 Executing...  
 
</p>
 
</p>
Line 69: Line 101:
 
* [https://www.lua.org/start.html lua.org - Getting started]
 
* [https://www.lua.org/start.html lua.org - Getting started]
 
* [http://lua-users.org/wiki/TutorialDirectory lua-users.org - Tutorial directory]
 
* [http://lua-users.org/wiki/TutorialDirectory lua-users.org - Tutorial directory]
 +
 +
==== Syntax Highlighter ====
 +
* [http://pinetools.com/syntax-highlighter pinetools.com/syntax-highlighter]

Latest revision as of 05:54, 13 August 2020


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]