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.
Imagesearch and if statement Topic is solved
Re: Imagesearch and if statement
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.
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
Re: Imagesearch and if statement
boiler wrote: ↑22 Apr 2021, 00:34If 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 not visible:
I verified that the image can be found. It appears that there is something wrong elsewhere.
Re: Imagesearch and if statement
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?
Re: Imagesearch and if statement
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.
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.
Re: Imagesearch and if statement
Are you sure Send , !vm works? Have you tried putting a line like MsgBox, got here instead?
Re: Imagesearch and if statement
I found the issue. I think the errorlevel check for image2 was still comparing to the image1 results.
Changing to appears to fix that
Weird.
Changing
Code: Select all
(If ErrorLevel == 1){
Code: Select all
Else{
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
Re: Imagesearch and if statement Topic is solved
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.
Re: Imagesearch and if statement
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.
Regardless, it accomplishes the same goal either way, so I'm satisfied. Thank you for your help.
Regardless, it accomplishes the same goal either way, so I'm satisfied. Thank you for your help.