Loop-ImageSearch on Active Screen

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Galaxis
Posts: 73
Joined: 04 Feb 2016, 20:09

Loop-ImageSearch on Active Screen

20 Apr 2017, 20:53

I'm having a difficult time adding a loop to this Imagesearch script. This works fine, but does not loop.

Code: Select all

$1::

imageAppeared := false
while (imageAppeared = false)
{
Imagesearch, FoundX, FoundY, 0, 0, A_ScreenWidth, A_ScreenHeight, C:\Users\ME\Desktop\SFV\Mika\m1.png
X:= FoundX
Y := FoundY
if (X <> "")
{
imageAppeared := true
}
}
Msgbox, POWER!
return



F1::suspend
return

Esc::ExitApp
return

F5::reload
return


I thought to myself- maybe this would be possible- but no dice

Code: Select all

1::
Loop
{

imageAppeared := false
while (imageAppeared = false)
{
Imagesearch, FoundX, FoundY, 0, 0, A_ScreenWidth, A_ScreenHeight, C:\Users\ME\Desktop\SFV\Mika\m1.png
X:= FoundX
Y := FoundY
if (X <> "")
{
imageAppeared := true
}
}
Msgbox, POWER!
return
}


F1::suspend
return

Esc::ExitApp
return

F5::reload
return
Xeno234
Posts: 71
Joined: 24 Mar 2017, 18:14

Re: Loop-ImageSearch on Active Screen

20 Apr 2017, 21:16

Having an unconditional return inside a loop kind of makes it not a loop.

Simplifying a bit:

Code: Select all

1::
Loop {
	loop {
		Imagesearch, FoundX, FoundY, 0, 0, A_ScreenWidth, A_ScreenHeight, C:\Users\ME\Desktop\SFV\Mika\m1.png
		if !errorlevel
			break
		sleep 100
	}
	Msgbox, POWER! #%a_index%
	sleep 1000
}
return


F1::suspend
return

Esc::ExitApp
return

F5::reload
return
A_AhkUser
Posts: 1147
Joined: 06 Mar 2017, 16:18
Location: France
Contact:

Re: Loop-ImageSearch on Active Screen

20 Apr 2017, 21:19

Hi,

First you should take advantage of ErrorLevel, which is set by the command ImageSearch - documentation:
ErrorLevel is set to 0 if the image was found in the specified region, 1 if it was not found, or 2 if there was a problem that prevented the command from conducting the search (such as failure to open the image file or a badly formatted option).

Code: Select all

$1::

Loop
{
Imagesearch, FoundX, FoundY, 0, 0, A_ScreenWidth, A_ScreenHeight, %A_Desktop%/CAPTURE.PNG
if (ErrorLevel) ; 1 (not found) or 2 (problem)
	break
	Msgbox, POWER from the loop!
}
Msgbox, POWER after the loop!
return
Hope this helps.


EDIT*: while loop replaced by a loop/untilto avoid begin a new iteration and call ImageSearch once again after imageAppeared has been set to false.

EDIT**: loop/until loop replaced by an loop/break (having trouble tonight :yawn: ) like suggest btw by Xeno234 above.
my scripts

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: peter_ahk and 329 guests