Lua IF

Want Cheat Engine to do something specific and no idea how to do that, ask here. (From simple scripts to full trainers and extensions)
Post Reply
Lorizzuoso
Noobzor
Noobzor
Posts: 13
Joined: Wed Apr 20, 2022 6:17 pm
Reputation: 0

Lua IF

Post by Lorizzuoso »

I would need to have an "if" that if an address value is 4 then it writes FF and it stops the "for" below but if that value is not 4 anymore than the "for" can resume.

[ENABLE]
{$lua}
b={}
timercharactersstand = createTimer(nil, false)
timercharactersstand.Interval = 20
timercharactersstand.Enabled = true
timercharactersstand.OnTimer = function(timer)

for i=1,2 do
repeat
b[i]=math.random(70)
until not ((b[i]==0x28) or (b[i]==0x09) or (b[i]==0x0a) or (b[i]==0x0c) or (b[i]==0x11) or (b[i]==0x17) or (b[i]==0x1b) or (b[i]==0x1c)or (b[i]==0x1d) or (b[i]==0x1e) or (b[i]==0x1f) or (b[i]==0x20) or (b[i]==0x21) or (b[i]==0x22) or (b[i]==0x23) or (b[i]==0x24) or (b[i]==0x25) or (b[i]==0x26) or (b[i]==0x27) or (b[i]==0x28) or (b[i]==0x29) or (b[i]==0x2a) or (b[i]==0x2b) or (b[i]==0x2c) or (b[i]==0x2d) or (b[i]==0x2e) or (b[i]==0x2f) or (b[i]==0x30) or (b[i]==0x31) or (b[i]==0x32) or (b[i]==0x33) or (b[i]==0x34) or (b[i]==0x36) or (b[i]==0x37) or (b[i]==0x39) or (b[i]==0x3c) or (b[i]==0x3d) or (b[i]==0x3e) or (b[i]==0x3f) or (b[i]==0x40) or (b[i]==0x41) or (b[i]==0x44) or (b[i]==0x45) or (b[i]==0x35))
end

writeBytes(0x2059E084,b)
end

[DISABLE]
{$lua}
timercharactersstand.Enabled = false
if readByte(0x2059DDA0,4) then
writeBytes(0x2059E084,0xFF)
writeBytes(0x2059E085,0xFF)
end

User avatar
LeFiXER
LeFixer
LeFixer
Posts: 489
Joined: Wed Mar 24, 2021 9:35 am
Reputation: 251

Re: Lua IF

Post by LeFiXER »

If I understand correctly, you want that the value 0xFF (255) is written to the addresses 0x2059E084/0x2059E085 if the value read at address 0x2059DDA0 is 4, but to ignore it if it is anything else? If you can elaborate a bit further what it is you're trying to achieve then maybe we can help more.

Lorizzuoso
Noobzor
Noobzor
Posts: 13
Joined: Wed Apr 20, 2022 6:17 pm
Reputation: 0

Re: Lua IF

Post by Lorizzuoso »

Yes indeed basically i'm trying to make a randomizer for a ps2 game but if the level with id 4 is loaded then the randomizer stop(which is that "for")and if the level id is not 4 anymore then the "for" can resume randomizing

User avatar
LeFiXER
LeFixer
LeFixer
Posts: 489
Joined: Wed Mar 24, 2021 9:35 am
Reputation: 251

Re: Lua IF

Post by LeFiXER »

This can be achieved with a timer instead of multiple types of loops.

Code: Select all

[ENABLE]
{$LUA}
if syntaxcheck then return end
if timer then timer.destroy(); timer = nil end

timer = createTimer(getMainForm())
timer.Interval = 50
timer.OnTimer = function()
		  r = math.random(1,255)
                  local level = readByte(0x2059DDA0,1)
		     if level == 4 then
		        timer.destroy()
			timer = nil
		     else
		        writeByte(0x2059E084,r)
			writeByte(0x2059E085,r)
		     end
	         end

{$ASM}
[DISABLE]
timer.destroy()
timer = nil
The timer will execute the function every 50 milliseconds. The objective of the function is to generate a random number between 1 and 255, then it will store that value in a global variable named "r". Then it will read a byte value from the address 0x2059DDA0 and store it in the variable level which is then checked against the value 4 in the if..else clause. If level does not equal 4 then it will write the random number (which is a byte value) to the addresses: 0x2059E084 and 0x2059E085.

If you're wondering why 255, it's because 255 is 0xFF in hex.

Lorizzuoso
Noobzor
Noobzor
Posts: 13
Joined: Wed Apr 20, 2022 6:17 pm
Reputation: 0

Re: Lua IF

Post by Lorizzuoso »

Error in line 22 timer.destroy():This instruction can't be compiled

User avatar
LeFiXER
LeFixer
LeFixer
Posts: 489
Joined: Wed Mar 24, 2021 9:35 am
Reputation: 251

Re: Lua IF

Post by LeFiXER »

Forgot the {$LUA} directive, sorry.

Code: Select all

...
[DISABLE]
{$LUA}
timer.destroy()
timer = nil

Lorizzuoso
Noobzor
Noobzor
Posts: 13
Joined: Wed Apr 20, 2022 6:17 pm
Reputation: 0

Re: Lua IF

Post by Lorizzuoso »

now there's this error : 2:attempt to index a niv value (global timer)

User avatar
LeFiXER
LeFixer
LeFixer
Posts: 489
Joined: Wed Mar 24, 2021 9:35 am
Reputation: 251

Re: Lua IF

Post by LeFiXER »

Lorizzuoso wrote:
Thu Apr 21, 2022 6:03 pm
now there's this error : 2:attempt to index a niv value (global timer)
Set timer as a local variable or give the timer another name. It worked fine for me upon testing.

Lorizzuoso
Noobzor
Noobzor
Posts: 13
Joined: Wed Apr 20, 2022 6:17 pm
Reputation: 0

Re: Lua IF

Post by Lorizzuoso »

I have the entire script here ,I implemented your script but it says violation access

[ENABLE]
{$lua}
b={}
timercharactersstand = createTimer(nil, false)
timercharactersstand.Interval = 20
timercharactersstand.Enabled = true
timercharactersstand.OnTimer = function(timer)

local level = readByte(0x2059DDA0,1)
if level == 4 then
timercharactersstand.destroy()
timer = nil
else
writeBytes(0x2059E084,0xFF)
writeBytes(0x2059E085,0xFF)
end

for i=1,2 do
repeat
b[i]=math.random(70)
until not ((b[i]==0x28) or (b[i]==0x09) or (b[i]==0x0a) or (b[i]==0x0c) or (b[i]==0x11) or (b[i]==0x17) or (b[i]==0x1b) or (b[i]==0x1c)or (b[i]==0x1d) or (b[i]==0x1e) or (b[i]==0x1f) or (b[i]==0x20) or (b[i]==0x21) or (b[i]==0x22) or (b[i]==0x23) or (b[i]==0x24) or (b[i]==0x25) or (b[i]==0x26) or (b[i]==0x27) or (b[i]==0x28) or (b[i]==0x29) or (b[i]==0x2a) or (b[i]==0x2b) or (b[i]==0x2c) or (b[i]==0x2d) or (b[i]==0x2e) or (b[i]==0x2f) or (b[i]==0x30) or (b[i]==0x31) or (b[i]==0x32) or (b[i]==0x33) or (b[i]==0x34) or (b[i]==0x36) or (b[i]==0x37) or (b[i]==0x39) or (b[i]==0x3c) or (b[i]==0x3d) or (b[i]==0x3e) or (b[i]==0x3f) or (b[i]==0x40) or (b[i]==0x41) or (b[i]==0x44) or (b[i]==0x45) or (b[i]==0x35) or (b[i]==0x46) or (b[i]==0x3a) or (b[i]==0x0b))
end

writeBytes(0x2059E084,b)
end

[DISABLE]
{$lua}
timercharactersstand.Enabled = false



User avatar
LeFiXER
LeFixer
LeFixer
Posts: 489
Joined: Wed Mar 24, 2021 9:35 am
Reputation: 251

Re: Lua IF

Post by LeFiXER »

The code I posted is a complete replacement for the script you posted:

Code: Select all

[ENABLE]
{$LUA}
if syntaxcheck then return end
if myTimer then myTimer.destroy(); myTimer = nil end

myTimer = createTimer(getMainForm())
myTimer.Interval = 50
myTimer.OnTimer = function()
		  local r = math.random(1,255)
                  local level = readByte(0x2059DDA0,1)
		     if level == 4 then
		        myTimer.destroy()
			myTimer = nil
		     else
		        writeByte(0x2059E084,r)
			writeByte(0x2059E085,r)
		     end
	         end

{$ASM}
[DISABLE]
{$LUA}
if myTimer then myTimer.destroy(); myTimer = nil end
Try this.

Lorizzuoso
Noobzor
Noobzor
Posts: 13
Joined: Wed Apr 20, 2022 6:17 pm
Reputation: 0

Re: Lua IF

Post by Lorizzuoso »

Ok solved it,thanks!

User avatar
LeFiXER
LeFixer
LeFixer
Posts: 489
Joined: Wed Mar 24, 2021 9:35 am
Reputation: 251

Re: Lua IF

Post by LeFiXER »

Lorizzuoso wrote:
Thu Apr 21, 2022 11:04 pm
Ok solved it,thanks!
Care to share for others how you solved it?

Lorizzuoso
Noobzor
Noobzor
Posts: 13
Joined: Wed Apr 20, 2022 6:17 pm
Reputation: 0

Re: Lua IF

Post by Lorizzuoso »

I just did this

[ENABLE]
{$lua}
b={}
timercharactersstand = createTimer(nil, false)
timercharactersstand.Interval = 20
timercharactersstand.Enabled = true
timercharactersstand.OnTimer = function()
local level = readByte(0x2059DDA0,1)
if level == 4 then
writeBytes(0x2059E085,0xFF)
else
for i=1,2 do
repeat
b[i]=math.random(70)
until not ((b[i]==0x28) or (b[i]==0x09) or (b[i]==0x0a) or (b[i]==0x0c) or (b[i]==0x11) or (b[i]==0x17) or (b[i]==0x1b) or (b[i]==0x1c)or (b[i]==0x1d) or (b[i]==0x1e) or (b[i]==0x1f) or (b[i]==0x20) or (b[i]==0x21) or (b[i]==0x22) or (b[i]==0x23) or (b[i]==0x24) or (b[i]==0x25) or (b[i]==0x26) or (b[i]==0x27) or (b[i]==0x28) or (b[i]==0x29) or (b[i]==0x2a) or (b[i]==0x2b) or (b[i]==0x2c) or (b[i]==0x2d) or (b[i]==0x2e) or (b[i]==0x2f) or (b[i]==0x30) or (b[i]==0x31) or (b[i]==0x32) or (b[i]==0x33) or (b[i]==0x34) or (b[i]==0x36) or (b[i]==0x37) or (b[i]==0x39) or (b[i]==0x3c) or (b[i]==0x3d) or (b[i]==0x3e) or (b[i]==0x3f) or (b[i]==0x40) or (b[i]==0x41) or (b[i]==0x44) or (b[i]==0x45) or (b[i]==0x35) or (b[i]==0x46) or (b[i]==0x3a) or (b[i]==0x0b))
writeBytes(0x2059E084,b)
end
end

end

[DISABLE]
{$lua}
timercharactersstand.Enabled = false
writeBytes(0x2059E084,0xFF)
writeBytes(0x2059E085,0xFF)



I added the "if" before the "for" and that worked

Frouk
Expert Cheater
Expert Cheater
Posts: 87
Joined: Wed Jun 30, 2021 10:21 am
Reputation: 16

Re: Lua IF

Post by Frouk »

we can set timer as local variable and destroy it when memory record is not active by adding this code in timer

Code: Select all

if not memrec.Active then t.destroy() t = nil return end --t represents timer 

Post Reply

Who is online

Users browsing this forum: No registered users