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 


Why is my code not working?

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> Cheat Engine
View previous topic :: View next topic  
Author Message
trifidix
Newbie cheater
Reputation: 0

Joined: 11 May 2022
Posts: 10

PostPosted: Thu May 12, 2022 8:04 am    Post subject: Why is my code not working? Reply with quote

I'm working with 2 addresses, reading the first (in-game match timer) and trying to adjust the value of the second based on the first, using a Lua script... but for some reason nothing happens, even when the checks should by all means pass.

Code:

matchTimer = readInteger('"game.exe"+addr1offset')

if matchTimer > 0 then -- Check if the match has started.

   timer = createTimer(nil)
   timer.OnTimer = max
   timer.OnTimer = zero
   timer.Interval = 20 -- 50 times a second, matches the in-game tick speed.
   timer.Enabled = true

   function max(timer)
            if (matchTimer + 50) % 800 == 0 then
            print("max check passed")
            writeInteger('"game.exe"+addr2offset', 11) -- Maxing out the value.
            end
   end

   function zero(timer)
            if (matchTimer - 1) % 800 == 0 then
            print("zero check passed")
            writeInteger('"game.exe"+addr2offset', 3) -- Resetting the value after some time.
            end
   end
end

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: Thu May 12, 2022 9:33 am    Post subject: Reply with quote

Code:

matchTimer = readInteger('"game.exe"+addr1offset')

if matchTimer > 0 then -- Check if the match has started.

   timer = createTimer(nil)
   timer.OnTimer = max
   timer.OnTimer = zero
   timer.Interval = 20 -- 50 times a second, matches the in-game tick speed.
   timer.Enabled = true

   function max(timer)
            if (matchTimer + 50) % 800 == 0 then
            print("max check passed")
            writeInteger('"game.exe"+addr2offset', 11) -- Maxing out the value.
            end
   end

   function zero(timer)
            if (matchTimer - 1) % 800 == 0 then
            print("zero check passed")
            writeInteger('"game.exe"+addr2offset', 3) -- Resetting the value after some time.
            end
   end
end


You must declare the functions before using them. The timer doesn't know what function max/zero is. Not only that, you can't set the timer to two functions simultaneously. Try this:
Code:

local matchTimer = readInteger('"game.exe"+addr1offset')

-- Check if the timer has already been created, if so then destroy it
if timer then timer.destroy(); timer = nil end

timer = createTimer(getMainForm())
timer.Interval = 20
timer.OnTimer = function()
      local max = (matchTimer + 50) % 800
      local zero = (matchTimer - 1) & 800

      if matchTimer > 0 then
         if matchTimer == max then
            writeInteger('"game.exe"+addr2offset', 11)
         elseif matchTimer == zero then
            writeInteger('"game.exe"+addr2offset', 3)
         end
      end
      end
Back to top
View user's profile Send private message
trifidix
Newbie cheater
Reputation: 0

Joined: 11 May 2022
Posts: 10

PostPosted: Thu May 12, 2022 10:03 am    Post subject: Reply with quote

Makes more sense, though this won't work - f.e. if the matchTimer was on 750 ticks (the point where I need to max out the value), this would take the 750 + 50 / 800 = 1, remaining 0, which doesn't match the matchTimer.

...to prevent further confusion - the matchTimer starts at 0 at the start of the match, increments by one 50 times a second and doesn't stop until the game ends, I need to set the 2nd address to 11 when the timer is at any multiple of 800 (-50), then set it back to 3 right after it hits the multiple of 800 (+1).

Though, one bigger problem that's preventing me from doing this - the matchTimer value in cheat engine seems to only update twice a second (not only the display one, but the actual value the script is reading), which is an issue I have absolutely no idea what to do about, since I need to have the script trigger on exact game ticks.

Okay THIS -

Code:

local matchTimer = readInteger('"game.exe"+AE0FC4')

if timer then timer.destroy(); timer = nil end

timer = createTimer(getMainForm())
timer.Interval = 20
timer.OnTimer = function()
      if matchTimer > 0 then
         if (matchTimer + 50) % 800 == 0 then
         print("Maxed")
         writeInteger('"game.exe"+D61974', 11)
         elseif (matchTimer - 1) % 800 == 0 then
         print("None")
         writeInteger('"game.exe"+D61974', 3)
         end
      end
end

- absolutely seems to work, except only when I set a value that triggers it manually, which leads me to believe that CE's update rate is what's causing it to fail.

So, is there any way to increase that, or maybe completely ditch the game timer for a one with the same 50 ticks/second starting with the match, which I create myself within the script? It's not like I know how, just an idea.
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: Thu May 12, 2022 10:42 am    Post subject: Reply with quote

You can adjust the update interval in the general settings of Cheat Engine. It's near the bottom; I believe the default is set to 500ms.
Back to top
View user's profile Send private message
ParkourPenguin
I post too much
Reputation: 140

Joined: 06 Jul 2014
Posts: 4289

PostPosted: Thu May 12, 2022 11:33 am    Post subject: Reply with quote

In that code you say works, nothing is updating the matchTimer variable. It gets initialized once to `readInteger('"game.exe"+AE0FC4')` and never gets changed again.
I haven't looked at this topic for more than a few seconds, but maybe you want to put that in the OnTimer function?
Code:
local matchTimer
...
timer.OnTimer = function(t)
  matchTimer = readInteger('"game.exe"+AE0FC4')
  ...


IIRC timers have the same resolution as getTickCount (usually about 16-17ms).
If you want exact timing, use code injection.

_________________
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
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> Cheat Engine 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