New to AHK Topic is solved

Ask gaming related questions
redbranch
Posts: 2
Joined: 15 Oct 2019, 08:41

New to AHK

15 Oct 2019, 10:44

So I have other scripting language experience, and this *seems* to me like it should work, but it's really hit or miss.
This is a point and click game. The idea I had was to wait until item shows up as available (First and second image search), when that item becomes available, click the appropriate images to collect the reward (Subsequent image search(s)). Or in case of error, close the window and continue on the iteration. I have never, in the last day or two working on this, had it run all the way through. Sometimes it works for one iteration and not the next, sometimes it sits and does nothing. I'm not AHK experienced, so I'm not sure if there is something obvious that I'm missing. :headwall:

Code: Select all

#NoEnv
SetWorkingDir %A_ScriptDir%
CoordMode, Screen, Window
SendMode Input
#SingleInstance Force
SetTitleMatchMode 2
#WinActivateForce
SetControlDelay 1
SetWinDelay 0
SetKeyDelay -1
SetMouseDelay -1
SetBatchLines -1

F3::
Macro1:

Loop
{
	ItemACount = 0
	while ItemACount < 2 {
		CoordMode, Pixel, Window
		ImageSearch FoundX, FoundY, 0, 0, A_ScreenWidth, A_ScreenHeight, C:\Users\winuser\Documents\comp\comp7.png
		if ErrorLevel = 0 
		{
			Click, %FoundX%, %FoundY%, Left, 1
			Sleep, 100
		}
		CoordMode, Pixel, Window
		ImageSearch FoundX, FoundY, 0, 0, A_ScreenWidth, A_ScreenHeight, C:\Users\winuser\Documents\comp\comp1.png
		if ErrorLevel = 0 
		{
			Click, %FoundX%, %FoundY%, Left, 1
			Sleep, 1000
			CoordMode, Pixel, Window
			ImageSearch FoundX, FoundY, 0, 0, A_ScreenWidth, A_ScreenHeight, C:\Users\winuser\Documents\comp\comp2.png
			if ErrorLevel = 0
				{
				Click, %FoundX%, %FoundY%, Left, 1 
				Sleep, 500
				}
			else
				{
					CoordMode, Pixel, Window
					ImageSearch FoundX, FoundY, 0, 0, A_ScreenWidth, A_ScreenHeight, C:\Users\winuser\Documents\comp\err1.png
						if ErrorLevel = 0
						{
							Click, %FoundX%, %FoundY%, Left, 1 
							Sleep, 500
						}
						
				}
			Sleep, 500
			CoordMode, Pixel, Window
			ImageSearch FoundX, FoundY, 0, 0, A_ScreenWidth, A_ScreenHeight, C:\Users\winuser\Documents\comp\comp3.png
			if ErrorLevel = 0
				{
				Click, %FoundX%, %FoundY%, Left, 1
				Sleep, 500				
				}
			else
				{
					CoordMode, Pixel, Window
					ImageSearch FoundX, FoundY, 0, 0, A_ScreenWidth, A_ScreenHeight, C:\Users\winuser\Documents\comp\err2.png
						if ErrorLevel = 0
						{
							Click, %FoundX%, %FoundY%, Left, 1
							Sleep, 500
						}
				}
			Sleep, 500
			ItemACount += 1
		}
	}
	
	ItemBCount = 0
	while ItemBCount < 7 {
		CoordMode, Pixel, Window
		ImageSearch FoundX, FoundY, 0, 0, A_ScreenWidth, A_ScreenHeight, C:\Users\winuser\Documents\comp\comp7.png
		if ErrorLevel = 0 
		{
			Click, %FoundX%, %FoundY%, Left, 1
			Sleep, 100
		}
		CoordMode, Pixel, Window
		ImageSearch FoundX, FoundY, 0, 0, A_ScreenWidth, A_ScreenHeight, C:\Users\winuser\Documents\comp\comp1.png
		if ErrorLevel = 0 
		{
			Click, %FoundX%, %FoundY%, Left, 1
			Sleep, 500
			CoordMode, Pixel, Window
			ImageSearch FoundX, FoundY, 0, 0, A_ScreenWidth, A_ScreenHeight, C:\Users\winuser\Documents\comp\comp4.png
			if ErrorLevel = 0
				{
				Click, %FoundX%, %FoundY%, Left, 1
				Sleep, 500
				}
			else
				{
					CoordMode, Pixel, Window
					ImageSearch FoundX, FoundY, 0, 0, A_ScreenWidth, A_ScreenHeight, C:\Users\winuser\Documents\comp\err1.png
						if ErrorLevel = 0
						{
							Click, %FoundX%, %FoundY%, Left, 1
							Sleep, 500							
						}
				}
			Sleep, 500
			CoordMode, Pixel, Window
			ImageSearch FoundX, FoundY, 0, 0, A_ScreenWidth, A_ScreenHeight, C:\Users\winuser\Documents\comp\comp3.png
			if ErrorLevel = 0
				{
				Click, %FoundX%, %FoundY%, Left, 1
				Sleep, 500
				}
			else
				{
					CoordMode, Pixel, Window
					ImageSearch FoundX, FoundY, 0, 0, A_ScreenWidth, A_ScreenHeight, C:\Users\winuser\Documents\comp\err2.png
						if ErrorLevel = 0
						{
							Click, %FoundX%, %FoundY%, Left, 1
							Sleep, 500
						}
				}
			Sleep, 500
			ItemBCount += 1
		}
	}
	
	ItemCCount = 0
	while ItemCCount < 2 {
		CoordMode, Pixel, Window
		ImageSearch FoundX, FoundY, 0, 0, A_ScreenWidth, A_ScreenHeight, C:\Users\winuser\Documents\comp\comp7.png
		if ErrorLevel = 0 
		{
			Click, %FoundX%, %FoundY%, Left, 1
			Sleep, 100
		}
		CoordMode, Pixel, Window
		ImageSearch FoundX, FoundY, 0, 0, A_ScreenWidth, A_ScreenHeight, C:\Users\winuser\Documents\comp\comp1.png
		if ErrorLevel = 0 
		{
			Click, %FoundX%, %FoundY%, Left, 1
			Sleep, 500
			CoordMode, Pixel, Window
			ImageSearch FoundX, FoundY, 0, 0, A_ScreenWidth, A_ScreenHeight, C:\Users\winuser\Documents\comp\comp5.png
			if ErrorLevel = 0
				{
				Click, %FoundX%, %FoundY%, Left, 1
				Sleep, 500
				}
			else
				{
					CoordMode, Pixel, Window
					ImageSearch FoundX, FoundY, 0, 0, A_ScreenWidth, A_ScreenHeight, C:\Users\winuser\Documents\comp\err1.png
						if ErrorLevel = 0
						{
							Click, %FoundX%, %FoundY%, Left, 1
							Sleep, 500
						}
				}
			Sleep, 500
			CoordMode, Pixel, Window
			ImageSearch FoundX, FoundY, 0, 0, A_ScreenWidth, A_ScreenHeight, C:\Users\winuser\Documents\comp\comp6.png
			if ErrorLevel = 0
				{
				Click, %FoundX%, %FoundY%, Left, 1
				Sleep, 500
				}
			else
				{
					CoordMode, Pixel, Window
					ImageSearch FoundX, FoundY, 0, 0, A_ScreenWidth, A_ScreenHeight, C:\Users\winuser\Documents\comp\err2.png
						if ErrorLevel = 0
						{
							Click, %FoundX%, %FoundY%, Left, 1
							Sleep, 500
						}
				}
			Sleep, 500
			ItemCCount += 1
		}
	}
}
Esc::ExitApp
gregster
Posts: 3275
Joined: 30 Sep 2013, 06:48

Re: New to AHK  Topic is solved

15 Oct 2019, 23:45

Welcome to the forum!

After a very quick look, I don't see anything too obvious apart from CoordMode, Screen, Window at the top which isn't a thing - I would remove it: Screen is not a valid first parameter. (The constantly repeated CoordMode, Pixel, Window lines, on the other hand, are unnecessary unless the Coordmode is actually changed in-between - by default, Imagesearch and Click use window-relative coordinates anyway, afaik, unless you change that.)

You should probably start debugging the code part by part to see where it doesn't do what you want, especially at which point the code 'sits and does nothing'.

You are using png-images for the Imagesearch, which is good, but you never allow any variation, what might be problematic - especially in a game, where objects move and shadows, backgrounds and textures might sightly vary depending on the position of the object:
https://www.autohotkey.com/docs/commands/ImageSearch.htm#Parameters wrote:ImageFile
[...]
*n (variation): Specify for n a number between 0 and 255 (inclusive) to indicate the allowed number of shades of variation in either direction for the intensity of the red, green, and blue components of each pixel's color. For example, *2 would allow two shades of variation. This parameter is helpful if the coloring of the image varies slightly or if ImageFile uses a format such as GIF or JPG that does not accurately represent an image on the screen. If you specify 255 shades of variation, all colors will match. The default is 0 shades.
Also, it is recommended to use images of minimal size that are still unique... and small search areas, if possible. (Not sure why you even use window-relative coordinates, if you want to search the whole screen anyway. Note that there are often problems with games in fullscreen mode, but (borderless) windowed mode, or whatever it's called, often works better.)

Btw, your script won't look for these items A, B and C at the same time, but one after another - look at SetTimer, if you want some (pseudo-)multithreading instead for the search of the different items.

Disclaimer: I am not a gamer and rarely use Imagesearch, at least not with multiple and/or moving objects ;)
redbranch
Posts: 2
Joined: 15 Oct 2019, 08:41

Re: New to AHK

17 Oct 2019, 10:03

Thank you Gregster. Adding the variance helped get the script working! I appreciate the info!

Return to “Gaming”

Who is online

Users browsing this forum: No registered users and 12 guests