Mouse Around

Ask gaming related questions (AHK v1.1 and older)
3rne5t0
Posts: 48
Joined: 16 Jun 2018, 01:36

Mouse Around

Post by 3rne5t0 » 21 May 2022, 05:27

Got this little Code to search around Mouse position. Atm its not working did i anything wrong?

Code: Select all

~Space::
While, GetKeyState("Space","P") {
MouseGetPos, MousePOSX, MousePOSY
ImageSearch, X, Y, %MousePOSX%-=300, %MousePOSY%-=300, %MousePOSX%+=300, %MousePOSY%+=300, *20 Healthbar.bmp
If (ErrorLevel == 0) {
	xa := x+80
    ya := y+180
	MouseMove, %xa%, %ya%
	send, q
}
}
Return

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

Re: Mouse Around

Post by mikeyww » 21 May 2022, 05:38

Try something like the following.

Code: Select all

MousePOSY -= 300
If it does not work, start with just a two-line script. In the first line, do an ImageSearch that you believe will work. Hard-code the coordinates in your test. In the second line, display the ErrorLevel from the search. If you get an ErrorLevel of 2, specify the full path to your image, and re-test. If your ErrorLevel is 1, then the image was not found, and you can backtrack to determine why.

A demonstration is below. What do you think the MsgBox will show?

Code: Select all

x = 10
; x += 5
%x% += 5
MsgBox, % x
In most circumstances, assignment of variables does not use % on the left side. Using % around variable names in numeric command parameters is optional. For an assignment there, you would omit %.
Last edited by mikeyww on 21 May 2022, 05:47, edited 1 time in total.

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

Re: Mouse Around

Post by boiler » 21 May 2022, 05:43

Is that really how you got that code? Because your search rectangle definition parameters do not make sense.

Edit: Saw mikeyww’s edit to question the same things after I posted.

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

Re: Mouse Around

Post by boiler » 21 May 2022, 05:48

mikeyww wrote: Try something like the following.

Code: Select all

MousePOSY -= 300
I also question why the operators -= and +=are even being used instead of simply + and -. These are actually changing the value of the variable (once the % are removed, as you point out should be done), so MousePOSY would become 300 less than its original value, and when MousePOSY += 300 later occurs, it would just be returning it to its original nominal value, not adding 300 to the original value.

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

Re: Mouse Around

Post by mikeyww » 21 May 2022, 05:49

Very good point!

3rne5t0
Posts: 48
Joined: 16 Jun 2018, 01:36

Re: Mouse Around

Post by 3rne5t0 » 21 May 2022, 06:37

Code: Select all

~Space::
While, GetKeyState("Space","P") {
MouseGetPos, MousePOSX, MousePOSY
ImageSearch, X, Y, MousePOSX - 300, MousePOSY - 300, MousePOSX + 300, MousePOSY + 300, *20 Healthbar.bmp
If (ErrorLevel == 0) {
	xa := x+80
    ya := y+180
	MouseMove, %xa%, %ya%
}
}
Return
This seems to work

Post Reply

Return to “Gaming Help (v1)”