ImageSearch reliability

Ask gaming related questions (AHK v1.1 and older)
Dobbythenerd1
Posts: 6
Joined: 14 Apr 2024, 02:55

ImageSearch reliability

14 Apr 2024, 03:05

Hello everyone.

First off I'd like to say that AHK has been a fantastic tool and I am thoroughly enjoying the process of making scripts and tinkering. Learning this has been very enjoyable for me. I mainly use it to automate things on games.

I have come across some strange behaviour however that maybe this forum could shed some light on. I am having problems with imagesearch.

When I run this script. It works everytime.

Code: Select all

ImageSearch, FoundX, FoundY,  0, 0, A_ScreenWidth, A_ScreenHeight, image.png
However, when i define an area it does not work at all or very little.

Code: Select all

ImageSearch, FoundX, FoundY,  10, 10, 550, 550, image.png
I've been tinkering with it for a few days but can't seem to figure it out. I understand that pixel variations can cause issues but the whole screen search works fine most times but the defined area doesn't. I am using Coordmode window for the defined area search.

Any advise?

[Mod edit: Moved topic from AHK v2 help since this is v1 code. I also added some missing I's in front of 'magesearch'.]
User avatar
boiler
Posts: 16996
Joined: 21 Dec 2014, 02:44

Re: ImageSearch reliability

14 Apr 2024, 03:59

It’s looking like the issue is that the window in which the image would be found is not the active window when you run the script. That’s what Window in CcordMode means — relative to the active window.
Dobbythenerd1
Posts: 6
Joined: 14 Apr 2024, 02:55

Re: ImageSearch reliability

14 Apr 2024, 04:50

boiler wrote:
14 Apr 2024, 03:59
It’s looking like the issue is that the window in which the image would be found is not the active window when you run the script. That’s what Window in CcordMode means — relative to the active window.
Thanks for the reply. What your saying makes total sense. So in my script i use this.

Code: Select all

IfWinExist, ahk_exe program.exe, ; Look for window AND Select
{
WinActivate, ahk_exe program.exe
WinwaitActive, ahk_exe program.exe,
}
Would that not get around this issue?
User avatar
boiler
Posts: 16996
Joined: 21 Dec 2014, 02:44

Re: ImageSearch reliability

14 Apr 2024, 05:04

Yes, that should activate the window if you specify the process name correctly. You should post an entire script that you are trying to use to find the image (just a minimal script containing all the necessary code for finding the image).
Dobbythenerd1
Posts: 6
Joined: 14 Apr 2024, 02:55

Re: ImageSearch reliability

14 Apr 2024, 11:22

Thanks for the advise. I think the issue i was having was being caused by my hotkey being below the WinActive. When pressing the hotkey it was running the script before activating the window. Now im having an odd issue with mousemove however.

I use a function to randomise my mouse clicks which works fine but the speed is stuck on 0 from what i can gather as the mouse moves are instant.

Code: Select all

CloseMouseMove(X,Y){
Random, xchange, -5, 5
Random, ychange, -5, 5
X:= X + xchange
Y:= Y + ychange
Random, mspeed, 15, 20
MouseMove %X%, %Y%, %mspeed%
Return
}

Code: Select all

Loop{ ; Check if spot is there
	ImageSearch, X, Y, 18, 52, 566, 483, includes\images\image.png
If ErrorLevel { ;  spot is not there
;spot has not spawned in yet. Wait here.
}
else { ; spot has spawned in
X:=X+10
CloseMouseMove(X,Y)
User avatar
boiler
Posts: 16996
Joined: 21 Dec 2014, 02:44

Re: ImageSearch reliability

14 Apr 2024, 13:00

I asked you to post a complete script that is exactly what you would run to demonstrate your issue. Even the two snippets you posted taken together don’t do that because it doesn’t show how or where you are activating the window and defining the coordinate mode relative to the rest of the script. I’ll continue to help if you try to follow what I’m saying in my posts. Read my prior post again carefully.
Dobbythenerd1
Posts: 6
Joined: 14 Apr 2024, 02:55

Re: ImageSearch reliability

15 Apr 2024, 02:44

My bad. I have managed to get it working now. I changed the CloseMouseMove(X,Y) mspeed variable to 20 instead of it being random and it worked perfectly. I then changed it back to exactly how it is above and it worked as intended again. Not sure what caused it. Thank you for the previous advice. It really helped me figure out what was causing the issue.

Return to “Gaming Help (v1)”

Who is online

Users browsing this forum: No registered users and 45 guests