Loop: Sequential Pixelsearch's not working

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
rubeusmalfoy
Posts: 25
Joined: 20 Sep 2023, 06:40

Loop: Sequential Pixelsearch's not working

Post by rubeusmalfoy » 10 Apr 2024, 06:25

I have a loop with a number of sequential pixel searches but the loop does not proceed past the first pixelsearch and its actions. Once it has found the colour 000000, it repeatedly clicks at Bx By instead of going onto the next search. Here is my script:

Code: Select all

ALoop:	
    ; click
    pixelsearch, Bx, By, 200, 150, 1230, 720, 000000, 1, Fast RGB
    if (ErrorLevel = 0)
    {
    Bx := Bx + rand_gaussian_NN(2,2) 
    By := By + rand_gaussian_NN(2,2) 
    Click, %Bx% %By% ;Click 
    Random, randsleep, 1000, 2500
    Sleep randsleep ; Wait
    return
    }
    else
        {
            ; Exit the loop if color 1 is not found
            MsgBox, Color 1 not found! Exiting loop.
            ExitApp
        }
    ;  all
    pixelsearch, Dx, Py, 200, 150, 1230, 720, B97A57, 1, Fast RGB
    if (ErrorLevel = 0)
    {
    Dx := Dx + rand_gaussian_NN(2,2) 
    Dy := Dy + rand_gaussian_NN(2,2) 
    Click, %Dx% %Dy% ;  all
    Random, randsleep, 1000, 2500
    Sleep randsleep ; Wait
    return
    }
    ; 
    pixelsearch, Wx, Wy, 200, 150, 1230, 720, ED1C24, 1, Fast RGB
    if (ErrorLevel = 0)
    {
    Wx := Wx + rand_gaussian_NN(2,2) 
    Wy := Wy + rand_gaussian_NN(2,2) 
    Click, %Wx% %Wy% ;  all
    Random, randsleep, 1000, 2500
    Sleep randsleep ; Wait
    ; close 
    send, esc
    Random, randsleep, 1000, 2500
    Sleep randsleep ; Wait
    return
    }
    ; click 
    pixelsearch, Rx, Ry, 200, 150, 1230, 720, FFAEC9, 1, Fast RGB
    if (ErrorLevel = 0)
    {
    Rx := Rx + rand_gaussian_NN(2,2) 
    Ry := Ry + rand_gaussian_NN(2,2) 
    Click, %Rx% %Ry% ; click 
    Random, randsleep, 1000, 2500
    Sleep randsleep ; Wait
    send, space ; TEST 
    Random, randsleep, 1000, 1500
    Sleep randsleep ; Wait  
    return
    }
    CallALoop() ; Set a new random interval for the next iteration
    UpdateToolTip() ; Update the tooltip
Return

User avatar
mikeyww
Posts: 27138
Joined: 09 Sep 2014, 18:38

Re: Loop: Sequential Pixelsearch's not working

Post by mikeyww » 10 Apr 2024, 06:46

Hello,

The Return command is not a "filler" but has a specific effect on a script. I would follow your script, line by line, to see what it does. Check the values of your variables. I see no loops, but the Return command does not continue a loop. Instead, it returns to the subroutine's caller. To continue to the next iteration, you can instead use Continue.

ExitApp does not exit from a loop. Instead, it exits from the application (script). To break from a loop, you can use Break. If you would like a loop, you can use Loop.

rubeusmalfoy
Posts: 25
Joined: 20 Sep 2023, 06:40

Re: Loop: Sequential Pixelsearch's not working

Post by rubeusmalfoy » 10 Apr 2024, 07:13

So i define this "ALoop" to then be called here, and repeated until stopped:

Code: Select all

ActionLoop() {
    Random, RandomInterval, 25000, 35000 ; Adjust the range (in milliseconds) as needed
    SetTimer, ALoop, % RandomInterval
}

; Press F1 to start/stop random clicking
F1::
    IsClicking := !IsClicking
    if (IsClicking) {
        ActionLoop()
    } else {
        SetTimer, ALoop, Off
    }
    UpdateToolTip()
return
Regarding the ExitApp I intended for the entire script to exit if the 1st colour is not found.

I will try with the Continue command and see what happens.

User avatar
mikeyww
Posts: 27138
Joined: 09 Sep 2014, 18:38

Re: Loop: Sequential Pixelsearch's not working

Post by mikeyww » 10 Apr 2024, 07:17

If the color stays the same and your coordinates stay the same, then the color will continue to be found each time, right? Change the coordinates if you wish to use different ones.

rubeusmalfoy
Posts: 25
Joined: 20 Sep 2023, 06:40

Re: Loop: Sequential Pixelsearch's not working

Post by rubeusmalfoy » 10 Apr 2024, 07:23

I guess I thought it would move sequentially once the condition was found, bad assumption it seems. By change of coordinates do you mean the area I'm searching over? All 4 colours will be present somewhat randomly within the defined rectangular area.

User avatar
mikeyww
Posts: 27138
Joined: 09 Sep 2014, 18:38

Re: Loop: Sequential Pixelsearch's not working

Post by mikeyww » 10 Apr 2024, 07:28

You can learn about this through the documentation. Nothing "moves". You are in control of the coordinates that you specify in the command.
The region to be searched must be visible; in other words, it is not possible to search a region of a window hidden behind another window. By contrast, pixels beneath the mouse cursor can usually be detected. The exception to this is game cursors, which in most cases will obstruct any pixels beneath them. The search order depends on the order of the parameters. In other words, if X1 is greater than X2, the search will be conducted from right to left, starting at column X1. Similarly, if Y1 is greater than Y2, the search will be conducted from bottom to top. However, prior to [v1.1.32], the fast mode required X1 and Y1 to be the top-left corner.

Fast mode: The search starts at the coordinates specified by X1 and Y1 and checks all pixels in the row from X1 to X2 for a match. If no match is found there, the search continues toward Y2, row by row, :arrow: until it finds a matching pixel.

Source: PixelSearch - Syntax & Usage | AutoHotkey v1

rubeusmalfoy
Posts: 25
Joined: 20 Sep 2023, 06:40

Re: Loop: Sequential Pixelsearch's not working

Post by rubeusmalfoy » 10 Apr 2024, 07:43

Sorry I'm not quite following what you mean. Each pixel search has a different colour but the same start and end coordinates. Are you meaning change each pixel searches starting coordinates to a more specific area?

User avatar
mikeyww
Posts: 27138
Joined: 09 Sep 2014, 18:38

Re: Loop: Sequential Pixelsearch's not working

Post by mikeyww » 10 Apr 2024, 07:46

The fast search mode will search the defined area, row by row, until the first match is found. If that match is always in the same specific location, then you will get the same results each time, as long as the color is found. To find multiple matches, you can search a different area by changing the coordinates.

The key is understanding that the command executes a single search for a single pixel. Only the first match is considered.

Boiler has posted a script to find all images within a region. A pixel is an example of an image that could be searched. You can create an image of the specific pixel that you wish to find.

You can debug your script and search by shortening the script to essential parts, and just testing those. Display or inspect the values of your variables as well as the ErrorLevel from your search. Instead of clicking, using If, and so on, you can display the ErrorLevel and the values of the coordinates that the command returns. That will tell you enough to debug your search.

rubeusmalfoy
Posts: 25
Joined: 20 Sep 2023, 06:40

Re: Loop: Sequential Pixelsearch's not working

Post by rubeusmalfoy » 10 Apr 2024, 08:10

Ok will test and report back if needed, thnak you

rubeusmalfoy
Posts: 25
Joined: 20 Sep 2023, 06:40

Re: Loop: Sequential Pixelsearch's not working

Post by rubeusmalfoy » 26 Apr 2024, 12:44

@mikeyww I've tried a few different things and not had much success. I've gone back to simpler code but am stuck here:

Code: Select all

CallALoop() {
    Random, RandomInterval, 3500, 4000 ; Adjust the range (in milliseconds) as needed
    SetTimer, ALoop, % RandomInterval
}

; Press F1 to start/stop
F1::
    IsClicking := !IsClicking
    if (IsClicking) {
        CallALoop()
    } else {
        SetTimer, ALoop, Off
    }
    UpdateToolTip()
return

ALoop:	
    ; click 
    pixelsearch, Bx, By, 550, 200, 850, 450, 000000, 1, Fast RGB
    if (ErrorLevel = 0)
    {
    Bx := Bx + rand_gaussian_NN(2,2) 
    By := By + rand_gaussian_NN(2,2) 
    Click, %Bx% %By% ;Click 
    continue
    }
    else
        {
            ; Exit the loop if color 1 is not found
            MsgBox, Color 1 not found! Exiting loop.
            ExitApp
        }
    ;
    pixelsearch, Dx, Py, 880, 570, 960, 650, B97A57, 1, Fast RGB
    if (ErrorLevel = 0)
    {
    Dx := Dx + rand_gaussian_NN(2,2) 
    Dy := Dy + rand_gaussian_NN(2,2) 
    Click, %Dx% %Dy% ; 
    continue
    }
    ; 
    pixelsearch, Wx, Wy, 1200, 230, 1270, 290, ED1C24, 1, Fast RGB
    if (ErrorLevel = 0)
    {
    Wx := Wx + rand_gaussian_NN(2,2) 
    Wy := Wy + rand_gaussian_NN(2,2) 
    Click, %Wx% %Wy% ; 
    continue
    }
    ; click
    pixelsearch, Rx, Ry, 1200, 530, 1500, 720, FFAEC9, 1, Fast RGB
    if (ErrorLevel = 0)
    {
    Rx := Rx + rand_gaussian_NN(2,2) 
    Ry := Ry + rand_gaussian_NN(2,2) 
    Click, %Rx% %Ry% ; click 
    }
    CallALoop() ; Set a new random interval for the next iteration
    UpdateToolTip() ; Update the tooltip with the new click count
Return

User avatar
mikeyww
Posts: 27138
Joined: 09 Sep 2014, 18:38

Re: Loop: Sequential Pixelsearch's not working

Post by mikeyww » 26 Apr 2024, 14:04

I have provided all of the recommendations that I have. If they are not useful for you, then others may have some better ones! Best of luck.

Code: Select all

#Requires AutoHotkey v1.1.33.11
x1  := 0
y1  := 0
x2  := A_ScreenWidth
y2  := A_ScreenHeight
rgb := 0xFFFFFF
CoordMode Pixel
PixelSearch x, y, x1, y1, x2, y2, rgb,, Fast RGB
MsgBox 64, Result, % "ErrorLevel = " ErrorLevel "`n`nx = " x "`ny = " y
B97A57 is a string but not a number (as interpreted). The documentation for PixelSearch has a description of the color parameter, along with an additional example.

Code: Select all

#Requires AutoHotkey v1.1.33.11
x1  := 0
y1  := 0
x2  := A_ScreenWidth
y2  := A_ScreenHeight
B97A57 := 0xFFFFFF
CoordMode Pixel
PixelSearch x, y, x1, y1, x2, y2, B97A57,, Fast RGB
MsgBox 64, Result, % "ErrorLevel = " ErrorLevel "`n`nx = " x "`ny = " y
These short examples are useful in quickly demonstrating a bug in your script.

Post Reply

Return to “Ask for Help (v1)”