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 


Lua code slow reaction/computation time

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

Joined: 07 Apr 2016
Posts: 82
Location: U.S

PostPosted: Wed Feb 01, 2017 11:51 pm    Post subject: Lua code slow reaction/computation time Reply with quote

So this code that was working at an amazing pace (1 frame a second and faster) is now going 2-4 frames and slower for seemingly absolutely no reason, no change in code, nothing, however i got a new cpu, ram, and motherboard. I tried going back to the version of cheat engine I made the code on to no avail, so i'm wondering, just exactly what happened? Was it a game update? I turned off all other background processes that might be using excess cpu power, but in reality, the statistics show that CE is only using 5-8% MAX at all times, and overall i'm only using 50-60% with the game.
Code:
[ENABLE]
{$lua}
if(t ~= nil) then -- if script is already running then
    timer_setEnabled(t, false) -- stop the script
    object_destroy(t)
    t = nil
end

tickrate = 1 -- 50ms is every 3 frames

function main ()
bally = readInteger("[ballcoords]+1c")
ballx = readInteger("[ballcoords]+18")
p1y = readInteger("[p1coords]+1c")
p1x = readInteger("[p1coords]+18")
hitstun_countdown = readInteger(getAddress("[ballstate]+128"))/1092
if hitstun_countdown < 0 then hitstun_countdown = 0
end
if hitstun_countdown > 120 then hitstun_countdown = 0
end
bunted = readInteger(getAddress("[ballstate]+144"))
if bunted == 8 then hitstun_countdown = 0
end
if bunted == 3 then hitstun_countdown = 100
end
if bunted == 10 then hitstun_countdown = 0
end
end
  createHotkey(function(hk)
    local dx = readInteger("[ballcoords]+18")-readInteger("[p1coords]+18")
    local dy = readInteger("[p1coords]+1c")-readInteger("[ballcoords]+1c")
    if math.abs(dy) <= 5111808 and math.abs(dx) <= 9388608 and hitstun_countdown >= 0 and hitstun_countdown <= 1 then
      doKeyPress(VK_Z)
      end
       end, VK_D)

       t = createTimer(nil, false)  -- create a Timer object and assign it to variable t
timer_onTimer(t, main)   -- When the timer ticks, call the function main
timer_setInterval(t, tickrate) -- Sets the tickrate of the timer in milliseconds
timer_setEnabled(t, true) -- Turns the timer on -- Lua code here
{$asm}
// other assembly code
[DISABLE]
// etc...
Back to top
View user's profile Send private message
Dark Byte
Site Admin
Reputation: 458

Joined: 09 May 2003
Posts: 25287
Location: The netherlands

PostPosted: Thu Feb 02, 2017 2:30 am    Post subject: Reply with quote

i assume you also installed a bit of new software?

Did you get a new anti virus? if so, that can be a reason

same OS?

Also, the game running at 50% doesn't say much. If you're on a 8 core system, and the game is designed to only use 4 cores, then when it's using 100% of it's speed, cpu usage will be 50%

_________________
Do not ask me about online cheats. I don't know any and wont help finding them.

Like my help? Join me on Patreon so i can keep helping
Back to top
View user's profile Send private message MSN Messenger
microsoftv
Advanced Cheater
Reputation: 0

Joined: 07 Apr 2016
Posts: 82
Location: U.S

PostPosted: Thu Feb 02, 2017 11:10 am    Post subject: Reply with quote

I probably have installed new software, no new antivirus, different Windows drive, yes it's using 4 cores.

EDIT: I've actually found that instead of the whole of lua code not working, it's only this piece that acts as though it lags
Code:
  createHotkey(function(hk)
        local hitstun_countdown = readInteger(getAddress("[ballstate]+128"))/1092
    local dx = readInteger("[ballcoords]+18")-readInteger("[p1coords]+18")
    local dy = readInteger("[p1coords]+1c")-readInteger("[ballcoords]+1c")
    if hitstun_countdown < 0 then hitstun_countdown = 0
    end
    local bunted = readInteger(getAddress("[ballstate]+144"))
    if bunted == 8 then hitstun_countdown = 0
    end
    if bunted == 3 then hitstun_countdown = 100
    end
    if bunted == 10 then hitstun_countdown = 0
    end
    if hitstun_countdown > 120 then hitstun_countdown = 0
  end
    if math.abs(dy) <= 5111808 and math.abs(dx) <= 8388608 and hitstun_countdown >= 0 and hitstun_countdown <= 1 then
      doKeyPress(VK_Z)
      end
       end, VK_D)

But of course this never happened before, it worked perfectly before I had got a new hard drive. I re-installed CE and uninstalled unneeded programs, to no effect.
Back to top
View user's profile Send private message
Zanzer
I post too much
Reputation: 126

Joined: 09 Jun 2013
Posts: 3278

PostPosted: Thu Feb 02, 2017 9:45 pm    Post subject: Reply with quote

Well of course. That code gets spammed while you hold down "D".
You could try making the code slightly more efficient, or adjust the interval that it spams.
Also note that each time you execute the script, it is duplicating the hotkey. Destroy the old one.
Code:
if myhotkey then
  myhotkey.Destroy()
end
myhotkey = createHotkey(function(hk)
  local ballstate = getAddress("[ballstate]")
  local ballcoords = getAddress("[ballcoords]")
  local p1coords = getAddress("[p1coords]")
  local hitstun_countdown = readInteger(ballstate+0x128) / 1092
  local dx = readInteger(ballcoords+0x18) - readInteger(p1coords+0x18)
  local dy = readInteger(p1coords+0x1C) - readInteger(ballcoords+0x1C)
  if hitstun_countdown < 0 then
    hitstun_countdown = 0
  end
  local bunted = readInteger(ballstate+0x144)
  if bunted == 8 then
    hitstun_countdown = 0
  end
  if bunted == 3 then
    hitstun_countdown = 100
  end
  if bunted == 10 then
    hitstun_countdown = 0
  end
  if hitstun_countdown > 120 then
    hitstun_countdown = 0
  end
  if math.abs(dy) <= 5111808 and math.abs(dx) <= 8388608 and hitstun_countdown >= 0 and hitstun_countdown <= 1 then
    doKeyPress(VK_Z)
  end
end, VK_D)
myhotkey.DelayBetweenActivate = 300
Back to top
View user's profile Send private message
microsoftv
Advanced Cheater
Reputation: 0

Joined: 07 Apr 2016
Posts: 82
Location: U.S

PostPosted: Thu Feb 02, 2017 10:02 pm    Post subject: Reply with quote

Zanzer wrote:
Well of course. That code gets spammed while you hold down "D".

This isn't a needed solution, my code was working the way it is coded now, before, and it worked even if holding down the hotkey D, also there was a lot less code put in the hotkey function than is shown in your code example.
Code:
[ENABLE]
{$lua}
if(t ~= nil) then -- if script is already running then
    timer_setEnabled(t, false) -- stop the script
    object_destroy(t)
    t = nil
end

tickrate = 10 -- 50ms is every 3 frames

function main ()
  createHotkey(function(hk)
    local dx = readInteger("[ballcoords]+18")-readInteger("[p1coords]+18")
    local dy = readInteger("[p1coords]+1c")-readInteger("[ballcoords]+1c")
    if math.abs(dy) <= 5111808 and math.abs(dx) <= 8388608 and hitstun_countdown >= 0 and hitstun_countdown <= 1 then
      doKeyPress(VK_Z)
      end
       end, VK_D)

       t = createTimer(nil, false)  -- create a Timer object and assign it to variable t
timer_onTimer(t, main)   -- When the timer ticks, call the function main
timer_setInterval(t, tickrate) -- Sets the tickrate of the timer in milliseconds
timer_setEnabled(t, true) -- Turns the timer on -- Lua code here
{$asm}
// other assembly code
[DISABLE]
// etc...
There's more code not shown here, but these are the important parts. Everything works like it did before, except this hotkey code.
Back to top
View user's profile Send private message
microsoftv
Advanced Cheater
Reputation: 0

Joined: 07 Apr 2016
Posts: 82
Location: U.S

PostPosted: Fri Feb 03, 2017 9:32 pm    Post subject: Reply with quote

Bumping, as this is still in effect as a problem/bug.
Back to top
View user's profile Send private message
ParkourPenguin
I post too much
Reputation: 140

Joined: 06 Jul 2014
Posts: 4289

PostPosted: Fri Feb 03, 2017 9:57 pm    Post subject: Reply with quote

  • Your formatting is terrible: properly indent your code.
  • The comments are incorrect/useless and make me think you just copied and pasted that code from someone else.
  • Most class_method functions are old, undocumented, possibly deprecated, and less readable than their modern counterparts.
  • There's no end to the function main in that example.
  • The timer is calling main which creates a hotkey every time it's run. After the first few thousand hotkeys, I'm not surprised it's lagging a bit when the key is pressed.
  • Use a more unique name for t since it's a global.

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

Joined: 07 Apr 2016
Posts: 82
Location: U.S

PostPosted: Sat Feb 04, 2017 12:35 am    Post subject: Reply with quote

ParkourPenguin wrote:
  • Your formatting is terrible: properly indent your code.
  • The comments are incorrect/useless and make me think you just copied and pasted that code from someone else.
  • Most class_method functions are old, undocumented, possibly deprecated, and less readable than their modern counterparts.
  • There's no end to the function main in that example.
  • The timer is calling main which creates a hotkey every time it's run. After the first few thousand hotkeys, I'm not surprised it's lagging a bit when the key is pressed.
  • Use a more unique name for t since it's a global.

Can you give examples of modern counterparts of the class_method functions?
The example was just a snippet, the full code includes the end before the create hotkey function to end main.
Is there a better way to use the timer function, or a better timer function? Also, the lagging ONLY happens on that specific createhotkey function, all other frame perfect (frame 1 or less) inputs are perfect and react like they should. Example is:
Code:
if isKeyPressed(VK_C) and hitstun_countdown >= 0 and hitstun_countdown <= 1 then
doKeyPress(VK_Z)
end
hitstun_countdown is in frames (the 0-1 is if it's within a 1 frame window, then press a button)
I can hold down this button and it will react in 1 frame every time, but the create hotkey function acts as though something has changed, even though it worked just as perfectly as this example before.
What name would you suggest for t?
Back to top
View user's profile Send private message
ParkourPenguin
I post too much
Reputation: 140

Joined: 06 Jul 2014
Posts: 4289

PostPosted: Sat Feb 04, 2017 9:48 am    Post subject: Reply with quote

Code:
if hitstunUpdateT then
  hitstunUpdateT.destroy()
end

...

hitstunUpdateT = createTimer()
hitstunUpdateT.Interval = 10
hitstunUpdateT.OnTimer = main


Check the global keypoll interval and the delay between hotkey activation. An example of a basic test you can do:
Code:
--setGlobalKeyPollInterval(100)
--setGlobalDelayBetweenHotkeyActivation(100)

if hk then hk.destroy() end

local oldT = getTickCount()  -- resolution isn't great, but good enough
hk = createHotkey(function()
  local nowT = getTickCount()
  print(nowT-oldT)
  oldT = nowT
end, VK_Z)

-- if you don't want to affect all other hotkeys, just change this hotkey:
--hk.DelayBetweenActivate = 100

If the hotkey doesn't run as fast as you would like, change how often CE checks keys and/or how much time needs to pass for the hotkey to be able to run again.

Take into account what Zanzer said:
Zanzer wrote:
Also, note that each time you execute the script, it is duplicating the hotkey. Destroy the old one.


If you already have some function running in a timer faster than the game draws frames, why not just check if some key is pressed in the timer and put the hotkey code there?

PS: there is a keyword called "elseif" in Lua.

_________________
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 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