Imagesearch and if statement Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
mero
Posts: 11
Joined: 20 Aug 2017, 14:54

Imagesearch and if statement

21 Apr 2021, 23:43

F4::
KeyWait, F4
CoordMode, Pixel, Screen
CoordMode, Mouse, Screen
ImageSearch, foundX, foundY, 0, 0, %A_ScreenWidth%, %A_ScreenHeight%, *100 image1.png
If(ErrorLevel == 0){
Click, %foundX%, %foundY%
Sleep, 300
Send, {Esc}
}
If(ErrorLevel == 1){
CoordMode, Pixel, Screen
CoordMode, Mouse, Screen
ImageSearch, foundX, foundY, 0, 0, %A_ScreenWidth%, %A_ScreenHeight%, *100 image2.png

If(ErrorLevel == 0){
Send, !vm
loop, 500
{
CoordMode, Pixel, Screen
CoordMode, Mouse, Screen
ImageSearch, foundX, foundY, 0, 0, %A_ScreenWidth%, %A_ScreenHeight%, *100 image3.png
If(ErrorLevel == 0){
Click, %foundX%, %foundY%
Break
}
}
}
}
return


What i'm trying to do:

If image 1 is found:
Click Image1
Send, ESC

If image 1 is not found:
>Search for image2
>If image 2 is found:
>>Send, !vm
>>Loop until image 3 is found
>>If found, click Image 3


My issue appears to be that the section for image2 isn't working. Can anyone help me figure out where i messed up. The image is in the same directory as the script. :)
User avatar
boiler
Posts: 16902
Joined: 21 Dec 2014, 02:44

Re: Imagesearch and if statement

22 Apr 2021, 00:34

If by not working, you mean it's not finding image2.png and not doing the stuff after it, it's looking like it's just not finding the image. Have you verified that you can find the image using that statement when it does appear (i.e., a simple script with just that ImageSearch statement and a reporting on the value of ErrorLevel)? It could be that the reference image needs to be cropped differently or even the 100 allowable variation isn't enough to find it, or something like that.

In case it helps others (or yourself), below is an easier to review version of the script. Not that you don't need to repeat the CoordMode statements, and you can use = instead of == since == is used for case sensitivity in AHK.

Code: Select all

F4::
KeyWait, F4
CoordMode, Pixel, Screen
CoordMode, Mouse, Screen
ImageSearch, foundX, foundY, 0, 0, %A_ScreenWidth%, %A_ScreenHeight%, *100 image1.png
If(ErrorLevel = 0){
	Click, %foundX%, %foundY%
	Sleep, 300
	Send, {Esc}
}
If(ErrorLevel = 1){
	ImageSearch, foundX, foundY, 0, 0, %A_ScreenWidth%, %A_ScreenHeight%, *100 image2.png
	If(ErrorLevel = 0){
		Send, !vm
		loop, 500 {
			ImageSearch, foundX, foundY, 0, 0, %A_ScreenWidth%, %A_ScreenHeight%, *100 image3.png
			If(ErrorLevel = 0){
				Click, %foundX%, %foundY%
				Break
			}
		}
	}
}
return
User avatar
mero
Posts: 11
Joined: 20 Aug 2017, 14:54

Re: Imagesearch and if statement

22 Apr 2021, 10:39

boiler wrote:
22 Apr 2021, 00:34
If by not working, you mean it's not finding image2.png and not doing the stuff after it, it's looking like it's just not finding the image. Have you verified that you can find the image using that statement when it does appear (i.e., a simple script with just that ImageSearch statement and a reporting on the value of ErrorLevel)? It could be that the reference image needs to be cropped differently or even the 100 allowable variation isn't enough to find it, or something like that.

In case it helps others (or yourself), below is an easier to review version of the script. Not that you don't need to repeat the CoordMode statements, and you can use = instead of == since == is used for case sensitivity in AHK.

Code: Select all

F4::
KeyWait, F4
CoordMode, Pixel, Screen
CoordMode, Mouse, Screen
ImageSearch, foundX, foundY, 0, 0, %A_ScreenWidth%, %A_ScreenHeight%, *100 image1.png
If(ErrorLevel = 0){
	Click, %foundX%, %foundY%
	Sleep, 300
	Send, {Esc}
}
If(ErrorLevel = 1){
	ImageSearch, foundX, foundY, 0, 0, %A_ScreenWidth%, %A_ScreenHeight%, *100 image2.png
	If(ErrorLevel = 0){
		Send, !vm
		loop, 500 {
			ImageSearch, foundX, foundY, 0, 0, %A_ScreenWidth%, %A_ScreenHeight%, *100 image3.png
			If(ErrorLevel = 0){
				Click, %foundX%, %foundY%
				Break
			}
		}
	}
}
return

Thank you for the tip. Yes, that is what i meant, sorry.
Image visible:
image.png
image.png (666 Bytes) Viewed 2056 times
Image not visible:
image.png
image.png (733 Bytes) Viewed 2056 times


I verified that the image can be found. It appears that there is something wrong elsewhere.
User avatar
boiler
Posts: 16902
Joined: 21 Dec 2014, 02:44

Re: Imagesearch and if statement

22 Apr 2021, 11:15

The other thing you have to consider is the timing and logic of the script. So in your script, it will only search for image2 if image1 was not found, and it only searches for it once. When you press and release F4, is either image1 or image2 visible on the screen at that instant? And you’ve tried it when only image2 was visible while image1 was not?
User avatar
mero
Posts: 11
Joined: 20 Aug 2017, 14:54

Re: Imagesearch and if statement

22 Apr 2021, 11:35

Yes, i've tried it when only image 2 is visible. The first part of the script works perfectly fine when image 1 is there.

If only image 1 is visible -> it clicks image 1
If image 1 and image 2 are visible -> it clicks image 1
If only image 2 is visible -> nothing happens. No mouse movements or inputs.

Edit: I just tried swapping image 1 and image 2 on the script. It finds image 2 with no issue when swapped. idk what is going on with that second imagesearch line.
Last edited by mero on 22 Apr 2021, 12:11, edited 2 times in total.
User avatar
boiler
Posts: 16902
Joined: 21 Dec 2014, 02:44

Re: Imagesearch and if statement

22 Apr 2021, 12:10

Are you sure Send , !vm works? Have you tried putting a line like MsgBox, got here instead?
User avatar
mero
Posts: 11
Joined: 20 Aug 2017, 14:54

Re: Imagesearch and if statement

22 Apr 2021, 12:12

I found the issue. I think the errorlevel check for image2 was still comparing to the image1 results.

Changing

Code: Select all

(If ErrorLevel == 1){
to

Code: Select all

Else{
appears to fix that

Weird.

Code: Select all

F4::
KeyWait, F4
CoordMode, Pixel, Screen
CoordMode, Mouse, Screen
ImageSearch, foundX, foundY, 0, 0, %A_ScreenWidth%, %A_ScreenHeight%, *50 image1.png
If(ErrorLevel = 0){
	Click, %foundX%, %foundY%
	Sleep, 300
	Send, {Esc}
}
Else{
	ImageSearch, foundX, foundY, 0, 0, %A_ScreenWidth%, %A_ScreenHeight%, *50 image2.png
	If(ErrorLevel = 0){
		Send, !vm
		loop, 500 {
			ImageSearch, foundX, foundY, 0, 0, %A_ScreenWidth%, %A_ScreenHeight%, *50 image3.png
			If(ErrorLevel = 0){
				Click, %foundX%, %foundY%
				Break
			}
		}
	}
}
return
User avatar
boiler
Posts: 16902
Joined: 21 Dec 2014, 02:44

Re: Imagesearch and if statement  Topic is solved

22 Apr 2021, 12:32

mero wrote:
22 Apr 2021, 12:12
I found the issue. I think the errorlevel check for image2 was still comparing to the image1 results.
It couldn't have been doing that if you executed another ImageSearch. Makes me wonder if you're really not getting an ErrorLevel of 2 after the first ImageSearch because unless you had a problem with the first image file, its result would have been 1 or 2.
User avatar
mero
Posts: 11
Joined: 20 Aug 2017, 14:54

Re: Imagesearch and if statement

22 Apr 2021, 13:54

That is a reasonable answer. However, it is weird that image 1 was able to be found without issue and swapping image 1 and image 2 in the script allowed image 2 to be found without issue. Not sure what was causing a 2 without moving them from the directory.:wtf:

Regardless, it accomplishes the same goal either way, so I'm satisfied. Thank you for your help.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: dipahk and 245 guests