Difference between revisions of "Tutorials:Lua:Setup Auto Attach"

From Cheat Engine
Jump to navigation Jump to search
 
(2 intermediate revisions by one other user not shown)
Line 5: Line 5:
 
   Theme:  Monokai Sublime
 
   Theme:  Monokai Sublime
 
-->
 
-->
 +
[[Category:Tutorial]]
 +
[[Category:Lua]]
 +
{{DISPLAYTITLE:Setup a Lua auto attach script}}
 
== How to setup a Lua auto attach script ==
 
== How to setup a Lua auto attach script ==
  
 
'''On the Cheat Engine main form press ''Ctrl+Alt+L''''' to open the cheat table Lua script form.
 
'''On the Cheat Engine main form press ''Ctrl+Alt+L''''' to open the cheat table Lua script form.
  
Now Let's add the Lua script for the auto attach to process timer.
+
Now let's add the Lua script to auto attach to the process.
 +
 
 +
<pre>getAutoAttachList().add("Tutorial-i386.exe")</pre>
 +
 
 +
yep, that's probably all you need, it's what Cheat Engine generates for the trainers it makes. However, if you need more control for some reason, you can do it yourself with a timer and '''openProcess'''
 +
 
 
<!-- <pre>PROCESS_NAME = 'Tutorial-i386.exe'
 
<!-- <pre>PROCESS_NAME = 'Tutorial-i386.exe'
 
--------
 
--------
 
-------- Auto Attach
 
-------- Auto Attach
 
--------
 
--------
local autoAttachTimer = nil
+
local autoAttachTimer = nil ---- variable to hold timer object
 
local autoAttachTimerInterval = 1000 ---- Timer intervals are in milliseconds
 
local autoAttachTimerInterval = 1000 ---- Timer intervals are in milliseconds
local autoAttachTimerTicks = 0
+
local autoAttachTimerTicks = 0 ---- variable to count number of times the timer has run
 
local autoAttachTimerTickMax = 5000 ---- Set to zero to disable ticks max
 
local autoAttachTimerTickMax = 5000 ---- Set to zero to disable ticks max
 
local function autoAttachTimer_tick(timer) ---- Timer tick call back
 
local function autoAttachTimer_tick(timer) ---- Timer tick call back
 +
        ---- Destroy timer if max ticks is reached
 
if autoAttachTimerTickMax > 0 and autoAttachTimerTicks >= autoAttachTimerTickMax then
 
if autoAttachTimerTickMax > 0 and autoAttachTimerTicks >= autoAttachTimerTickMax then
timer.destroy() ---- Destroy timer if max ticks is reached
+
timer.destroy()
 
end
 
end
if getProcessIDFromProcessName(PROCESS_NAME) ~= nil then ---- Check if process is running
+
        ---- Check if process is running
 +
if getProcessIDFromProcessName(PROCESS_NAME) ~= nil then
 
timer.destroy() ---- Destroy timer
 
timer.destroy() ---- Destroy timer
 
openProcess(PROCESS_NAME) ---- Open the process
 
openProcess(PROCESS_NAME) ---- Open the process
Line 32: Line 42:
 
autoAttachTimer.OnTimer = autoAttachTimer_tick ---- Set timer tick call back
 
autoAttachTimer.OnTimer = autoAttachTimer_tick ---- Set timer tick call back
 
</pre> -->
 
</pre> -->
<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);">PROCESS_NAME = <span style="color:rgb(230, 219, 116);">'Tutorial-i386.exe'</span>
+
<!--
<span style="color:rgb(117, 113, 94);">--------</span>
+
  Monokai style, no clue what the original was...
<span style="color:rgb(117, 113, 94);">-------- Auto Attach</span>
+
  had to manually add "white-space:pre;" to the style list to get line breaks
<span style="color:rgb(117, 113, 94);">--------</span>
+
-->
<span style="color:rgb(249, 38, 114);">local</span> autoAttachTimer = <span style="color:rgb(174, 129, 255);">nil</span>
+
 
<span style="color:rgb(249, 38, 114);">local</span> autoAttachTimerInterval = <span style="color:rgb(174, 129, 255);">1000</span> <span style="color:rgb(117, 113, 94);">---- Timer intervals are in milliseconds</span>
+
<p class="hljs" style="white-space:pre; display: block; overflow-x: auto; padding: 0.5em; background: rgb(39, 40, 34); color: rgb(221, 221, 221);">PROCESS_NAME = <span class="hljs-string" style="color: rgb(166, 226, 46);">'Tutorial-i386.exe'</span>
<span style="color:rgb(249, 38, 114);">local</span> autoAttachTimerTicks = <span style="color:rgb(174, 129, 255);">0</span>
+
<span class="hljs-comment" style="color: rgb(117, 113, 94);">--------</span>
<span style="color:rgb(249, 38, 114);">local</span> autoAttachTimerTickMax = <span style="color:rgb(174, 129, 255);">5000</span> <span style="color:rgb(117, 113, 94);">---- Set to zero to disable ticks max</span>
+
<span class="hljs-comment" style="color: rgb(117, 113, 94);">-------- Auto Attach</span>
<span style="color:rgb(249, 38, 114);">local</span> <span style="color:rgb(249, 38, 114);">function</span> <span style="color:rgb(166, 226, 46);">autoAttachTimer_tick</span><span style="color:rgb(248, 248, 242);">(timer)</span> <span style="color:rgb(117, 113, 94);">---- Timer tick call back</span>
+
<span class="hljs-comment" style="color: rgb(117, 113, 94);">--------</span>
  <span style="color:rgb(249, 38, 114);">if</span> autoAttachTimerTickMax &gt; <span style="color:rgb(174, 129, 255);">0</span> <span style="color:rgb(249, 38, 114);">and</span> autoAttachTimerTicks &gt;= autoAttachTimerTickMax <span style="color:rgb(249, 38, 114);">then</span>
+
<span class="hljs-keyword" style="color: rgb(249, 38, 114); font-weight: 700;">local</span> autoAttachTimer = <span class="hljs-keyword" style="color: rgb(249, 38, 114); font-weight: 700;">nil</span> <span class="hljs-comment" style="color: rgb(117, 113, 94);">---- variable to hold timer object</span>
      timer.destroy() <span style="color:rgb(117, 113, 94);">---- Destroy timer if max ticks is reached</span>
+
<span class="hljs-keyword" style="color: rgb(249, 38, 114); font-weight: 700;">local</span> autoAttachTimerInterval = <span class="hljs-number">1000</span> <span class="hljs-comment" style="color: rgb(117, 113, 94);">---- Timer intervals are in milliseconds</span>
  <span style="color:rgb(249, 38, 114);">end</span>
+
<span class="hljs-keyword" style="color: rgb(249, 38, 114); font-weight: 700;">local</span> autoAttachTimerTicks = <span class="hljs-number">0</span> <span class="hljs-comment" style="color: rgb(117, 113, 94);">---- variable to count number of times the timer has run</span>
  <span style="color:rgb(249, 38, 114);">if</span> getProcessIDFromProcessName(PROCESS_NAME) <span style="color:pink;">~=</span> <span style="color:rgb(174, 129, 255);">nil</span> <span style="color:rgb(249, 38, 114);">then</span> <span style="color:rgb(117, 113, 94);">---- Check if process is running</span>
+
<span class="hljs-keyword" style="color: rgb(249, 38, 114); font-weight: 700;">local</span> autoAttachTimerTickMax = <span class="hljs-number">5000</span> <span class="hljs-comment" style="color: rgb(117, 113, 94);">---- Set to zero to disable ticks max</span>
      timer.destroy() <span style="color:rgb(117, 113, 94);">---- Destroy timer</span>
+
<span class="hljs-keyword" style="color: rgb(249, 38, 114); font-weight: 700;">local</span> <span class="hljs-function"><span class="hljs-keyword" style="color: rgb(249, 38, 114); font-weight: 700;">function</span> <span class="hljs-title" style="color: rgb(166, 226, 46); font-weight: 700;">autoAttachTimer_tick</span><span class="hljs-params">(timer)</span></span> <span class="hljs-comment" style="color: rgb(117, 113, 94);">---- Timer tick call back</span>
      openProcess(PROCESS_NAME) <span style="color:rgb(117, 113, 94);">---- Open the process</span>
+
        <span class="hljs-comment" style="color: rgb(117, 113, 94);">---- Destroy timer if max ticks is reached</span>
  <span style="color:rgb(249, 38, 114);">end</span>
+
<span class="hljs-keyword" style="color: rgb(249, 38, 114); font-weight: 700;">if</span> autoAttachTimerTickMax &gt; <span class="hljs-number">0</span> <span class="hljs-keyword" style="color: rgb(249, 38, 114); font-weight: 700;">and</span> autoAttachTimerTicks &gt;= autoAttachTimerTickMax <span class="hljs-keyword" style="color: rgb(249, 38, 114); font-weight: 700;">then</span>
  autoAttachTimerTicks = autoAttachTimerTicks + <span style="color:rgb(174, 129, 255);">1</span> <span style="color:rgb(117, 113, 94);">---- Increase ticks</span>
+
timer.destroy()
<span style="color:rgb(249, 38, 114);">end</span>
+
<span class="hljs-keyword" style="color: rgb(249, 38, 114); font-weight: 700;">end</span>
autoAttachTimer = createTimer(getMainForm()) <span style="color:rgb(117, 113, 94);">---- Create timer with the main form as it's parent</span>
+
        <span class="hljs-comment" style="color: rgb(117, 113, 94);">---- Check if process is running</span>
autoAttachTimer.Interval = autoAttachTimerInterval <span style="color:rgb(117, 113, 94);">---- Set timer interval</span>
+
<span class="hljs-keyword" style="color: rgb(249, 38, 114); font-weight: 700;">if</span> getProcessIDFromProcessName(PROCESS_NAME) ~= <span class="hljs-keyword" style="color: rgb(249, 38, 114); font-weight: 700;">nil</span> <span class="hljs-keyword" style="color: rgb(249, 38, 114); font-weight: 700;">then</span>
autoAttachTimer.OnTimer = autoAttachTimer_tick <span style="color:rgb(117, 113, 94);">---- Set timer tick call back</span>
+
timer.destroy() <span class="hljs-comment" style="color: rgb(117, 113, 94);">---- Destroy timer</span>
</p>
+
openProcess(PROCESS_NAME) <span class="hljs-comment" style="color: rgb(117, 113, 94);">---- Open the process</span>
 +
<span class="hljs-keyword" style="color: rgb(249, 38, 114); font-weight: 700;">end</span>
 +
autoAttachTimerTicks = autoAttachTimerTicks + <span class="hljs-number">1</span> <span class="hljs-comment" style="color: rgb(117, 113, 94);">---- Increase ticks</span>
 +
<span class="hljs-keyword" style="color: rgb(249, 38, 114); font-weight: 700;">end</span>
 +
autoAttachTimer = createTimer(getMainForm()) <span class="hljs-comment" style="color: rgb(117, 113, 94);">---- Create timer with the main form as it's parent</span>
 +
autoAttachTimer.Interval = autoAttachTimerInterval <span class="hljs-comment" style="color: rgb(117, 113, 94);">---- Set timer interval</span>
 +
autoAttachTimer.OnTimer = autoAttachTimer_tick <span class="hljs-comment" style="color: rgb(117, 113, 94);">---- Set timer tick call back</span></p>
  
 
'''Set the process name to the process you are attaching to''', use the full process name from the process list.
 
'''Set the process name to the process you are attaching to''', use the full process name from the process list.

Latest revision as of 01:34, 6 February 2018


How to setup a Lua auto attach script[edit]

On the Cheat Engine main form press Ctrl+Alt+L to open the cheat table Lua script form.

Now let's add the Lua script to auto attach to the process.

getAutoAttachList().add("Tutorial-i386.exe")

yep, that's probably all you need, it's what Cheat Engine generates for the trainers it makes. However, if you need more control for some reason, you can do it yourself with a timer and openProcess


PROCESS_NAME = 'Tutorial-i386.exe' -------- -------- Auto Attach -------- local autoAttachTimer = nil ---- variable to hold timer object local autoAttachTimerInterval = 1000 ---- Timer intervals are in milliseconds local autoAttachTimerTicks = 0 ---- variable to count number of times the timer has run local autoAttachTimerTickMax = 5000 ---- Set to zero to disable ticks max local function autoAttachTimer_tick(timer) ---- Timer tick call back ---- Destroy timer if max ticks is reached if autoAttachTimerTickMax > 0 and autoAttachTimerTicks >= autoAttachTimerTickMax then timer.destroy() end ---- Check if process is running if getProcessIDFromProcessName(PROCESS_NAME) ~= nil then timer.destroy() ---- Destroy timer openProcess(PROCESS_NAME) ---- Open the process end autoAttachTimerTicks = autoAttachTimerTicks + 1 ---- Increase ticks end autoAttachTimer = createTimer(getMainForm()) ---- Create timer with the main form as it's parent autoAttachTimer.Interval = autoAttachTimerInterval ---- Set timer interval autoAttachTimer.OnTimer = autoAttachTimer_tick ---- Set timer tick call back

Set the process name to the process you are attaching to, use the full process name from the process list.

Launch the process then click execute script in the cheat table Lua script form.

Tutorials.Lua.AutoAttachScript.01.png


You should see the process name at the top of the Cheat Engine form.

Tutorials.Lua.AutoAttachScript.02.png


Now when ever the cheat table is opened Cheat Engine will attempt to run the cheat table Lua script which will start the auto attach timer. By default Cheat Engine will ask to run the cheat table Lua script.

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


See also[edit]

Syntax Highlighter[edit]