PixelSearch and Until Loop Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Thaking
Posts: 2
Joined: 04 Jul 2022, 18:31

PixelSearch and Until Loop

Post by Thaking » 04 Jul 2022, 18:36

I am trying to make an Until Loop that loops a PixelSearch until the pixel comes back positive but i cant seem to get it working, my code is

Code: Select all

Loop
    PixelSearch, Px, Py, 835, 885, 835, 855, 0x00F26F, Fast
    Sleep 500
Until, ErrorLevel = 0

gregster
Posts: 8990
Joined: 30 Sep 2013, 06:48

Re: PixelSearch and Until Loop  Topic is solved

Post by gregster » 04 Jul 2022, 18:40

In AHK, you would group multiple statements together by using braces { } (aka blocks); indenting has no effect in AHK, it's just a visual helper.
You can only omit braces if only a single dependent line is following a statement like loop, if, while, ...
https://www.autohotkey.com/docs/commands/Until.htm wrote:

Code: Select all

Loop {
    ...
} Until Expression

Thaking
Posts: 2
Joined: 04 Jul 2022, 18:31

Re: PixelSearch and Until Loop

Post by Thaking » 04 Jul 2022, 18:46

gregster wrote:
04 Jul 2022, 18:40
In AHK, you would group multiple statements together by using braces { } (aka blocks); indenting has no effect in AHK, it's just a visual helper.
You can only omit braces if only a single dependent line is following a statement like loop, if, while, ...
https://www.autohotkey.com/docs/commands/Until.htm wrote:

Code: Select all

Loop {
    ...
} Until Expression
Ahh, okay that makes sense. Thanks!

gregster
Posts: 8990
Joined: 30 Sep 2013, 06:48

Re: PixelSearch and Until Loop

Post by gregster » 04 Jul 2022, 18:49

btw, I think you missed a parameter in your PixelSearch line. After the ColorID, Variation would come first, before Mode:
https://www.autohotkey.com/docs/commands/PixelSearch.htm wrote:

Code: Select all

PixelSearch, OutputVarX, OutputVarY, X1, Y1, X2, Y2, ColorID [, Variation, Mode]
This means, if you don't want to use variation, just leave that (optional) parameter blank:

Code: Select all

 PixelSearch, Px, Py, 835, 885, 835, 855, 0x00F26F, , Fast

SYSYA
Posts: 1
Joined: 06 Jul 2022, 12:32

Re: PixelSearch and Until Loop

Post by SYSYA » 06 Jul 2022, 13:52

I use a wait until a pixel is a color a lot. I found it one of the best ways to see if a window has updated. Try this code...

Code: Select all

WaitForColor(90, 110, 0xF0F0F0)      ; Go to function WaitForColor to wait for color to change.  (X, Y, color)
MsgBox,0,,The color was found.
Exitapp


;*************** Wait for a pixel to be a Color ********************************
WaitForColor(pX, pY, pC)                            ; Start of function WaitForColor. Gets X and Y coordinates and color from the sending line.
{                                                                ; Needed at the start of the function
 Loop                                                         ; Loop to wait for color to change
 {                                                               ; Needed at the start of the loop
  PixelGetColor, color, %pX%, %pY%, RGB      ; Get the color of the pixel at the specified location. Remove the RGB if not using RGB
  If color = %pC%                                         ; Compare the color of that pixel with the specified color. 
	break                                                ; If that color matches, break
  Sleep 50	                                                 ; Reduce CPU usage so its not looking every millisecond.
 }                                                               ; Needed at the end of the loop
}                                                               ; Needed at the end of the function

;To have it wait until the pixel is not the color you specified, change the line in the loop to  If color != %pC%

I also have one that does the same thing but instead of one pixel looks for a color in an area. This one also has a time out if the color never appears...

Code: Select all

If WaitForColorArea(175, 126, 195, 148, 0xFFE792, 5, 50, FoundX, FoundY) 
;                (x1, y1, x2, y2, Color, variation of color, timeout, FoundX, FoundY)
; Timeout is in 10th of a second.  for 1 second enter 10, for 10 seconds enter 100 
  MsgBoX,0,,Color found
Else
  MsgBox,0,,Color NOT found
Exitapp


;*************** Wait for a color in an area then return the coordinates *********************
WaitForColorArea(X1, Y1, X2, Y2, pC, V, T=10000, ByRef FoundX=0, ByRef FoundY=0)	  
; Start of function WaitForColorArea. Gets X and Y coordinates, color and timeout time from the sending line.  
;If no time specified in sending line, timeout is set to 10000 which is 1000 seconds (16min 40 seconds)
{                                                                                                                              ; Needed at the start of the function
  Loop, %T%                                                                                                              ; Loop the number of times specified in the timeout time.
  {                                                                                                                            ; Needed at the start of the loop
    PixelSearch, FoundX, FoundY, %X1%, %Y1%, %X2%, %Y2%, %PC%, %V%, Fast RGB        ; You can remove the RGB if not using RGB. 
    If (Errorlevel = 0)                                                                                                  ; Once the color is found...
	  Return True                                                                                                    ; ...Return to caller with True
		Sleep 100                                                                                                ; Reduce CPU usage so its not looking every millisecond.
  }                                                                                                                           ; Needed at the end of the loop
  Return False                                                                                                         ; If the color was not found after the specified time, return false
}                                                                                                                            ; Needed at the end of the function

; To make it search for any color other than the one you specify, put a ! in font of the color in the sending statement. ie !0xFFE792.
; Then if a color other than the one you specify is found, it will return True and do the lines under the If command.
; And if only the color you specify is found, it will time out and return False and do the lines under the Else command.
; To reverse it so the code after the sending statement is if the color is not found, and the code after the Else is if the color is found, change the sending statement to 'If Not WaitForColorArea...'

[Mod edit: Replaced unformatted codebox tags (here they used 'text' formatting aka none) with [code][/code] tags.]

Post Reply

Return to “Ask for Help (v1)”