Imagesearch returns X and Y values swapped Topic is solved

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
Cyrano
Posts: 12
Joined: 29 Mar 2023, 17:03

Imagesearch returns X and Y values swapped

Post by Cyrano » 29 Mar 2023, 17:32

If I understand the syntax correctly, the first output variable name I specify will be assigned the X value of the search result, and the second output variable name I specify will be assigned the Y value of the search result.

I'm getting the returned values swapped. The first output variable name I specify is getting assigned the Y value of the search result, and the second output variable name I specify is getting assigned the X value of the search result.

TIA for any guidance on why this is happening.

User avatar
boiler
Posts: 16925
Joined: 21 Dec 2014, 02:44

Re: Imagesearch returns X and Y values swapped

Post by boiler » 29 Mar 2023, 17:56

That's not really possible. Please post your script (between [code][/code] tags), the results you're getting, and what you think the results should be.

User avatar
mikeyww
Posts: 26883
Joined: 09 Sep 2014, 18:38

Re: Imagesearch returns X and Y values swapped

Post by mikeyww » 29 Mar 2023, 20:39

Welcome to this AutoHotkey forum!

If multiple images exist, it is possible for the images to exist in a way that the unexpected image is found and happens to have the inverted coordinates.
Since the search starts at the top row of the region and moves downward, if there is more than one match, the one closest to the top will be found.

Cyrano
Posts: 12
Joined: 29 Mar 2023, 17:03

Re: Imagesearch returns X and Y values swapped

Post by Cyrano » 30 Mar 2023, 07:55

You are correct: the values for X and Y are being assigned correctly to the output variables. My confusion arose from not understanding the behavior of MsgBox.

I expected things to appear in the MsgBox pop-up in the same order as in my code -- but this is not so, as seen in this example. From my code, I expected the X output variable to be shown, then the Y output variable -- but that's not the sequence in which MsgBox displayed the variables.

Code: Select all

FoundX := "0"
FoundY := "0"

ImageSearch &FoundX, &FoundY, 0, 0, A_ScreenWidth, A_ScreenHeight, "Target.png"
MouseMove FoundX, FoundY
MsgBox "X =" FoundX, "Y =" FoundY
Image

User avatar
boiler
Posts: 16925
Joined: 21 Dec 2014, 02:44

Re: Imagesearch returns X and Y values swapped  Topic is solved

Post by boiler » 30 Mar 2023, 09:00

It's not that MsgBox puts them in the wrong order. The issue is that you put them in two different parameters. You put the X value in the Text parameter, and since you separated the two values with a comma, your Y value ended up in the Title parameter, making it appear "first" to you.

User avatar
mikeyww
Posts: 26883
Joined: 09 Sep 2014, 18:38

Re: Imagesearch returns X and Y values swapped

Post by mikeyww » 30 Mar 2023, 09:03

Here is a fix if you need it.

Code: Select all

#Requires AutoHotkey v2.0
FoundX := 1
FoundY := 2
MsgBox 'X = ' FoundX '`nY = ' FoundY, 'Coordinates'
The moral of the story is not how to use the MsgBox function. It is the value of posting your problematic script for readers to examine and test. Doing that in the first post saves everyone a bunch of time and effort.

Cyrano
Posts: 12
Joined: 29 Mar 2023, 17:03

Re: Imagesearch returns X and Y values swapped

Post by Cyrano » 30 Mar 2023, 11:03

boiler wrote:
30 Mar 2023, 09:00
It's not that MsgBox puts them in the wrong order. The issue is that you put them in two different parameters. You put the X value in the Text parameter, and since you separated the two values with a comma, your Y value ended up in the Title parameter, making it appear "first" to you.
Now I understand! Thanks.

Cyrano
Posts: 12
Joined: 29 Mar 2023, 17:03

Re: Imagesearch returns X and Y values swapped

Post by Cyrano » 30 Mar 2023, 11:05

mikeyww wrote:
30 Mar 2023, 09:03
The moral of the story is not how to use the MsgBox function. It is the value of posting your problematic script for readers to examine and test. Doing that in the first post saves everyone a bunch of time and effort.
Understood.

Post Reply

Return to “Ask for Help (v2)”