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 


Check if a value is increased by exactly xx amount

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> Cheat Engine Lua Scripting
View previous topic :: View next topic  
Author Message
Flux.
Advanced Cheater
Reputation: 0

Joined: 10 Nov 2016
Posts: 88
Location: Another World - N5X2 106311411+2123518

PostPosted: Mon Feb 24, 2020 1:35 pm    Post subject: Check if a value is increased by exactly xx amount Reply with quote

Hello,
After some more help with lua.
What i want to do is - check if a value is increased by exactly xx amount.

So i can use
local z = getAddressList().getMemoryRecordByDescription("test") for the value,
and a timer to keep checking for a change.
But stuck on the function part, how to check if the z value is increased by exactly 100 ? and if it is then do something.
Any help appreciated.
Back to top
View user's profile Send private message
ParkourPenguin
I post too much
Reputation: 152

Joined: 06 Jul 2014
Posts: 4716

PostPosted: Mon Feb 24, 2020 1:54 pm    Post subject: Reply with quote

Code:
local memrec = AddressList.getMemoryRecordByDescription('test')
local oldval = tonumber(memrec.Value)

timer.OnTimer = function()
  local newval = tonumber(memrec.Value)
  if newval - oldval == 100 then
    -- code
  end
  oldval = newval
end

_________________
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
Flux.
Advanced Cheater
Reputation: 0

Joined: 10 Nov 2016
Posts: 88
Location: Another World - N5X2 106311411+2123518

PostPosted: Mon Feb 24, 2020 3:30 pm    Post subject: Reply with quote

Hello ParkourPenguin.

Thank you for the reply,
I have just tried your script, and its works pretty well, it doesn't grab the value every time, but most of the time it does.
Here is a full script for anyone who may need it.
Code:
{$lua}
[ENABLE]
local memrec = AddressList.getMemoryRecordByDescription("test")
local oldval = tonumber(memrec.Value)

timer = createTimer(nil)
timer.setInterval = 100
timer.OnTimer = function()
  local newval = tonumber(memrec.Value)
  if newval - oldval == 100 then
    print('success')
  end
  oldval = newval
end

[DISABLE]
timer_setEnabled(timer, false)

Thanks again, much appreciated.
Back to top
View user's profile Send private message
ParkourPenguin
I post too much
Reputation: 152

Joined: 06 Jul 2014
Posts: 4716

PostPosted: Mon Feb 24, 2020 4:22 pm    Post subject: Reply with quote

setInterval is a function, not a property, and most of those old underscore function calls should be replaced now.
Code:
timer.Interval = 100
...
timer.Enabled = false

If it still doesn't catch everything, use a lower interval on the timer or a breakpoint instead of a timer.

_________________
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
Flux.
Advanced Cheater
Reputation: 0

Joined: 10 Nov 2016
Posts: 88
Location: Another World - N5X2 106311411+2123518

PostPosted: Tue Feb 25, 2020 4:31 am    Post subject: Reply with quote

Hello ParkourPenguin,

Thank you for the info about the timer.

After some testing this morning, a new problem has arisen -
For instance the value on my cheat table shows in hex 0100, (2 bytes)
in the script if i use this 0x100 or 256 as the value it does not work, but simply using 100 does,
Shouldn't it accept 0x100 in the script instead of 100, its a little confusing.

Anyway the problem is once the value reaches 9999 and changes to A000,
the arithmetic breaks and lua throws errors every interval,
is there a fix for using when hex values populate the address ?
Back to top
View user's profile Send private message
ParkourPenguin
I post too much
Reputation: 152

Joined: 06 Jul 2014
Posts: 4716

PostPosted: Tue Feb 25, 2020 11:07 am    Post subject: Reply with quote

What do you mean by "use 0x100"? Are you setting the value of the memory record to that? If so, the Value property is a string, not a number. CE parses that string using other properties of the memory record (i.e. show as hex) to write the correct value to the address. So, if you're showing it as hex and you want to write 0x100 to the address, do this:
Code:
memrec.Value = '100'


It probably breaks because tonumber is translating a hex string as if it were in base 10 and/or you're trying to do arithmetic on a string. Do this instead:
Code:
local v = tonumber(memrec.Value, memrec.ShowAsHex and 16 or 10)
-- do arithmetic on v
v = v + 10
memrec.Value = (memrec.ShowAsHex and '%X' or '%d'):format(v)
If you want more specific help, post the code and I can tell you what's wrong.
_________________
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
Flux.
Advanced Cheater
Reputation: 0

Joined: 10 Nov 2016
Posts: 88
Location: Another World - N5X2 106311411+2123518

PostPosted: Tue Feb 25, 2020 12:38 pm    Post subject: Reply with quote

Hello ParkourPenguin,

Thanks or explaining, that make sense now, as for the script i am obviously doing something wrong,
after adding the code you posted it now constantly prints 10, here is the modified script -
Code:
{$lua}
[ENABLE]
local memrec = AddressList.getMemoryRecordByDescription("test")
local v = tonumber(memrec.Value, memrec.ShowAsHex and 16 or 10)
v = v + 10
memrec.Value = (memrec.ShowAsHex and '%X' or '%d'):format(v)

local oldval = v

timer = createTimer(nil)
timer.Interval = 1000
timer.OnTimer = function()
    print(oldval)
  local newval = v
    print(newval)
  if (newval - oldval == 100) then
    print('success')
  end
  oldval = newval
end

[DISABLE]
timer.Enabled = false
Back to top
View user's profile Send private message
ParkourPenguin
I post too much
Reputation: 152

Joined: 06 Jul 2014
Posts: 4716

PostPosted: Tue Feb 25, 2020 1:39 pm    Post subject: This post has 1 review(s) Reply with quote

Don't just copy and paste stuff literally. Try to read the code and understand what it's doing.
Code:
{$lua}
[ENABLE]
local memrec = AddressList.getMemoryRecordByDescription("test")
local oldval = tonumber(memrec.Value, memrec.ShowAsHex and 16 or 10)

timer = createTimer()
timer.Interval = 1000
timer.OnTimer = function()
  print(oldval)
  local newval = tonumber(memrec.Value, memrec.ShowAsHex and 16 or 10)
  print(newval)
  if (newval - oldval == 100) then
    print('success')
  end
  oldval = newval
end

[DISABLE]
if timer then
  timer.Enabled = false
  timer.destroy()
  timer = nil
end

_________________
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
Flux.
Advanced Cheater
Reputation: 0

Joined: 10 Nov 2016
Posts: 88
Location: Another World - N5X2 106311411+2123518

PostPosted: Tue Feb 25, 2020 2:31 pm    Post subject: Reply with quote

Hello ParkourPenguin,

this script does indeed work, thank you for that, much appreciated.

The values that are printed are now in dec , and using 0x100 in the script works fine, i have also just changed the timer interval which also works a little better.
what are the advantages of using a breakpoint instead of a timer, if its worth doing could you give an example.

Many thanks for the help with table.
Back to top
View user's profile Send private message
ParkourPenguin
I post too much
Reputation: 152

Joined: 06 Jul 2014
Posts: 4716

PostPosted: Tue Feb 25, 2020 5:26 pm    Post subject: Reply with quote

To print numbers in hex:
Code:
print(('%X'):format(255))


The timer checks it periodically. If the game writes to the value faster than the timer checks the value, it might not work.

Breakpoints will trigger every time the value is written to. It's guaranteed to catch every write to the address (more or less). However, the number of hardware breakpoints is limited, but this shouldn't be a problem if you're not monitoring a bunch of addresses at once.
Code:
{$lua}
if syntaxcheck then return end
local memrec = AddressList.getMemoryRecordByDescription("test")

[ENABLE]
local oldval = tonumber(memrec.Value, memrec.ShowAsHex and 16 or 10)
local size_lookup = {
  [vtByte] = 1,
  [vtWord] = 2,
  [vtDword] = 4,
  [vtQword] = 8
}

debug_setBreakpoint(memrec.Address, assert(size_lookup[memrec.Type],'Unsupported type'),
    bptWrite, bpmDebugRegister, function()
      print(('%X'):format(oldval))
      local newval = tonumber(memrec.Value, memrec.ShowAsHex and 16 or 10)
      print(('%X'):format(newval))
      if (newval - oldval == 100) then
        print('success')
      end
      oldval = newval
      debug_continueFromBreakpoint(co_run)
      return 0
    end)

[DISABLE]
debug_removeBreakpoint(memrec.Address)
(the if syntaxcheck... line at the top is important for Lua in AA scripts)
_________________
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
Flux.
Advanced Cheater
Reputation: 0

Joined: 10 Nov 2016
Posts: 88
Location: Another World - N5X2 106311411+2123518

PostPosted: Wed Feb 26, 2020 11:26 am    Post subject: Reply with quote

Thank you ParkourPenguin.

Going to give this a good testing now, thank you for all the help.
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