PMC, counting down timer to break the loop

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
erotoman
Posts: 1
Joined: 17 May 2024, 13:29

PMC, counting down timer to break the loop

17 May 2024, 13:51

Hi, i spend like 1 hour figuring out how to do this and im done, i've done some research and i cant do it by my own, when code stops at certain moment because it cant execute if statement(don't find certain photo to click) i want it to start counting down from 5s to 0 and then break the loop and start going to next lines. I dont really know AHK language, im using Pullover's Macro Creator its easier for me. (To be exact i want it to stop at 22 line if cant find image, then wait 5 seconds, break loop and continue)


https://imgur.com/a/P2mIYeG

Code: Select all

IfWinActive, Weapon Trading - TF2 - Scrap.TF - Google Chrome
{
    Loop
    {
        CoordMode, Pixel, Window
        ImageSearch, RXX, RYY, 788, 242, 948, 318, C:\Users\fafik\AppData\Roaming\MacroCreator\Screenshots\Screen_20240517172618.png
        If (ErrorLevel = 0)
        {
            Click, 1401, 286 Left, 1
            Sleep, 100
        }
        CoordMode, Pixel, Window
        ImageSearch, 24X, 24Y, 1268, 233, 1407, 282, C:\Users\fafik\AppData\Roaming\MacroCreator\Screenshots\Screen_20240517155343.png
        If (ErrorLevel = 0)
        {
            Sleep, 300
            Click, 1497, 263 Left, 1
            Sleep, 100
            Sleep, 300
            Click, 595, 123 Left, 1
            Sleep, 100
            Sleep, 300
            Click, 429, 220 Left, 1
            Sleep, 100
            Break
        }
        If (ErrorLevel)
        {
            CoordMode, Pixel, Window
            ImageSearch, SCRX, SCRY, 414, 682, 1544, 1047, C:\Users\fafik\AppData\Roaming\MacroCreator\Screenshots\Screen_20240517130055.png
            CenterImgSrchCoords("C:\Users\fafik\AppData\Roaming\MacroCreator\Screenshots\Screen_20240517130055.png", SCRX, SCRY)
            If (ErrorLevel = 0)
            {
                Click, %SCRX%, %SCRY% Left, 1
                Sleep, 5
                Click, 1371, 577 Left, 1
                Sleep, 5
                Goto, Macro1
            }
        }
    }
    Loop
    {
        Loop
        {
            CoordMode, Pixel, Window
            ImageSearch, AX, AY, 769, 149, 1088, 197, *20 C:\Users\fafik\AppData\Roaming\MacroCreator\Screenshots\Screen_20240517155748.png
            CenterImgSrchCoords("*20 C:\Users\fafik\AppData\Roaming\MacroCreator\Screenshots\Screen_20240517155748.png", AX, AY)
        }
        Until ErrorLevel = 0
        If (ErrorLevel = 0)
        {
            Click, 962, 218 Left, 1
            Sleep, 100
            Break
        }
    }
}
Loop
{
    CoordMode, Pixel, Window
    ImageSearch, RX, RY, 520, 105, 711, 451, C:\Users\fafik\AppData\Roaming\MacroCreator\Screenshots\Screen_20240517133305.png
    CenterImgSrchCoords("C:\Users\fafik\AppData\Roaming\MacroCreator\Screenshots\Screen_20240517133305.png", RX, RY)
    If (ErrorLevel = 0)
    {
        Sleep, 1000
        Click, %RX%, %RY% Left, 1
        Sleep, 100
        Sleep, 100
        Click, 901, 569 Left, 1
        Sleep, 100
        Loop
        {
            CoordMode, Pixel, Window
            ImageSearch, ReX, ReY, 151, 414, 815, 1018, C:\Users\fafik\AppData\Roaming\MacroCreator\Screenshots\Screen_20240517134026.png
            CenterImgSrchCoords("C:\Users\fafik\AppData\Roaming\MacroCreator\Screenshots\Screen_20240517134026.png", ReX, ReY)
        }
        Until ErrorLevel = 0
        If (ErrorLevel = 0)
        {
            Sleep, 1000
            Click, %ReX%, %ReY% Left, 1
            Sleep, 100
            Sleep, 500
            Click, 1011, 14 Left, 1
            Sleep, 100
            Sleep, 200
            Break
        }
    }
}
Send, {LAlt Down}
Sleep, 200
Send, {Tab}
Sleep, 200
Send, {LAlt Up}
Goto, Macro2
[Mod edit: Moved topic from AHK v2 help since this is v1 code.]
User avatar
CoffeeChaton
Posts: 44
Joined: 11 May 2024, 10:50

Re: PMC, counting down timer to break the loop

17 May 2024, 14:24

Code: Select all

#Requires AutoHotkey v1.1.33+

;#1 
; IfWinActive is Deprecated: Use `WinActive()` function instead.
if (WinActive("Weapon Trading - TF2 - Scrap.TF - Google Chrome")) {
    Loop
    {
        CoordMode, Pixel, Window
        ImageSearch, RXX, RYY, 788, 242, 948, 318, C:\Users\fafik\AppData\Roaming\MacroCreator\Screenshots\Screen_20240517172618.png
        If (ErrorLevel = 0)
        {
            Click, 1401, 286 Left, 1
            Sleep, 100
        }
        CoordMode, Pixel, Window ; #0 not need to change `CoordMode, Pixel, Window`
        ImageSearch, 24X, 24Y, 1268, 233, 1407, 282, C:\Users\fafik\AppData\Roaming\MacroCreator\Screenshots\Screen_20240517155343.png
        If (ErrorLevel = 0) ; by ImageSearch, ErrorLevel := "0, 1, 2"
        {
            Sleep, 300
            Click, 1497, 263 Left, 1
            Sleep, 100
            Sleep, 300
            Click, 595, 123 Left, 1
            Sleep, 100
            Sleep, 300
            Click, 429, 220 Left, 1
            Sleep, 100
            Break
        }
        ; #2
        ; ErrorLevel := "1, 2"
        ; you are use Break, not need to check ErrorLevel, allow is true
        ; If (ErrorLevel)
        CoordMode, Pixel, Window ; #0 not need to change `CoordMode, Pixel, Window`
        ImageSearch, SCRX, SCRY, 414, 682, 1544, 1047, C:\Users\fafik\AppData\Roaming\MacroCreator\Screenshots\Screen_20240517130055.png
        CenterImgSrchCoords("C:\Users\fafik\AppData\Roaming\MacroCreator\Screenshots\Screen_20240517130055.png", SCRX, SCRY)
        ; #3 where you CenterImgSrchCoords()?
        ; in CenterImgSrchCoords() ,may change ErrorLevel
        If (ErrorLevel = 0)
        {
            Click, %SCRX%, %SCRY% Left, 1
            Sleep, 5
            Click, 1371, 577 Left, 1
            Sleep, 5
            Goto, Macro1 ; #4 are you sure use `Goto` not `GotoSub` ?
        }
    }
    ; #4-2, never run to here

    Loop
    {
        Loop
        {
            CoordMode, Pixel, Window
            ImageSearch, AX, AY, 769, 149, 1088, 197, *20 C:\Users\fafik\AppData\Roaming\MacroCreator\Screenshots\Screen_20240517155748.png
            CenterImgSrchCoords("*20 C:\Users\fafik\AppData\Roaming\MacroCreator\Screenshots\Screen_20240517155748.png", AX, AY)
        } Until ErrorLevel = 0

        If (ErrorLevel = 0) ; ErrorLevel always = 0 ,beacuse `Until ErrorLevel = 0`
        {
            Click, 962, 218 Left, 1
            Sleep, 100
            Break ; #5 it just Break*1 , never run to #6
        }
    }
}
; #6 , never run to here
Loop
{
    CoordMode, Pixel, Window
    ImageSearch, RX, RY, 520, 105, 711, 451, C:\Users\fafik\AppData\Roaming\MacroCreator\Screenshots\Screen_20240517133305.png
    CenterImgSrchCoords("C:\Users\fafik\AppData\Roaming\MacroCreator\Screenshots\Screen_20240517133305.png", RX, RY)
    If (ErrorLevel = 0)
    {
        Sleep, 1000
        Click, %RX%, %RY% Left, 1
        Sleep, 100
        Sleep, 100
        Click, 901, 569 Left, 1
        Sleep, 100
        Loop
        {
            CoordMode, Pixel, Window
            ImageSearch, ReX, ReY, 151, 414, 815, 1018, C:\Users\fafik\AppData\Roaming\MacroCreator\Screenshots\Screen_20240517134026.png
            CenterImgSrchCoords("C:\Users\fafik\AppData\Roaming\MacroCreator\Screenshots\Screen_20240517134026.png", ReX, ReY)
        } Until ErrorLevel = 0

        If (ErrorLevel = 0) ; ErrorLevel always = 0 ,beacuse `Until ErrorLevel = 0`
        {
            Sleep, 1000
            Click, %ReX%, %ReY% Left, 1
            Sleep, 100
            Sleep, 500
            Click, 1011, 14 Left, 1
            Sleep, 100
            Sleep, 200
            Break
        }
    }
}

Send, {LAlt Down}
Sleep, 200
Send, {Tab}
Sleep, 200
Send, {LAlt Up}
Goto, Macro2
(To be exact i want it to stop at 22 line if cant find image, then wait 5 seconds, break loop and continue)
1. i do not where is (line 22), please use Comment, and I have marked the problems I saw in the code, like ;#1 #2 #3.
2. (if cant find image), which picture?
3. i do not what is (break loop and continue) ? if you main Continue ? https://www.autohotkey.com/docs/v1/lib/Continue.htm
rockitdontstopit
Posts: 107
Joined: 12 Nov 2022, 15:47

Re: PMC, counting down timer to break the loop

18 May 2024, 12:02

erotoman wrote:
17 May 2024, 13:51
Hi, i spend like 1 hour figuring out how to do this and im done, i've done some research and i cant do it by my own, when code stops at certain moment because it cant execute if statement(don't find certain photo to click) i want it to start counting down from 5s to 0 and then break the loop and start going to next lines. I dont really know AHK language, im using Pullover's Macro Creator its easier for me. (To be exact i want it to stop at 22 line if cant find image, then wait 5 seconds, break loop and continue)
This ahk v1 script may be useful to you. It searches for an image until it finds the image or after 5 seconds elapses. The tooltip is just for testing. I would run this ahk script from within PMC using a RunWait much like a function. It's very seamless and surprisingly fast. Basically as if the code was located as a tab or subroutine within PMC itself.

Code: Select all

SetTimer, CheckImage, 100  ; Set a timer to check for the image every 100 milliseconds
SetTimer, StopScript, -5000  ; Set a timer to stop the script after 5 seconds
stop := false  ; Initialize a variable to control the loop

Loop
{
    if (stop)  ; Check if the stop variable is true
        break  ; If it is, break the loop

    ToolTip, %A_Index%  ; Display the index of the current loop iteration
}
return

CheckImage:
ImageSearch, FoundX, FoundY, 0, 0, 1920, 1080, C:\LOCATION_OF_YOUR_IMAGE.png
if ErrorLevel = 0  ; If the image is found
{
    stop := true  ; Set the stop variable to true to stop the loop
    SetTimer, StopScript, Off  ; Cancel the StopScript timer
    ExitApp  ; Exit the script
}
return

StopScript:
ExitApp  ; Exit the script
return

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Bing [Bot] and 278 guests