Looking for help simplifying and getting an imagesearch script to work

Get help with using AutoHotkey and its commands and hotkeys
Guest

Looking for help simplifying and getting an imagesearch script to work

19 Nov 2016, 14:36

I am looking for help to try and simplify and get this script to work. I am sure there was a better way to write it with functions so I did not have to do an imagesearch IF/click command for each possible encounter.
It seems to somewhat work until the longer if/thens i have (which i do not think im scripting properly)

For instance:

Code: Select all

ImageSearch, FoundX, FoundY, 75, 45, 1239, 662, *100 C:\Users\Bill\Desktop\Macro\4starblue.bmp
	If (ErrorLevel = 0)
	{
		ImageSearch, FoundX, FoundY, 75, 45, 1239, 662, *100 C:\Users\Bill\Desktop\Macro\sell.bmp
		If (ErrorLevel = 0)
		{
			Click, %FoundX%, %FoundY% Left, 1
			ImageSearch, FoundX, FoundY, 75, 45, 1239, 662, *100 C:\Users\Bill\Desktop\Macro\yes.bmp
			If (ErrorLevel = 0)
				Click, %FoundX%, %FoundY% Left, 1
It will find the 4starblue.bmp, it will click sell, but it will not then click yes. Any help would be appreciated. I have the same issues with all my longer if/thens. I also attached the full script in case anyone wants to help me simplify it so its easier to edit/add encounters.

Thank you very much in advance.

Code: Select all

WinWait, BlueStacks App Player, _ctl.Window
IfWinNotActive, BlueStacks App Player, _ctl.Window, WinActivate, BlueStacks App Player, _ctl.Window
WinWaitActive, BlueStacks App Player, _ctl.Window

CoordMode, Pixel, client
Coordmode, Mouse, client

Loop
{
	ImageSearch, FoundX, FoundY, 75, 45, 1239, 662, *100 C:\Users\Bill\Desktop\Macro\Start.bmp
	If (ErrorLevel = 0)
		Click, %FoundX%, %FoundY% Left, 1

	ImageSearch, FoundX, FoundY, 75, 45,  1284, 715, *100 C:\Users\Bill\Desktop\Macro\Arrow.bmp
	If (ErrorLevel = 0)
		Click, %FoundX%, %FoundY% Left, 1

	ImageSearch, FoundX, FoundY, 75, 45, 1239, 662, *100 C:\Users\Bill\Desktop\Macro\Replay.bmp
	If (ErrorLevel = 0)
		Click, %FoundX%, %FoundY% Left, 1

	ImageSearch, FoundX, FoundY, 75, 45, 1239, 662, *100 C:\Users\Bill\Desktop\Macro\Reward.bmp
	If (ErrorLevel = 0)
		Click, %FoundX%, %FoundY% Left, 1

	ImageSearch, FoundX, FoundY, 75, 45, 1239, 662, *100 C:\Users\Bill\Desktop\Macro\Chest.bmp
	If (ErrorLevel = 0)
		Click, %FoundX%, %FoundY% Left, 1

	ImageSearch, FoundX, FoundY, 75, 45, 1239, 662, *100 C:\Users\Bill\Desktop\Macro\6starpurple.bmp
	If (ErrorLevel = 0)
	{
		ImageSearch, FoundX, FoundY, 75, 45, 1239, 662, *100 C:\Users\Bill\Desktop\Macro\get.bmp
		If (ErrorLevel = 0)
			Click, %FoundX%, %FoundY% Left, 1
	}

	ImageSearch, FoundX, FoundY, 75, 45, 1239, 662, *100 C:\Users\Bill\Desktop\Macro\5starlegend.bmp
	If (ErrorLevel = 0)
	{
		ImageSearch, FoundX, FoundY, 75, 45, 1239, 662, *100 C:\Users\Bill\Desktop\Macro\get.bmp
		If (ErrorLevel = 0)
			Click, %FoundX%, %FoundY% Left, 1
	}

	ImageSearch, FoundX, FoundY, 75, 45, 1239, 662, *100 C:\Users\Bill\Desktop\Macro\5starpurple.bmp
	If (ErrorLevel = 0)
	{
		ImageSearch, FoundX, FoundY, 75, 45, 1239, 662, *100 C:\Users\Bill\Desktop\Macro\get.bmp
		If (ErrorLevel = 0)
			Click, %FoundX%, %FoundY% Left, 1
	}

	ImageSearch, FoundX, FoundY, 75, 45, 1239, 662, *100 C:\Users\Bill\Desktop\Macro\5starblue.bmp
	If (ErrorLevel = 0)
	{
		ImageSearch, FoundX, FoundY, 75, 45, 1239, 662, *100 C:\Users\Bill\Desktop\Macro\sell.bmp
		If (ErrorLevel = 0)
		{
			Click, %FoundX%, %FoundY% Left, 1
			ImageSearch, FoundX, FoundY, 75, 45, 1239, 662, *100 C:\Users\Bill\Desktop\Macro\yes.bmp
			If (ErrorLevel = 0)
				Click, %FoundX%, %FoundY% Left, 1
		}
	}

	ImageSearch, FoundX, FoundY, 75, 45, 1239, 662, *100 C:\Users\Bill\Desktop\Macro\4starblue.bmp
	If (ErrorLevel = 0)
	{
		ImageSearch, FoundX, FoundY, 75, 45, 1239, 662, *100 C:\Users\Bill\Desktop\Macro\sell.bmp
		If (ErrorLevel = 0)
		{
			Click, %FoundX%, %FoundY% Left, 1
			ImageSearch, FoundX, FoundY, 75, 45, 1239, 662, *100 C:\Users\Bill\Desktop\Macro\yes.bmp
			If (ErrorLevel = 0)
				Click, %FoundX%, %FoundY% Left, 1
		}
	}

	ImageSearch, FoundX, FoundY, 75, 45, 1239, 662, *100 C:\Users\Bill\Desktop\Macro\NO.bmp
	If (ErrorLevel = 0)
		Click, %FoundX%, %FoundY% Left, 1

	ImageSearch, FoundX, FoundY, 75, 45, 1239, 662, *100 C:\Users\Bill\Desktop\Macro\ok.bmp
	If (ErrorLevel = 0)
		Click, %FoundX%, %FoundY% Left, 1
}

ESC::ExitApp
BBock727
Posts: 6
Joined: 19 Nov 2016, 14:30

Re: Looking for help simplifying and getting an imagesearch script to work

19 Nov 2016, 18:00

This is my question. If anyone can help, id greatly appreciate it
ahcahc
Posts: 110
Joined: 25 Jul 2014, 23:55

Re: Looking for help simplifying and getting an imagesearch script to work

19 Nov 2016, 19:13

Try adding sleep before every imagesearch (except the first one).

Here is what I use when doing imagesearch.

Code: Select all

if (imgSearchWait("1.bmp", 0, 0, A_ScreenWidth, A_ScreenHeight, 5) and imgSearchWait("2.bmp", 0, 0, A_ScreenWidth, A_ScreenHeight, 5, 1))
	msgbox found 1.bmp and then found & clicked 2.bmp.
else
	msgbox found nothing or only one of them was found.

imgSearchWait(img, x1, y1, x2, y2, timeout := 1, click := 0, variation := 50, offset_x := 0, offset_y := 0)
{
	st := A_TickCount, ct := (A_TickCount - st)/1000
	while (ct <= timeout)
	{
		ImageSearch, oX, oY, x1, y1, x2, y2, *%variation% %img%
		if errorlevel = 0
		{
			if click = 1
			{
				cx := oX + offset_x
				cy := oY + offset_y
				Click %cx% %cy%
				Sleep, 80
			}
			return 1
		}
		Sleep, 100
		ct := (A_TickCount - st)/1000
	}
	return 0
}
BBock727
Posts: 6
Joined: 19 Nov 2016, 14:30

Re: Looking for help simplifying and getting an imagesearch script to work

19 Nov 2016, 21:35

This seems complicated to adapt my script to this. I attempted to add a sleep in various spots and ill let you know if it helps. Could you give me a sample on how you would change my script?
BBock727
Posts: 6
Joined: 19 Nov 2016, 14:30

Re: Looking for help simplifying and getting an imagesearch script to work

19 Nov 2016, 21:35

This seems complicated to adapt my script to this. I attempted to add a sleep in various spots and ill let you know if it helps. Could you give me a sample on how you would change my script?
User avatar
Almost_there
Posts: 404
Joined: 30 Sep 2014, 10:32

Re: Looking for help simplifying and getting an imagesearch script to work

19 Nov 2016, 22:04

Soo - the Imagesearch work ok - and some clicks works and other not so much.

I suspect that there might be an offset issue - that is the click takes place outside the clickable area for the button. Nut of course, without seeing the images and at least one possible screen shot of the program, it's impossible to say.

There might be other graphic effect that make it hard to use imagesearch, and I think on such features as "dimming light" in areas close to mouse pointer, and other similar effects.
ahcahc
Posts: 110
Joined: 25 Jul 2014, 23:55

Re: Looking for help simplifying and getting an imagesearch script to work

19 Nov 2016, 22:56

This

Code: Select all

WinWait, BlueStacks App Player, _ctl.Window
IfWinNotActive, BlueStacks App Player, _ctl.Window, WinActivate, BlueStacks App Player, _ctl.Window
WinWaitActive, BlueStacks App Player, _ctl.Window

CoordMode, Pixel, client
Coordmode, Mouse, client

Loop
{
	ImageSearch, FoundX, FoundY, 75, 45, 1239, 662, *100 C:\Users\Bill\Desktop\Macro\Start.bmp
	If (ErrorLevel = 0)
		Click, %FoundX%, %FoundY% Left, 1

	ImageSearch, FoundX, FoundY, 75, 45,  1284, 715, *100 C:\Users\Bill\Desktop\Macro\Arrow.bmp
	If (ErrorLevel = 0)
		Click, %FoundX%, %FoundY% Left, 1

	ImageSearch, FoundX, FoundY, 75, 45, 1239, 662, *100 C:\Users\Bill\Desktop\Macro\Replay.bmp
	If (ErrorLevel = 0)
		Click, %FoundX%, %FoundY% Left, 1
}
To

Code: Select all

WinWait, BlueStacks App Player, _ctl.Window
IfWinNotActive, BlueStacks App Player, _ctl.Window, WinActivate, BlueStacks App Player, _ctl.Window
WinWaitActive, BlueStacks App Player, _ctl.Window

CoordMode, Pixel, client
Coordmode, Mouse, client

loop
{
imgSearchWait("C:\Users\Bill\Desktop\Macro\Start.bmp", 75, 45, 1239, 662, 2, 1, 100)	;keep searching for the image for 2 seconds and click the image if found, with *100 variation
imgSearchWait("C:\Users\Bill\Desktop\Macro\Arrow.bmp", 75, 45, 1284, 715, 2, 1, 100)
imgSearchWait("C:\Users\Bill\Desktop\Macro\Replay.bmp", 75, 45, 1239, 662, 2, 1, 100)
}


imgSearchWait(img, x1, y1, x2, y2, timeout := 1, click := 0, variation := 50, offset_x := 0, offset_y := 0)
{
	st := A_TickCount, ct := (A_TickCount - st)/1000
	while (ct <= timeout)
	{
		ImageSearch, oX, oY, x1, y1, x2, y2, *%variation% %img%
		if errorlevel = 0
		{
			if click = 1
			{
				cx := oX + offset_x
				cy := oY + offset_y
				Click %cx% %cy%
				Sleep, 80
			}
			return 1
		}
		Sleep, 100
		ct := (A_TickCount - st)/1000
	}
	return 0
}
BBock727
Posts: 6
Joined: 19 Nov 2016, 14:30

Re: Looking for help simplifying and getting an imagesearch script to work

21 Nov 2016, 23:32

Almost_there wrote:Soo - the Imagesearch work ok - and some clicks works and other not so much.

I suspect that there might be an offset issue - that is the click takes place outside the clickable area for the button. Nut of course, without seeing the images and at least one possible screen shot of the program, it's impossible to say.

There might be other graphic effect that make it hard to use imagesearch, and I think on such features as "dimming light" in areas close to mouse pointer, and other similar effects.
All the imagesearches work. For example in my script when it finds 5starblue, it then finds sell and clicks it, but it will not click YES. I think it has to do with how the script was written and not the actual image search.
BBock727
Posts: 6
Joined: 19 Nov 2016, 14:30

Re: Looking for help simplifying and getting an imagesearch script to work

21 Nov 2016, 23:34

this would be awesome to simplify my script but if you notice, not every image must be clicked. I have a bunch of if/thens where if 1 image is found, 2 image is clicked. I would not be able to adapt the rest of the script unfortunately without significant help from you.

ahcahc wrote:This

Code: Select all

WinWait, BlueStacks App Player, _ctl.Window
IfWinNotActive, BlueStacks App Player, _ctl.Window, WinActivate, BlueStacks App Player, _ctl.Window
WinWaitActive, BlueStacks App Player, _ctl.Window

CoordMode, Pixel, client
Coordmode, Mouse, client

Loop
{
	ImageSearch, FoundX, FoundY, 75, 45, 1239, 662, *100 C:\Users\Bill\Desktop\Macro\Start.bmp
	If (ErrorLevel = 0)
		Click, %FoundX%, %FoundY% Left, 1

	ImageSearch, FoundX, FoundY, 75, 45,  1284, 715, *100 C:\Users\Bill\Desktop\Macro\Arrow.bmp
	If (ErrorLevel = 0)
		Click, %FoundX%, %FoundY% Left, 1

	ImageSearch, FoundX, FoundY, 75, 45, 1239, 662, *100 C:\Users\Bill\Desktop\Macro\Replay.bmp
	If (ErrorLevel = 0)
		Click, %FoundX%, %FoundY% Left, 1
}
To

Code: Select all

WinWait, BlueStacks App Player, _ctl.Window
IfWinNotActive, BlueStacks App Player, _ctl.Window, WinActivate, BlueStacks App Player, _ctl.Window
WinWaitActive, BlueStacks App Player, _ctl.Window

CoordMode, Pixel, client
Coordmode, Mouse, client

loop
{
imgSearchWait("C:\Users\Bill\Desktop\Macro\Start.bmp", 75, 45, 1239, 662, 2, 1, 100)	;keep searching for the image for 2 seconds and click the image if found, with *100 variation
imgSearchWait("C:\Users\Bill\Desktop\Macro\Arrow.bmp", 75, 45, 1284, 715, 2, 1, 100)
imgSearchWait("C:\Users\Bill\Desktop\Macro\Replay.bmp", 75, 45, 1239, 662, 2, 1, 100)
}


imgSearchWait(img, x1, y1, x2, y2, timeout := 1, click := 0, variation := 50, offset_x := 0, offset_y := 0)
{
	st := A_TickCount, ct := (A_TickCount - st)/1000
	while (ct <= timeout)
	{
		ImageSearch, oX, oY, x1, y1, x2, y2, *%variation% %img%
		if errorlevel = 0
		{
			if click = 1
			{
				cx := oX + offset_x
				cy := oY + offset_y
				Click %cx% %cy%
				Sleep, 80
			}
			return 1
		}
		Sleep, 100
		ct := (A_TickCount - st)/1000
	}
	return 0
}
ahcahc
Posts: 110
Joined: 25 Jul 2014, 23:55

Re: Looking for help simplifying and getting an imagesearch script to work

22 Nov 2016, 04:15

BBock727 wrote: All the imagesearches work. For example in my script when it finds 5starblue, it then finds sell and clicks it, but it will not click YES. I think it has to do with how the script was written and not the actual image search.
Try changing the click to mousemove with a speed of 30 MouseMove, X, Y, 30 and sendmode to event SendMode Event in order to check where did the mouse moved/clicked (if the image was found). Maybe it missed the intended target.
BBock727 wrote:this would be awesome to simplify my script but if you notice, not every image must be clicked. I have a bunch of if/thens where if 1 image is found, 2 image is clicked. I would not be able to adapt the rest of the script unfortunately without significant help from you.
The 7th parameter is for clicking or not clicking, 1 means it will click the image if found, 0 means it will not click it.

Code: Select all

;search for start.bmp (no click) and click arrow.bmp (if found) and then click Replay.bmp (if found).
if imgSearchWait("C:\Users\Bill\Desktop\Macro\Start.bmp", 75, 45, 1239, 662, 2, 0, 100)	;keep searching for the image every 100ms for 2 seconds and it will not click it, with *100 variation
{
imgSearchWait("C:\Users\Bill\Desktop\Macro\Arrow.bmp", 75, 45, 1284, 715, 2, 1, 100)        ;keep searching for the image  every 100ms for 2 seconds and click it as soon as it is found, with *100 variation
imgSearchWait("C:\Users\Bill\Desktop\Macro\Replay.bmp", 75, 45, 1239, 662, 2, 1, 100)        ;keep searching for the image  every 100ms for 2 seconds and click it as soon as it is found, with *100 variation
}
User avatar
Almost_there
Posts: 404
Joined: 30 Sep 2014, 10:32

Re: Looking for help simplifying and getting an imagesearch script to work

22 Nov 2016, 14:05

No - I know what your problem is! The image you searching for sends the mouse pointer to TO THE EDGE (I've already presented you for this, but you didn't hear me out) of the matching square (the found box area) - wich will be positioned too much to left and above - so therefore you don't click in center of the button, but miss target.

It's the same as when you're aiming (gun practice) at a rectangular target with numbered rings. Then you tries to aim at upper left corner when you fire, and complaints when you realize there is no hits in the center of the target.
BBock727
Posts: 6
Joined: 19 Nov 2016, 14:30

Re: Looking for help simplifying and getting an imagesearch script to work

28 Nov 2016, 12:48

Can either of you help me revamp this script to work better/properly with the suggestions you gave? My scripting ability only takes me so far
User avatar
Almost_there
Posts: 404
Joined: 30 Sep 2014, 10:32

Re: Looking for help simplifying and getting an imagesearch script to work

28 Nov 2016, 17:07

There seems to be a couple of errors - just read the comments.

The one reason that it seems not being working out seems that you've used a one millisecont timeout.

Also - you probably won't let ImageSearch steal CPU time when the wondow isn't active.

Code: Select all

;WinWait, BlueStacks App Player, _ctl.Window
; You don't neeed that when you already have WinWaitActive for the same window.

;IfWinNotActive, BlueStacks App Player, _ctl.Window, WinActivate, BlueStacks App Player, _ctl.Window
; Cannot do it like that. See my suggestion for the loop.
WinWaitActive, BlueStacks App Player, _ctl.Window

CoordMode, Pixel, client
Coordmode, Mouse, client

loop
{
	IfWinNotActive, BlueStacks App Player, _ctl.Window
	{	; Does nothing if correct window in fact is not the active one.
		Sleep 500
		Continue
	}
	imgSearchWait("C:\Users\Bill\Desktop\Macro\Start.bmp", 75, 45, 1239, 662, 2, 1, 100)	;keep searching for the image for 2 seconds and click the image if found, with *100 variation
	imgSearchWait("C:\Users\Bill\Desktop\Macro\Arrow.bmp", 75, 45, 1284, 715, 2, 1, 100)
	imgSearchWait("C:\Users\Bill\Desktop\Macro\Replay.bmp", 75, 45, 1239, 662, 2, 1, 100)
}


; IMPORTANT - your timeout seems to be one millisecond!
imgSearchWait(img, x1, y1, x2, y2, timeout := 1, click := 0, variation := 50, offset_x := 0, offset_y := 0)
{
	st := A_TickCount, ct := (A_TickCount - st)/1000
	while (ct <= timeout)
	{
		ImageSearch, oX, oY, x1, y1, x2, y2, *%variation% %img%
		if errorlevel = 0
		{
			if click = 1
			{
				/*
				cx := oX + offset_x
				cy := oY + offset_y
				Click %cx% %cy%
				*/
				; Alternatively
				MouseMove, oX + offset_x, oY + offset_y, 0
				Click
				Sleep, 80
			}
			return 1
		}
		Sleep, 100
		ct := (A_TickCount - st)/1000
	}
	return 0
}
ahcahc
Posts: 110
Joined: 25 Jul 2014, 23:55

Re: Looking for help simplifying and getting an imagesearch script to work

28 Nov 2016, 22:36

Check out how functions work. https://autohotkey.com/docs/Functions.htm

img = the image file
x1, y1 = upper left corner
x2, y2 = lower right corner
timeout = how long to keep on searching for the image
click = to click(1) or not to click (0) the image
variation = variation
offset_x, offset_y = imagesearch will get the location of the image which is the upper left corner of the image. So if you DO NOT want to click the upper left corner of the image, you specify how many pixels to add inorder to click the approximate center of the image (or the clickable area)

st = start time (milliseconds)
ct = current time (seconds), current duration of the search
timeout = units in seconds

Code: Select all

imgSearchWait(img, x1, y1, x2, y2, timeout := 1, click := 0, variation := 50, offset_x := 0, offset_y := 0)
{
	st := A_TickCount, ct := (A_TickCount - st)/1000
	while (ct <= timeout)				;while current time (duration) is less than or equal to timeout (seconds)
	{
		ImageSearch, oX, oY, x1, y1, x2, y2, *%variation% %img%
		if errorlevel = 0
		{
			if click = 1
			{
				cx := oX + offset_x
				cy := oY + offset_y
				Click %cx% %cy%
				Sleep, 80
			}
			return 1
		}
		Sleep, 100				;repeat every 100ms
		ct := (A_TickCount - st)/1000
	}
	return 0
}

Return to “Ask For Help”

Who is online

Users browsing this forum: ANDRIMYR, Bing [Bot], BobbitWormJoe, Google [Bot], loek6000, popwojciu and 260 guests