Why is my AHK Script Lagging? Topic is solved

Ask gaming related questions (AHK v1.1 and older)
Maimed
Posts: 7
Joined: 02 Dec 2022, 04:45

Why is my AHK Script Lagging?

13 Feb 2024, 12:35

Hi, i'm having a problem with my ahk script being super laggy when i press "q". and when i release the 'q' button, the script continues to spam 'q' and 'lbutton', and the fps drops significantly in my game each time i press 'q'. i've tried extending the timer and using really small 1x3 pixel images for pattern matching, but it's still lagging. can someone help me figure out why it's so laggy and suggest how to speed it up? if it's not the timer or the images, i'm not sure what else could be causing it. here's the script:

Code: Select all

global agilityPixelPattern := "D:\agility_pixel_pattern.png"
global chatPixelPattern := "D:\chat_pixel_pattern.png"
global consolePixelPattern := "D:\console_pixel_pattern.png"

; Calculate the coordinates for the bottom half, top half, left half, and right half of the screen
bottomHalfHeight := A_ScreenHeight / 2
topHalfHeight := bottomHalfHeight
leftHalfWidth := A_ScreenWidth / 2
rightHalfWidth := leftHalfWidth

; Initialize a variable to store permission status
conditionsMet := AreConditionsMet()

; Set a timer to check permission every 50ms
SetTimer, CheckPermission, 50
Return

CheckPermission:
    ; Check for permission and update the variable
    conditionsMet := AreConditionsMet()
Return

AreConditionsMet() {
    ; Check for specific pixel patterns on the screen
    return !ImageSearchSuccess(chatPixelPattern) && !ImageSearchSuccess(consolePixelPattern, "EntireScreen") && ImageSearchSuccess(agilityPixelPattern)
}

ImageSearchSuccess(pattern, searchRegion := "BottomHalf") {
    if (searchRegion = "BottomHalf") {
        ImageSearch, foundX, foundY, 0, %bottomHalfHeight%, A_ScreenWidth, A_ScreenHeight, %pattern%
    } else if (searchRegion = "EntireScreen") {
        ImageSearch, foundX, foundY, 0, 0, A_ScreenWidth, A_ScreenHeight, %pattern%
    }
    return !ErrorLevel
}

#If WinActive("ahk_exe game.exe") && (conditionsMet)
*~$q::
    Send, q{LButton}q{LButton}q{LButton}
Return
Rohwedder
Posts: 7750
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: Why is my AHK Script Lagging?  Topic is solved

14 Feb 2024, 02:06

Hallo,
use one script for the ImageSearch timer:

Code: Select all

#Persistent
global agilityPixelPattern := "D:\agility_pixel_pattern.png"
global chatPixelPattern := "D:\chat_pixel_pattern.png"
global consolePixelPattern := "D:\console_pixel_pattern.png"

; Calculate the coordinates for the bottom half, top half, left half, and right half of the screen
bottomHalfHeight := A_ScreenHeight / 2
topHalfHeight := bottomHalfHeight
leftHalfWidth := A_ScreenWidth / 2
rightHalfWidth := leftHalfWidth

; Initialize and set a timer to check permission every 50ms
SetTimer, CheckPermission, 50
CheckPermission:
    ; Check for permission and update vkFF state
IF AreConditionsMetOld <> AreConditionsMet := AreConditionsMet()
	SendInput,% AreConditionsMet?"{Blind}{vkFF Down}":"{Blind}{vkFF Up}"
AreConditionsMetOld := AreConditionsMet
Return

AreConditionsMet() {
    ; Check for specific pixel patterns on the screen
    return !ImageSearchSuccess(chatPixelPattern) && !ImageSearchSuccess(consolePixelPattern, "EntireScreen") && ImageSearchSuccess(agilityPixelPattern)
}

ImageSearchSuccess(pattern, searchRegion := "BottomHalf") {
    if (searchRegion = "BottomHalf") {
        ImageSearch, foundX, foundY, 0, %bottomHalfHeight%, A_ScreenWidth, A_ScreenHeight, %pattern%
    } else if (searchRegion = "EntireScreen") {
        ImageSearch, foundX, foundY, 0, 0, A_ScreenWidth, A_ScreenHeight, %pattern%
    }
    return !ErrorLevel
}
and a second for your Hotkeys:

Code: Select all

#If WinActive("ahk_exe game.exe") && GetKeyState("vkFF")
*~$q::Send, q{LButton}q{LButton}q{LButton}
Maimed
Posts: 7
Joined: 02 Dec 2022, 04:45

Re: Why is my AHK Script Lagging?

15 Feb 2024, 03:17

Thanks Rohwedder! your idea worked well. after trying it out there was still a bit of lag until i moved the 'GetKeyState("vkFF")' part from the '#If WinActive("ahk_exe game.exe")' line down to the hotkey itself. now everything runs smoothly.

Code: Select all

global agilityPixelPattern := "D:\agility_pixel_pattern.png"
global chatPixelPattern := "D:\chat_pixel_pattern.png"
global consolePixelPattern := "D:\console_pixel_pattern.png"
global vkFF_KEY_SENT := false

; Calculate the coordinates for the bottom half, top half, left half, and right half of the screen
bottomHalfHeight := A_ScreenHeight / 2
topHalfHeight := bottomHalfHeight
leftHalfWidth := A_ScreenWidth / 2
rightHalfWidth := leftHalfWidth

Function to perform image search and return true if found
ImageSearchSuccess(pattern, searchRegion := "BottomHalf") {
    if (searchRegion = "BottomHalf") {
        ImageSearch, foundX, foundY, 0, %bottomHalfHeight%, A_ScreenWidth, A_ScreenHeight, %pattern%
    } else if (searchRegion = "EntireScreen" or "") {
        ImageSearch, foundX, foundY, 0, 0, A_ScreenWidth, A_ScreenHeight, %pattern%
    }
    return !ErrorLevel
}

; Function to check game conditions
gameConditionsStatus() {
    ; Check for specific pixel patterns on the screen
    return !ImageSearchSuccess(chatPixelPattern, "BottomHalf") && !ImageSearchSuccess(consolePixelPattern, "EntireScreen") && ImageSearchSuccess(agilityPixelPattern, "BottomHalf")
}

; Set a timer to check conditions every 400ms
SetTimer, CheckConditions, 400
Return

CheckConditions:
    ; Check if game is active
    If WinActive("ahk_exe game.exe") {
        ; Check for conditions and update the variable
        gameConditionsMet := gameConditionsStatus()
        if (gameConditionsMet && !vkFF_KEY_SENT) {
            SendInput, {Blind}{vkFF Down}
            ;msgbox, vkFF Down
            vkFF_KEY_SENT := true
        } else if (!gameConditionsMet && vkFF_KEY_SENT) {
            SendInput, {Blind}{vkFF Up}
            ;msgbox, vkFF Up
            vkFF_KEY_SENT := false
        }
    }
Return

#IfWinActive, ahk_exe game.exe
*~$q::
    if (GetKeyState("vkFF"))
    {
        Send, q{LButton}q{LButton}q{LButton}
    }
Return
Btw if you don't mind me asking, what does 'vkFF' represent, and why did you use the '{Blind}' before it? just curious to understand it better and maybe use it in future scripts.
Rohwedder
Posts: 7750
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: Why is my AHK Script Lagging?

15 Feb 2024, 05:04

vkFF is a virtual key that has no function (see: https://www.autohotkey.com/docs/v1/lib/_MenuMaskKey.htm#Remarks)
and {Blind} prevents modifier keys from being affected when the script presses or releases vkFF.
Maimed
Posts: 7
Joined: 02 Dec 2022, 04:45

Re: Why is my AHK Script Lagging?

15 Feb 2024, 06:42

Ok, now I'm wondering, do you know for sure if sending 'vkFF' affects modifier keys, or is it just there as a precaution? Do I always have to use {Blind} with the key 'vkFF' and similar ones?
Rohwedder
Posts: 7750
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: Why is my AHK Script Lagging?

15 Feb 2024, 07:31

See: https://ahkde.github.io/docs/v1/lib/Send.htm#Blind
If only the key status of vkFF is to be changed, why should I ask the script to release downward pressed modifier keys first and then press them again?

Return to “Gaming Help (v1)”

Who is online

Users browsing this forum: No registered users and 35 guests