Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


Simple trigger script

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> Cheat Engine Lua Scripting
View previous topic :: View next topic  
Author Message
Turok2
How do I cheat?
Reputation: 0

Joined: 16 Feb 2024
Posts: 8

PostPosted: Fri Feb 16, 2024 2:54 pm    Post subject: Simple trigger script Reply with quote

Hi guys, I'm a simple noob, but I'm very curious . In these two images below, the value of the first string (20002D43A7B) changes on columns 7C and 80, going from 0 (which is normal) to 90 (which is the action I'm interested in). How do I write a script that presses the P key on the keyboard every time the value changes from 0 to 90? Thank you very much, I can't figure it out. Sad


Tab 01.png
 Description:
 Filesize:  10.38 KB
 Viewed:  1370 Time(s)

Tab 01.png



Tab 02.png
 Description:
 Filesize:  55.72 KB
 Viewed:  1370 Time(s)

Tab 02.png


Back to top
View user's profile Send private message
LeFiXER
Grandmaster Cheater Supreme
Reputation: 20

Joined: 02 Sep 2011
Posts: 1055
Location: 0x90

PostPosted: Fri Feb 16, 2024 4:21 pm    Post subject: Reply with quote

No guarantee given, but this will press the P key if the values at both addresses = 90 (hex)
Code:

[ENABLE]
{$LUA}
if syntaxcheck then return end
if kpTimer then kpTimer.destroy() end

kpTimer = createTimer()
kpTimer.Interval = 200
kpTimer.OnTimer = function()
                              if readByte(0x20002D43A7C) == 0x90 and readByte(0x20002D43A80) == 0x90 then
                                doKeyPress(VK_P)
                              end
                             end
{$ASM}
[DISABLE]
{$LUA}
if syntaxcheck then return end
kpTimer.destroy()
Back to top
View user's profile Send private message
Turok2
How do I cheat?
Reputation: 0

Joined: 16 Feb 2024
Posts: 8

PostPosted: Fri Feb 16, 2024 4:37 pm    Post subject: Reply with quote

ok i try it and lua say this message:

[ENABLE]
{$LUA}
if syntaxcheck then return end
if kpTimer then kpTimer.destroy() end

kpTimer = createTimer()
kpTimer.Interval = 200
kpTimer.OnTimer = function()
if readByte(0x20002D43A7C) == 0x90 and readByte(0x20002D43A80) == 0x90 then
doKeyPress(VK_P)
end
end
{$ASM}
[DISABLE]
{$LUA}
if syntaxcheck then return end
kpTimer.destroy()

Script Error:[string "[ENABLE]
..."]:1: unexpected symbol near '['


Twisted Evil Twisted Evil Twisted Evil
Back to top
View user's profile Send private message
AylinCE
Grandmaster Cheater Supreme
Reputation: 32

Joined: 16 Feb 2017
Posts: 1260

PostPosted: Fri Feb 16, 2024 5:41 pm    Post subject: Reply with quote

In my case the code works fine.


ek1.PNG
 Description:
 Filesize:  62.41 KB
 Viewed:  1341 Time(s)

ek1.PNG



_________________
Hi Hitler Different Trainer forms for you!
https://forum.cheatengine.org/viewtopic.php?t=619279
Enthusiastic people: Always one step ahead
Do not underestimate me Master: You were a beginner in the past
Back to top
View user's profile Send private message Visit poster's website MSN Messenger
ParkourPenguin
I post too much
Reputation: 140

Joined: 06 Jul 2014
Posts: 4307

PostPosted: Fri Feb 16, 2024 5:50 pm    Post subject: This post has 1 review(s) Reply with quote

You're calling destroy twice. I don't know if CE catches it, but it's a bad thing to do. Set the global variable to nil after it's destroyed.

I like to put that cleanup code above [ENABLE] so it's always run even if the script is disabled without executing the disable section.
Code:
{$lua}
if syntaxcheck then return end
if my_timer then my_timer.destroy(); my_timer = nil end

[ENABLE]

my_timer = createTimer()
...

[DISABLE]
-- empty: timer destroyed above


Turok2 wrote:
unexpected symbol near '['
That code LeFiXER posted is an AA script, not a Lua script.
If you want to use the Lua script window, use this:
Code:
if kpTimer then kpTimer.destroy(); kpTimer = nil end

kpTimer = createTimer()
kpTimer.Interval = 200
kpTimer.OnTimer = function()
  if readByte(0x20002D43A7C) == 0x90 and readByte(0x20002D43A80) == 0x90 then
    doKeyPress(VK_P)
  end
end
You can't exactly turn it off once you execute this like you can an AA script.
_________________
I don't know where I'm going, but I'll figure it out when I get there.
Back to top
View user's profile Send private message
Turok2
How do I cheat?
Reputation: 0

Joined: 16 Feb 2024
Posts: 8

PostPosted: Fri Feb 16, 2024 5:53 pm    Post subject: Reply with quote

i love you all guys, it works! THANK YOU!
Back to top
View user's profile Send private message
LeFiXER
Grandmaster Cheater Supreme
Reputation: 20

Joined: 02 Sep 2011
Posts: 1055
Location: 0x90

PostPosted: Fri Feb 16, 2024 8:21 pm    Post subject: Reply with quote

ParkourPenguin wrote:
You're calling destroy twice. I don't know if CE catches it, but it's a bad thing to do. Set the global variable to nil after it's destroyed.


Noted, thanks. I seldom use globals and wasn't sure if it was necessary to handle it separately; however, correct me if I'm wrong, the enable/disable sections are independent of one another, aren't they?
Back to top
View user's profile Send private message
AylinCE
Grandmaster Cheater Supreme
Reputation: 32

Joined: 16 Feb 2017
Posts: 1260

PostPosted: Fri Feb 16, 2024 9:43 pm    Post subject: Reply with quote

I tried 3 variants and found it works.

v1:
Code:
[ENABLE]
{$LUA}
if syntaxcheck then return end
if kpTimer then kpTimer.destroy() end

kpTimer = createTimer()
kpTimer.Interval = 2000
 kpTimer.OnTimer = function()
  doKeyPress(VK_P)
  print(1000)
 end

{$ASM}

[DISABLE]

{$LUA}
-- kpTimer.Enabled = false -- It will return an undefined local error!
if syntaxcheck then return end
if kpTimer then kpTimer.destroy() end -- For the "Disable" switch, it will act as stopping the timer!


v2:
Code:
[ENABLE]
{$LUA}
if syntaxcheck then return end
--if kpTimer then kpTimer.destroy() end

kpTimer = createTimer()
kpTimer.Interval = 200
 kpTimer.OnTimer = function()
  doKeyPress(VK_P)
  print(1000)
 end

{$ASM}

[DISABLE]

{$LUA}
-- kpTimer.Enabled = false -- It will return an undefined local error!
if syntaxcheck then return end
if kpTimer then kpTimer.destroy() end -- For the "Disable" switch, it will act as stopping the timer!



It's especially surprising that scenario 3 stops the timer in case of "active=false".
It's nice that @ParkourPenguin caught this detail.

v3:

Code:
{$LUA}
if syntaxcheck then return end
if kpTimer then kpTimer.destroy() end

[ENABLE]

kpTimer = createTimer()
kpTimer.Interval = 2000
 kpTimer.OnTimer = function()
  doKeyPress(VK_P)
  print(1000)
 end

[DISABLE]


Result:
The following detail is revealed;

script.Active = true
(In the script code; the line of code between the first line and "Disable" is activated (Execute).)

script.Active = false
(All lines are activated except the lines between "Enable" - "Disable")

_________________
Hi Hitler Different Trainer forms for you!
https://forum.cheatengine.org/viewtopic.php?t=619279
Enthusiastic people: Always one step ahead
Do not underestimate me Master: You were a beginner in the past
Back to top
View user's profile Send private message Visit poster's website MSN Messenger
ParkourPenguin
I post too much
Reputation: 140

Joined: 06 Jul 2014
Posts: 4307

PostPosted: Fri Feb 16, 2024 10:28 pm    Post subject: Reply with quote

LeFiXER wrote:
the enable/disable sections are independent of one another, aren't they?
They're independent in the sense that only one of them is run at a time.

Basically, the lines after [ENABLE] / [DISABLE] are only kept if the script is being enabled or disabled respectively. Above [ENABLE], it's always kept.
e.g. this script:
Code:
{$lua}
if syntaxcheck then return end

[ENABLE]
print'enabled'

[DISABLE]
print'disabled'

When this is enabled, CE executes this AA script:
Code:
{$lua}
if syntaxcheck then return end

print'enabled'

And when it's disabled, it executes this AA script:
Code:
{$lua}
if syntaxcheck then return end

print'disabled'

Local variables aren't kept between invocations.
Code:
{$lua}
if syntaxcheck then return end

[ENABLE]
local v = 5
print(v)  -- prints 5

[DISABLE]
print(tostring(v))  -- prints nil

_________________
I don't know where I'm going, but I'll figure it out when I get there.
Back to top
View user's profile Send private message
LeFiXER
Grandmaster Cheater Supreme
Reputation: 20

Joined: 02 Sep 2011
Posts: 1055
Location: 0x90

PostPosted: Sat Feb 17, 2024 12:18 am    Post subject: Reply with quote

Thanks for the clarification. Your wisdom is always welcome. Smile
Back to top
View user's profile Send private message
Turok2
How do I cheat?
Reputation: 0

Joined: 16 Feb 2024
Posts: 8

PostPosted: Sat Feb 17, 2024 1:19 am    Post subject: Reply with quote

Ok daddy I've created the Lua script by extending it with additional values at other addresses. But I don't want to enter Cheat Engine every time and press Ctrl+Alt+L and click Execute Script. Can I bring it to the initial table, where I marked the red lines, and then activate it by checking the box? Thanks a lot! What a great community!


bibu.png
 Description:
 Filesize:  85.03 KB
 Viewed:  1293 Time(s)

bibu.png


Back to top
View user's profile Send private message
LeFiXER
Grandmaster Cheater Supreme
Reputation: 20

Joined: 02 Sep 2011
Posts: 1055
Location: 0x90

PostPosted: Sat Feb 17, 2024 3:37 am    Post subject: Reply with quote

Open the Memory View window > Click Tools on the menu > Select Auto Assemble from this menu > Paste your code in this new window, it will be blank to begin with. You can either use a template by clicking Template on the menu followed by Cheat Table Framework (basic framework for a script), and make sure that the [ENABLE] and [DISABLE] sections have the relevant code. Or you can type in [ENABLE]/[DISABLE] yourself.

It will look similar to what I posted initially at the beginning of this thread.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> Cheat Engine Lua Scripting All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites