Trying to get this auto grinding script to work. Topic is solved

Ask gaming related questions (AHK v1.1 and older)
NotAThrowaway
Posts: 4
Joined: 18 Sep 2017, 19:21

Trying to get this auto grinding script to work.

18 Sep 2017, 19:40

Code: Select all

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn  ; Enable warnings to assist with detecting common errors.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.

#IfWinActive Snes9X v1.54.1 for Windows
	Loop, 99
	{
		CoordMode, Pixel, Screen
		PixelSearch, FoundX, FoundY, 256, 1623, 260, 1630, 0x000080, 20, Fast RGB
		If ErrorLevel = 0
		{
			Loop
			{
				Send, {X down}
			}
		
		}
		Else
		{
			Loop
			{
				Send, {Down down}
				Sleep, 1000
				Send, {Down up}
				Send, {Up down}
				Sleep, 1000
				Send, {Up up}
			}
		}
	}
Return
Now to point out how I want it to work compared to how it actually works:

Code: Select all

#IfWinActive Snes9X v1.54.1 for Windows
I want it to only work when Snes9x is the active window, script doesn't seem to care.

Code: Select all

	Loop, 99
	{
		CoordMode, Pixel, Screen
		PixelSearch, FoundX, FoundY, 256, 1623, 260, 1630, 0x000080, 20, Fast RGB
		If ErrorLevel = 0
		{
			Loop
			{
				Send, {X down}
			}
		
		}
If it finds a certain shade of blue then I must be in a battle and the script should hold down the key mapped to the A button. What really happens is that this part is skipped and it keeps going up and down through my commands.

Code: Select all

Else
		{
			Loop
			{
				Send, {Down down}
				Sleep, 1000
				Send, {Down up}
				Send, {Up down}
				Sleep, 1000
				Send, {Up up}
			}
		}
	}
Return
If the color isn't detected then I'm on the world map and need to move myself until I get into the battle, this kind of works although it goes up and down regardless if the color is present or not.

How do I get this script working as it should?
Rohwedder
Posts: 7616
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: Trying to get this auto grinding script to work.

19 Sep 2017, 11:00

Hallo,
#IfWinActive neither starts scripts nor interrupts running scripts.
I installed a Timer. Now it should start if your window will become active.
I've also stopped your never-ending loops!
To the function, I can unfortunately contribute nothing.

Code: Select all

#Persistent
#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn  ; Enable warnings to assist with detecting common errors.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.
SetTimer, Snes9x, 200
Return ;End of Auto-execute Section https://autohotkey.com/docs/Scripts.htm#auto
Snes9x:
	IfWinNotActive, Snes9X v1.54.1 for Windows
		Return
	SetTimer, Snes9x, Off
	Loop, 99
	{
		CoordMode, Pixel, Screen
		PixelSearch, FoundX, FoundY, 256, 1623, 260, 1630, 0x000080, 20, Fast RGB
		If ErrorLevel = 0
		{
			Loop, 10 ;without the 10, the loop never ends
			{
				Send, {X down}
			}
		
		}
		Else
		{
			Loop, 10 ;without the 10, the loop never ends
			{
				Send, {Down down}
				Sleep, 1000
				Send, {Down up}
				Send, {Up down}
				Sleep, 1000
				Send, {Up up}
			}
		}
	}
	SetTimer, Snes9x, On
Return
NotAThrowaway
Posts: 4
Joined: 18 Sep 2017, 19:21

Re: Trying to get this auto grinding script to work.

19 Sep 2017, 12:52

Rohwedder wrote:I've also stopped your never-ending loops!
To the function, I can unfortunately contribute nothing.
Unfortunately your solution wasn't ideal, it did let me start doing actions during battles but the arrow keys continued and my characters started wasting items on themselves. I tried something like this

Code: Select all

Loop, 99
{
	PixelSearch, Px, Py, 242, 1729, 242, 1729, 0x800000, 5
	Loop
	{
		Send, {Down down}
		Sleep, 10
		Send, {Down up}
		Send, {Up down}
		Sleep, 10
		Send, {Up up}
	}
	Until ErrorLevel = 0
	Loop
	{
		Send, {X down}
		Sleep, 10
		Send, {X up}
	}
	Until ErrorLevel = 1
}
Return
but I'm still having the same issue as the arrow keys continued even when it should be detecting the color and giving an error level of 0. I used another script to test out the pixelsearch function and that's working perfectly. so I'm doing something wrong with ending the loop but I have no idea what, I tried break inbetween posts and that didn't seem to work.
Rohwedder
Posts: 7616
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: Trying to get this auto grinding script to work.

19 Sep 2017, 23:35

Hallo,
this:

Code: Select all

PixelSearch, Px, Py, 242, 1729, 242, 1729, 0x800000, 5
Loop
{
	Send, {Down down}
	Sleep, 10
	Send, {Down up}
	Send, {Up down}
	Sleep, 10
	Send, {Up up}
}
Until ErrorLevel = 0
is definitely wrong! After the PixelSearch, ErrorLevel is either 1 or 0.
If ErrorLevel is 1, you will get a never-ending loop, because
the Sends and Sleeps within the loop do not change Errorlevel!
Only if other threads of the script change ErrorLevel the loop would be terminated.
NotAThrowaway
Posts: 4
Joined: 18 Sep 2017, 19:21

Re: Trying to get this auto grinding script to work.

20 Sep 2017, 11:55

the Sends and Sleeps within the loop do not change Errorlevel!
?

They should, they move the character on the world map until they get into a battle which puts the shade of blue in the proper location giving an error level of 0.
Rohwedder
Posts: 7616
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: Trying to get this auto grinding script to work.

20 Sep 2017, 23:45

If I want to check my scripts, I always write tooltips into it.

Code: Select all

Test = 1 ;On/Off: 1/0
Loop, 99
{
	If Test
		ToolTip, % "ErrorLevel: "ErrorLevel "  LineNumber: " A_LineNumber
	PixelSearch, Px, Py, 242, 1729, 242, 1729, 0x800000, 5
	Loop
	{
		If Test
			ToolTip, % "ErrorLevel: "ErrorLevel "  LineNumber: " A_LineNumber
		Send, {Down down}
		Sleep, 10
		Send, {Down up}
		Send, {Up down}
		Sleep, 10
		Send, {Up up}
	}
	Until ErrorLevel = 0
	Loop
	{
		If Test
			ToolTip, % "ErrorLevel: "ErrorLevel "  LineNumber: " A_LineNumber
		Send, {X down}
		Sleep, 10
		Send, {X up}
	}
	Until ErrorLevel = 1
}
Return
NotAThrowaway
Posts: 4
Joined: 18 Sep 2017, 19:21

Re: Trying to get this auto grinding script to work.  Topic is solved

21 Sep 2017, 08:08

I see, in any case I got the script working last night so thank you for putting me on the right track, I would probably still be trying to make the original plan work to no avail otherwise.

Now if anyone has the same issue and comes across this post through a Google search here's what worked for me:

Code: Select all

													; Variable setup: Useful if switching between a 4K TV and a 1080P laptop

Blue := 0x800000									; Sets the exact color needed in a BGR format to the "Blue" variable. 

Loop, 99											; Maximum amount of item that can be held at a time
{
	Loop
	{
		Send, {Down down}
		Sleep, 1
		Send, {Down up}
		Send, {Up down}
		Sleep, 1
		Send, {Up up}								; Moves character on world map repeatedly
		MouseGetPos, MouseX, MouseY					; Gets mouse information for next step
		PixelGetColor, color, %MouseX%, %MouseY%	; Grabs the color of whatever pixel the mouse is on
		tooltip Color is [%color%]					; Displays color information, comment out if bothered by it
		If color = %Blue%							
		{
			Break									; Ends the loop when the color for the variable is found and moves on to the next loop
		}
	}
	Loop
	{
		Send, {Right down}
		Send, {X down}
		Sleep, 1
		Send, {X up}
		Send, {Right up}							; Selects the right action for the battle user is in
		MouseGetPos, MouseX, MouseY					; Gets mouse information for next step
		PixelGetColor, color, %MouseX%, %MouseY%	; Grabs the color of whatever pixel the mouse is on
		tooltip Color is [%color%]					; Displays color information, comment out if bothered by it
		If color <> %Blue%
		{
			Break									; Ends the loop when the color for the variable isn't found
		}
	}												; 1 full loop is complete, subtracts one from the original loop and starts all over
}
SoundPlay, E:\Sounds\Alarm.wav						; Lets the user know to sell off all their items before getting back into place and reactivating
and with absolute variables:

Code: Select all

											; Absolute setup: Use if needed pixel will never change

Blue := 0x800000							; Sets the exact color needed in a BGR format to the "Blue" variable. 

Loop, 99									; Maximum amount of item that can be held at a time
{
	Loop
	{
		Send, {Down down}
		Sleep, 1
		Send, {Down up}
		Send, {Up down}
		Sleep, 1
		Send, {Up up}						; Moves character on world map repeatedly
		PixelGetColor, color, 169, 1621		; Grabs the color of whatever pixel is at your specified location in a X, Y format
		tooltip Color is [%color%]			; Displays color information, comment out if bothered by it
		If color = %Blue%
		{
			Break							; Ends the loop when the color for the variable is found and moves on to the next loop
		}
	}
	Loop
	{
		Send, {Right down}
		Send, {X down}
		Sleep, 1
		Send, {X up}
		Send, {Right up}					; Selects the right action for the battle user is in
		PixelGetColor, color, 169, 1621		; Grabs the color of whatever pixel is at your specified location in a X, Y format
		tooltip Color is [%color%]			; Displays color information, comment out if bothered by it
		If color <> %Blue%
		{
			Break							; Ends the loop when the color for the variable isn't found
		}
	}										; 1 full loop is complete, subtracts one from the original loop and starts all over
}
SoundPlay, E:\Sounds\Alarm.wav				; Lets the user know to sell off all their items before getting back into place and reactivating

Return to “Gaming Help (v1)”

Who is online

Users browsing this forum: prototype_zero and 36 guests