Looping PixelSearch with color array

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
SeverusKalkkaros
Posts: 3
Joined: 22 May 2018, 13:47

Looping PixelSearch with color array

22 May 2018, 14:04

Hello. I have been struggling with this problem for some days now and cant seem to figure this out. I've tried searching the forums and google for couple of days, but I cant seem to find an example that would help me with my case. So here goes. Basically Im trying to create a script that includes a different functions. It would look for set of colors (3 for an example) in order: Color1, if not found -> Color2, if not found -> Color3. When color3 is not found, the script would advance to another function.

In the first part, I would like to define the variables such as what colors will be used. Then would be the main script as "run function1, then run function2. Run function1 again, then func2. When function1 doesnt find color, run Function3. As explained here:

Heres an example of what the script would be doing:
Toggle for looping (looping on, looping off). When toggled "on" it would loop as long as its turned off.

Function1 = Find harvestable resource (based on colors, with pixelsearch). When found, advance to function2. When not found, advance to function3.
Function2 = Harvest available resource (for Function2, I need to obtain the X and Y coordinates, for whatever color it was that found, in the priority order I explained earlier)
Function3 = Move to another spot, then begin Function1.

Perhaps even, when Function1 has advanced to Function3 skipping Function2 X times, it would sleep for a while (like 5 minutes) then begin again.

Here's what I've tried to do.. I cant seem to figure out the syntax of AHK.. If one could help me figuring out how to do the PixelSearch part, I think I can manage the rest myself. :? :)

Code: Select all

Colors	:= [0xFBAF00, 0x150088, 0x4CB122]
Found	:= 0

^F5::
{
	Loop, % Colors.MaxIndex() {
		WinActivate, Untitled - Paint
		PixelSearch,  PosX, PosY, 230, 240, 550, 540, % Colors[A_Index], 3, fast
		if (!ErrorLevel)
			Found = 1	
	}
	if Found {
		MsgBox, One or more colors within 3 shades of variation was found at X%PosX% Y%PosY%.
	}
	else
		MsgBox, Colors not found in the specified region.	
}
return
User avatar
TLM
Posts: 1608
Joined: 01 Oct 2013, 07:52
Contact:

Re: Looping PixelSearch with color array

22 May 2018, 14:51

A couple of things here:
  • - The color values will be used as strings: Colors := [ "0xFBAF00", "0x150088", "0x4CB122" ]
    - AHk can parse arrays using a for loop:

Code: Select all

Colors := [ "0xFBAF00", "0x150088", "0x4CB122" ]

For Each, Color in Colors
{
    msgbox % Color
}
  • - You don't need a secondary condition if there's a match:

Code: Select all

Colors := [ "0xFBAF00", "0x150088", "0x4CB122" ]

For Each, Color in Colors
{
		WinActivate, Untitled - Paint
		WinWaitActive, Untitled - Paint ; << always a good idea to wait for the window to be active before continuing
		PixelSearch,  PosX, PosY, 230, 240, 550, 540, % Color, 3, fast
		if (!ErrorLevel)
		{
				Found = 1 ; you can still assign `found` if you plan on using it elsewhere
				MsgBox, One or more colors within 3 shades of variation was found at X%PosX% Y%PosY%.
		}
		else
		{
				MsgBox, Colors not found in the specified region.
		}
}
hth
SeverusKalkkaros
Posts: 3
Joined: 22 May 2018, 13:47

Re: Looping PixelSearch with color array

22 May 2018, 15:04

[quote="TLM"]A couple of things here:
  • - The color values will be used as strings: Colors := [ "0xFBAF00", "0x150088", "0x4CB122" ]
    - AHk can parse arrays using a for loop:

Code: Select all

Colors := [ "0xFBAF00", "0x150088", "0x4CB122" ]

hth[/quote]

Ah, I see. I think you nailed it, although I do have to ask. Now that I run it in my "untitled - paint" it informs me about finding both of the colors. Like MsgBox "color found here", after clicking ok another "Color found here". Would that be a problem, when, in case color is found the script would move on to another function (to click where the color was found, then do an additional click with a little offset). I mean, that it wouldnt be clicking at both of those locations, but only one? Anyways, awesome, quick help!
User avatar
TLM
Posts: 1608
Joined: 01 Oct 2013, 07:52
Contact:

Re: Looping PixelSearch with color array

22 May 2018, 15:30

It really depends on how you structure the script.

I pretty much never use Pixel commands in AutoHotkey but iirc they can run into finicky issues if Aero is turned on due to some odd color variations.
SeverusKalkkaros
Posts: 3
Joined: 22 May 2018, 13:47

Re: Looping PixelSearch with color array

22 May 2018, 16:19

I see, I think I'll be able to figure that out with sleeps, or something, but now for some reason I couldnt get it work with MouseClick, left, X%PosX%, Y%PosY%. If I were only to replace MsgBox with MouseClick.

Code: Select all

Colors := [ "0xFBAF00", "0x150088", "0x4CB122" ]

For Each, Color in Colors
{
		WinActivate, Untitled - Paint
		WinWaitActive, Untitled - Paint ; << always a good idea to wait for the window to be active before continuing
		PixelSearch,  PosX, PosY, 230, 240, 550, 540, % Color, 3, fast
		if (!ErrorLevel)
		{
				Found = 1 ; you can still assign `found` if you plan on using it elsewhere
				;MsgBox, One or more colors within 3 shades of variation was found at X%PosX% Y%PosY%.
				MouseClick, left, X%PosX%, Y%PosY% ; <-- Only added this here, but it does not seem to work. 
		}
		else
		{
				MsgBox, Colors not found in the specified region.
		}
}
User avatar
TLM
Posts: 1608
Joined: 01 Oct 2013, 07:52
Contact:

Re: Looping PixelSearch with color array

22 May 2018, 17:24

Again I almost never use Pixel commands but you should probably add Coordmode to change how the search and Click behave
Here's an example:

Code: Select all

CoordMode, Pixel, Window   ; For window search
CoordMode, Mouse, Window

For Each, Color in Colors
{
		WinActivate, Untitled - Paint
		WinWaitActive, Untitled - Paint ; << always a good idea to wait for the window to be active before continuing
		PixelSearch,  PosX, PosY, 230, 240, 550, 540, % Color, 3, fast
		if (!ErrorLevel)
		{
				Found = 1 ; you can still assign `found` if you plan on using it elsewhere
				;MsgBox, One or more colors within 3 shades of variation was found at X%PosX% Y%PosY%.
				MouseClick, left, X%PosX%, Y%PosY% ; <-- Only added this here, but it does not seem to work. 
		}
		else
		{
				MsgBox, Colors not found in the specified region.
		}
}
Return
You may have to try different settings. Check the documentation on Coordmode for more info on how it works.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Araphen, doanmvu and 348 guests