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 


IF conditional statement in lua trainer
Goto page 1, 2  Next
 
Post new topic   Reply to topic    Cheat Engine Forum Index -> Cheat Engine Lua Scripting
View previous topic :: View next topic  
Author Message
Tum
Cheater
Reputation: 0

Joined: 10 Jan 2010
Posts: 28

PostPosted: Wed Jun 07, 2017 8:46 am    Post subject: IF conditional statement in lua trainer Reply with quote

Hi,
How do i go about having a IF statement in my trainer LUA ?

I'd like it to change and freeze the byte within an address, IF the byte is lower than a certain amount.

Heres my code at the minute.

Quote:
function CEToggleBox2Click(sender)
if UDF1.CEToggleBox2.Checked then
CheatEntry = addresslist.getMemoryRecord(1)
CheatEntry.Value = "6.8"
CheatEntry.Active = true
else
CheatEntry.Active = false
end
end


But i only want it to change and freeze the value to 6.8, IF the value originally less than 7.7

This is the code i have been trying, but it doesnt seem to work.

Quote:
function CEToggleBox2Click(sender)
if UDF1.CEToggleBox2.Checked then
CheatEntry = addresslist.getMemoryRecord(1)
if CheatEntry.Value < "7.7" then
CheatEntry.Value = "6.8"
CheatEntry.Active = true -- activate script / freeze value
end
else
CheatEntry.Active = false -- unfreeze
end
end
Back to top
View user's profile Send private message
Zanzer
I post too much
Reputation: 126

Joined: 09 Jun 2013
Posts: 3278

PostPosted: Wed Jun 07, 2017 7:20 pm    Post subject: Reply with quote

Code:
CheatEntry = addresslist.getMemoryRecord(0)
Back to top
View user's profile Send private message
Tum
Cheater
Reputation: 0

Joined: 10 Jan 2010
Posts: 28

PostPosted: Thu Jun 08, 2017 7:46 am    Post subject: Reply with quote

Memory Record 0 is not the cheat am using, that is a different cheat.
Memory record 1 is the cheat am trying to add this to.

I already have this script working, am just trying to adjust it so that it will only activate if the current value, of memory record 1 is less than 7.7

Am not sure why the second script i posted in my first post, is not working, as that has the first IF statement to check whether the cheat is activated on the trainer, and the second IF statement is checking the value of the memory address that contains my character movement speed.

Obviously i have something wrong though for it not to work.
Maybe its something to do with the value been a FLOAT, and its checking it as a string ?
Back to top
View user's profile Send private message
FreeER
Grandmaster Cheater Supreme
Reputation: 53

Joined: 09 Aug 2013
Posts: 1091

PostPosted: Thu Jun 08, 2017 8:17 am    Post subject: Reply with quote

Tum wrote:
Maybe its something to do with the value been a FLOAT, and its checking it as a string ?


Pretty sure CE returns all values as a string (at least based on the main.lua/celua.txt file) and based on a quick test lua will compare strings as numbers.

hm, that code seems to work for me for both int values and float (I had to use 0 instead of 1 for the memory record since it's the first/only record in my test, but even if you'd used the id instead of the list position I'd have probably had to change it), though since it only runs when you click the togglebox it's not constantly checking to see if the value is less than 7.7 and then freezing it, simply checking once when you clicked it. Not sure if that's what you were hoping for but if so then you'd need your code to create a timer when it's activated and stop/destroy it when it's deactivated (unchecked).
Back to top
View user's profile Send private message
Tum
Cheater
Reputation: 0

Joined: 10 Jan 2010
Posts: 28

PostPosted: Thu Jun 08, 2017 8:23 am    Post subject: Reply with quote

Yes i think that might be the problem on only checking while the box has been checked.

What am trying to do is this.

My normal character run speed is a float of 4.8 or something.
And when i activate the cheat, i increase my run speed to 6.8

But in game, if i use a mount, my mount speed changes my movement speed to 7.7

So am trying to get it to not activate the moment i mount up, but when i jump off my mount to activate.


Am guessing i might need to go about this a different method ?
Back to top
View user's profile Send private message
ParkourPenguin
I post too much
Reputation: 138

Joined: 06 Jul 2014
Posts: 4275

PostPosted: Thu Jun 08, 2017 8:50 am    Post subject: Reply with quote

FreeER wrote:
...based on a quick test lua will compare strings as numbers.
Code:
print(tostring('35'<'7'))  -- prints true
print(tostring(35 < 7))    -- prints false

If you want to convert the Value property of a memory record to a number, wrap it in a call to tonumber.
Code:
tonumber(CheatEntry.Value)

_________________
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
Tum
Cheater
Reputation: 0

Joined: 10 Jan 2010
Posts: 28

PostPosted: Thu Jun 08, 2017 9:25 am    Post subject: Reply with quote

Nice 1 thanks, ill remember that one.

But honestly now reading the post above, I do not think that is my problem.

My problem is, The trainer is called when the togglebox is pressed.
That changes my movement speed to the 6.8

But then the trainer is not called again until i press a toggle button again, Therefore the code is never checked again, to check if my movement speed had increased to 7.7 (when mounted).
Back to top
View user's profile Send private message
ParkourPenguin
I post too much
Reputation: 138

Joined: 06 Jul 2014
Posts: 4275

PostPosted: Thu Jun 08, 2017 9:34 am    Post subject: Reply with quote

So use a timer. It will periodically call a function to run while it's enabled. In that function, set the movement speed to its current value or 6.8- whichever is greater. In the togglebox's OnClick event, set the timer's Enabled property to the togglebox's Checked property.

Use Google for examples.

_________________
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
Tum
Cheater
Reputation: 0

Joined: 10 Jan 2010
Posts: 28

PostPosted: Thu Jun 08, 2017 10:04 am    Post subject: Reply with quote

Thanks ill google it and try and figure that out.

EDIT


Ok, ive had to edit this as i cannot double post yet.

OK, i have had a go at figuring this out, but still not working.
This time, it doesnt even look like the cheat deactivates when i disable my cheat on my trainer.

Heres the code am trying at present

Quote:
function CEToggleBox2Click(sender)
if UDF1.CEToggleBox2.Checked then
moveSpeed = createTimer()
moveSpeed.Interval = 100
moveSpeed.OnTimer = MovementSpeed
moveSpeed.Enabled = true
else
moveSpeed.Enabled = false
end
end
function MovementSpeed()
CheatEntry = addresslist.getMemoryRecord(1)
if CheatEntry.Value < "7.7" then
CheatEntry.Value = "6.8"
CheatEntry.Active = true -- activate script / freeze value
else
CheatEntry.Active = false -- unfreeze
end
end





EDIT 2:

Ok, I thought i had finally figure this out, but i havent Sad
This script below, worked once or twice. But then for some reason it doesnt work anymore.

Anyone know why this might only work now and again please ?


Quote:
addresslist = getAddressList()
mainForm = getMainForm()

moveSpeed = createTimer(mainForm, false)
moveSpeed.Interval = 100
moveSpeed.OnTimer = MovementSpeed

function CEToggleBox2Click(sender)
CheatEntry = addresslist.getMemoryRecord(1)
if UDF1.CEToggleBox2.Checked then
moveSpeed.Enabled = true
else
moveSpeed.Enabled = false
CheatEntry.Active = false
end
end
function MovementSpeed()
CheatEntry = addresslist.getMemoryRecord(1)
if CheatEntry.Value < "7.7" then
CheatEntry.Value = "6.8"
CheatEntry.Active = true -- activate script / freeze value
else
CheatEntry.Active = false -- unfreeze
end
end
Back to top
View user's profile Send private message
FreeER
Grandmaster Cheater Supreme
Reputation: 53

Joined: 09 Aug 2013
Posts: 1091

PostPosted: Thu Jun 08, 2017 3:11 pm    Post subject: Reply with quote

Glad you got it working Tum! Smile

thanks for the correction ParkourPenguin!
Of course then I had to test what happens if it's set to show as hex, short answer you'll need to use tonumber(..., 16)

test
Code:
al = getAddressList()
mr1 = al.getMemoryRecord(0)
mr2 = al.getMemoryRecord(1)

print(mr1.Value)
print(mr2.Value)
print("")
print(tonumber(mr1.Value))
print(tonumber(mr2.Value))
print("")
print(tonumber(mr1.Value))
print(tonumber(mr2.Value,16))


result
Code:
96
00000060
 
96
60
 
96
96


you could use a function like this to handle either case:

Code:
function memRecValueToNumber(mr)
  if not mr then return nil end
  if not mr.className == "TMemoryRecord" then return nil end
  -- assume the type is a number not string, array of bytes, etc.

  local base = mr.ShowAsHex and 16 or 10
  return tonumber(mr.Value, base)
end

print(memRecValueToNumber(mr1))
print(memRecValueToNumber(mr2))


Note, of course, that ShowAsSigned matters as well, however since that is only applicable to decimal values not hex I didn't do anything for it.
Back to top
View user's profile Send private message
Tum
Cheater
Reputation: 0

Joined: 10 Jan 2010
Posts: 28

PostPosted: Thu Jun 08, 2017 3:18 pm    Post subject: Reply with quote

Ye, thanks a lot for everyones help today.

Heres the full working part of the script. The last script i posted wasnt working all the time cause i think i had the timer interval set to slow at 100, so i changed the interval to 10 and it works fine now.

Quote:
addresslist = getAddressList()
mainForm = getMainForm()

moveSpeed = createTimer(mainForm, false)
moveSpeed.Interval = 10
moveSpeed.OnTimer = MovementSpeed

function CEToggleBox2Click(sender)
CheatEntry = addresslist.getMemoryRecord(1)
if UDF1.CEToggleBox2.Checked then
moveSpeed.Enabled = true
else
moveSpeed.Enabled = false
CheatEntry.Active = false
end
end
function MovementSpeed()
CheatEntry = addresslist.getMemoryRecord(1)
if CheatEntry.Value < "7.7" then
CheatEntry.Value = "6.8"
CheatEntry.Active = true -- activate script / freeze value
else
CheatEntry.Active = false -- unfreeze
end
end
Back to top
View user's profile Send private message
ParkourPenguin
I post too much
Reputation: 138

Joined: 06 Jul 2014
Posts: 4275

PostPosted: Thu Jun 08, 2017 3:51 pm    Post subject: Reply with quote

There's a few problems with that code.

When you assign the function MovementSpeed to the OnTimer property, that function hasn't been defined yet. As it is now, every time you execute the code, the timer uses the previous definition of the MovementSpeed function instead of the one written in the script. Move the function definition above the assignment to OnTimer (or just use an anonymous function).

You don't need to use the Active property of the memory record if you're using a timer. Just assign whatever value you want to the memory record.

You neglected the aforementioned discrepancy between string comparison and number comparison.

This is a small semantic error in that you should be using the togglebox's OnChange event instead of the OnClick event, but it shouldn't make any significant difference.

Here's an example of another way it could be done:
Code:
local speedMemRec = getAddressList()[1]

-- for testing purposes, destroy the timer if it already exists
if moveSpeedT then moveSpeedT.destroy() end

moveSpeedT = createTimer(UDF1, false)
moveSpeedT.Interval = getFreezeTimer().Interval  -- copy interval from settings
moveSpeedT.OnTimer = function(t)
  if tonumber(speedMemRec.Value) < 6.8 then
    speedMemRec.Value = '6.8'
  end
end

function CEToggleBox2Click(sender)
  moveSpeed.Enabled = sender.Checked
end

PS: Use the code tag for code and the quote tag for quotes.

_________________
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
Tum
Cheater
Reputation: 0

Joined: 10 Jan 2010
Posts: 28

PostPosted: Fri Jun 09, 2017 8:52 am    Post subject: Reply with quote

Whoops, sorry i had missed some bits of the code that i pasted last time.
This is the script with the timer destroy in as well.

This code is working fine for me at the moment.

Has this code still got errors in it please ?

Code:
addresslist = getAddressList()
mainForm = getMainForm()

function CEToggleBox2Click(sender)
         CheatEntry = addresslist.getMemoryRecord(1)
         if UDF1.CEToggleBox2.Checked then
            moveSpeed = createTimer(mainForm, false)
            moveSpeed.Interval = 10
            moveSpeed.OnTimer = MovementSpeed
            moveSpeed.Enabled = true
         else
             moveSpeed.Enabled = false
             CheatEntry.Active = false
             moveSpeed.destroy()
         end
end
function MovementSpeed()
        CheatEntry = addresslist.getMemoryRecord(1)
        if CheatEntry.Value < "7.6" then
            CheatEntry.Value = "6.8"
            CheatEntry.Active = true
        else
            CheatEntry.Active = false
        end
end
Back to top
View user's profile Send private message
ParkourPenguin
I post too much
Reputation: 138

Joined: 06 Jul 2014
Posts: 4275

PostPosted: Fri Jun 09, 2017 8:58 am    Post subject: Reply with quote

Tum wrote:
Has this code still got errors in it please ?

ParkourPenguin wrote:
You don't need to use the Active property of the memory record if you're using a timer. Just assign whatever value you want to the memory record.

You neglected the aforementioned discrepancy between string comparison and number comparison.

This is a small semantic error in that you should be using the togglebox's OnChange event instead of the OnClick event, but it shouldn't make any significant difference.

I also don't see why you are continuously creating or destroying the timer every time you click the togglebox, but if it works, it works.

_________________
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
Tum
Cheater
Reputation: 0

Joined: 10 Jan 2010
Posts: 28

PostPosted: Fri Jun 09, 2017 1:31 pm    Post subject: Reply with quote

Am not sure what you mean by using the active property of the memory record.
The method am using is just one from a guide i found.
I will certainly study the method you've shown me above to figure it all out.

Ye am not sure why i have to have the create timer within my togglebox function. But when i moved it out of the function, it didnt work for me.

Maybe its something to do with the function constantly destroying the timer if the togglebox is false, and it destroys the timer before the the togglebox is true ?
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
Goto page 1, 2  Next
Page 1 of 2

 
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