Does PixelSeach update the pixel Location by itself or does it "lock" the pixel location the moment it is found?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
kauzds
Posts: 5
Joined: 17 Jan 2018, 16:52

Does PixelSeach update the pixel Location by itself or does it "lock" the pixel location the moment it is found?

23 Jan 2018, 20:50

Hi, I'm trying to Get 2 pixels and compare their location but I don't know if the result I'm getting is "real".

Here is a video of what I'm trying to automate: https://streamable.com/aidak(with each cycle the "Fixed" bar changes position)
Here is the script "working": https://streamable.com/5n8r9

here is my script

Code: Select all

q::
Start:
	MouseGetPos, xpos, ypos 					
	send, 1			
	sleep, 100		
	MouseClick, left, %xpos%, %ypos%, 1 	; Get mouse pos and left click at that location
	sleep 500
	goto MovingBar
MovingBar:		
	PixelSearch, P1x, P1y, 0, 7, 330, 25, 0xB4CFC0, 20, RGB  ; Search for the moving bar	
	if ErrorLevel
		MsgBox, Moving bar not found
	Else
		goto FixedBar
FixedBar:
	sleep, 6000
	PixelSearch, P2x, P2y, 0, 7, 330, 25, 0x6A7CDE, 20, RGB  ; Search for the fixed bar	
	if ErrorLevel
		MsgBox, Fixed bar not found
	Else
	    goto P-location
P-location:
Loop,
{
	if (abs(P1x - P2x) <= 30)   ; If the distance between P1x and P2x is less or equal to 50 pixels go to final stage
		goto Final
	Else
		MsgBox, Not close enough
}	
Final:
	Send {Space down}   
	;Sleep 10  		  		; Press space and loop back
	Send {Space up}  	
	Sleep 1000
	goto Start
As you can see I'm comparing the distance between P1x and P2x but I found that more often than not the script still sends {Space} despite P1x and P2x not being close enough. I even tried using a Loop but I'm not sure it is working. How can I make PixelSearch keep track of P1x and P2x location? Or if it already does keep tracking the pixel location how can I make so the comparison of P1x and P2x's distance is done multiple times until they are in the set range or a "time limit" is reached?

Any tips appreciated, I am a beginner after all :)
User avatar
boiler
Posts: 16965
Joined: 21 Dec 2014, 02:44

Re: Does PixelSeach update the pixel Location by itself or does it "lock" the pixel location the moment it is found?

23 Jan 2018, 21:18

First, don't use goto. It's creates horrible code. Plus, you're often going to the next line which it would have gone to anyway, so it accomplishes nothing.

Here is the equivalent of your code without using goto statements. However, note the comment on the inner loop. That's where you need more searches to update the values of the variables.

Code: Select all

q::
loop
{
	MouseGetPos, xpos, ypos 					
	send, 1			
	sleep, 100		
	MouseClick, left, %xpos%, %ypos%, 1 	; Get mouse pos and left click at that location
	sleep 500
	PixelSearch, P1x, P1y, 0, 7, 330, 25, 0xB4CFC0, 20, RGB  ; Search for the moving bar	
	if ErrorLevel
		MsgBox, Moving bar not found
	sleep, 6000
	PixelSearch, P2x, P2y, 0, 7, 330, 25, 0x6A7CDE, 20, RGB  ; Search for the fixed bar	
	if ErrorLevel
		MsgBox, Fixed bar not found
	Loop ; will never get out of this loop once you've failed the if condition because you never update the values of P1x and P2x.  need to redo pixel searches inside the loop
	{
		if (abs(P1x - P2x) <= 30)   ; If the distance between P1x and P2x is less or equal to 50 pixels go to final stage
			break
		Else
			MsgBox, Not close enough
	}	
	Send {Space down}   
	;Sleep 10  		  		; Press space and loop back
	Send {Space up}  	
	Sleep 1000
}
kauzds
Posts: 5
Joined: 17 Jan 2018, 16:52

Re: Does PixelSeach update the pixel Location by itself or does it "lock" the pixel location the moment it is found?

24 Jan 2018, 01:39

Can you show me some examples? I tried the script below with only the moving bar PixelSearch and had no sucess, same with both PixelSearch in the loop :/

Code: Select all

q::
	  
Loop,
{
	PixelSearch, P2x, P2y, 0, 7, 330, 25, 0x6A7CDE, 20, RGB  ; Search for the fixed bar	
		if Errorlevel
			MsgBox, Error1
		else
			continue
	PixelSearch, P1x, P1y, 0, 7, 330, 25, 0xB4CFC0, 20, RGB  ; Search for the moving bar	
		if (abs(P1x - P2x) <= 50)   ; If the distance between P1x and P2x is less or equal to 50 pixels go to final stage
			Send, Space  
		Else
			MsgBox, Error2
}
User avatar
Exaskryz
Posts: 2882
Joined: 17 Oct 2015, 20:28

Re: Does PixelSeach update the pixel Location by itself or does it "lock" the pixel location the moment it is found?

24 Jan 2018, 01:51

My understanding of PixelSearch is it store one static value into the x/y variables even if that pixel is moving on screen. I don't know if PixelSearch "freezes" the screen into memory and then searches that, or if it will ask "in real time" "What is at pixel 1,1? Pixel 1,2? Pixel 1,3? Pixel 1,4? ... Pixel 1920,1080?"

You do have at least a 600 ms delay in your initial code before any pixels are sampled, which may be why you are getting undesirable results and thinking that the pixels aren't close enough, but when AHK samples, they may be close enough.

In your latest code in the reply above, you are using the Continue command for reasons I'm not understanding. Continue will jump back to the top of the loop -- repeating the "P2x, P2y" PixelSearch command, if there was no Error in the previous one. The only way to ever search "P1x, P1y" (the second PixelSearch) is by getting MsgBox, Error1 to trigger, which skips the else/continue lines.

Also note that if P2x happened to be ≤50, and then the second Pixelsearch reports an ErrorLevel, then P1x could be 0 or unchanged from last time giving a false result. You should consider an if statement: if ((abs(P1x - P2x) <= 50) && !ErrorLevel) where the ! is the logical-not operator and && is the AND operator, to make sure that that Pixelsearch did not result in an error.

I do admit I did not watch your recordings, so I likely have not given a complete answer.

Edit: I watched the videos. I'm not a fan of the PixelSearch logic of searching the same areas. I propose you search just one row of pixels (so same value for the y parameters in PixelSearch) for each of the moving and fixed (draining) bars. What I would do is search for the first dark pixel (background) of the fixed bar first, to indicate where the edge is. Then search for the moving bar's brighter color. The pixelsearch should move left to right, if you put the smaller X coordinate in the first pair of coordinates, so that you find the edge of the draining bar.

I don't know how fast PixelSearch runs, but I would actually consider making your search for the moving bar dynamic by using the result for the fixed bar. If you find the fixed bar at pixel X=200, then make it so your search for the moving bar only fits into X1=150 to X2=250. This can be dynamically done by using something like

Code: Select all

Loop
{
PixelSearch, P1x, P1y, *Fixed Bar Coordinates*, *Color*, *Variation*
If ErrorLevel
    continue ; something went wrong, search again
PixelSearch, P2x, P2y, P1x-50, yCoord, P1x+50, yCoord, *Color*, *Variation*
If ErrorLevel
   continue ; start from the top, we didn't find the moving bar in range
else
   MsgBox The moving bar was close to the fixed bar!
}
We could actually reduce the code just a bit by reversing the logic at the end:

Code: Select all

Loop
{
PixelSearch, P1x, P1y, *Fixed Bar Coordinates*, *Color*, *Variation*
If ErrorLevel
    continue ; something went wrong, search again
PixelSearch, P2x, P2y, P1x-50, yCoord, P1x+50, yCoord, *Color*, *Variation*
If !ErrorLevel
   MsgBox The moving bar was close to the fixed bar!
}
The loop would have "Continued" if there was an ErrorLevel due to not finding the draining bar.

Code is untested. Diagnostics should include using MsgBox %ErrorLevel% to know what error happened according to PixelSearch.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: ccqcl, DataLife, Google [Bot], Rohwedder and 164 guests