[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
Lua IF
-
- Noobzor
- Posts: 13
- Joined: Wed Apr 20, 2022 6:17 pm
- Reputation: 0
Lua IF
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.
Re: Lua IF
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.
-
- Noobzor
- Posts: 13
- Joined: Wed Apr 20, 2022 6:17 pm
- Reputation: 0
Re: Lua IF
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
Re: Lua IF
This can be achieved with a timer instead of multiple types of loops.
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.
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
If you're wondering why 255, it's because 255 is 0xFF in hex.
-
- Noobzor
- Posts: 13
- Joined: Wed Apr 20, 2022 6:17 pm
- Reputation: 0
Re: Lua IF
Error in line 22 timer.destroy():This instruction can't be compiled
Re: Lua IF
Forgot the {$LUA} directive, sorry.
Code: Select all
...
[DISABLE]
{$LUA}
timer.destroy()
timer = nil
-
- Noobzor
- Posts: 13
- Joined: Wed Apr 20, 2022 6:17 pm
- Reputation: 0
Re: Lua IF
now there's this error : 2:attempt to index a niv value (global timer)
Re: Lua IF
Set timer as a local variable or give the timer another name. It worked fine for me upon testing.Lorizzuoso wrote: ↑Thu Apr 21, 2022 6:03 pmnow there's this error : 2:attempt to index a niv value (global timer)
-
- Noobzor
- Posts: 13
- Joined: Wed Apr 20, 2022 6:17 pm
- Reputation: 0
Re: Lua IF
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
Re: Lua IF
The code I posted is a complete replacement for the script you posted:
Try this.
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
-
- Noobzor
- Posts: 13
- Joined: Wed Apr 20, 2022 6:17 pm
- Reputation: 0
Re: Lua IF
Ok solved it,thanks!
Re: Lua IF
Care to share for others how you solved it?
-
- Noobzor
- Posts: 13
- Joined: Wed Apr 20, 2022 6:17 pm
- Reputation: 0
Re: Lua IF
I just did this
I added the "if" before the "for" and that worked
[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
Re: Lua IF
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
Who is online
Users browsing this forum: No registered users