Exit a running macro ? Topic is solved

Ask gaming related questions
LePetitMage
Posts: 4
Joined: 24 Jul 2020, 07:59

Exit a running macro ?

24 Jul 2020, 08:05

Hi,
I want to stop a macro after using control + l (little L). I tried several things but it looks like it doesn't work.
Here is the macro.

Code: Select all

^m::
#IfWinActive A, Discord
onoff := 0
macro:
Loop, 1000
{
	Loop, 40
	{
		Loop, 25
        	{
                        if (onoff = 1)
			{
				Break macro
			}
			else
			{
                        Send, {;}p{Enter}
        		Sleep, 1500
        		PixelGetColor, cap, 531, 865, RGB
        		PixelGetColor, couleur, 588, 865, RGB
        		Loop {
        			PixelGetColor, cap, 531, 865, RGB
        		} Until (captcha = "0x36393F" or captcha = "0x403E3D" or couleur = "0x403E3D")
        		if (cap = "0x403E3D") 
        		{
       				SoundPlay *-1
        			MsgBox, I think there is a cap !
        		}
			else
			{
        			PixelGetColor, couleur, 589, 865, RGB
        			if (onoff = 1)
                                {
                                        Break macro
                                }
                                else if (couleur = "0x0855FB")
        			{
            				Send, pb{Enter} ;The ball to send at common Pokémon.
        			}
				else if (couleur = "0x13B4E7")
				{
					Send, pb{Enter} ;The ball to send at uncommon Pokémon.
            			}
				else if (couleur = "0xFB8908")
                		{
                    			Send, gb{Enter} ;The ball to send at rare Pokémon.
                		}
				else if (couleur = "0xF8F407")
                    		{
                        		Send, ub{Enter} ;The ball to send at super rare Pokémon.
                    		}
				else if (couleur = "0xA007F8")
                        	{
                            		Send, mb{Enter} ;The ball to send at legendary Pokémon.
                            		Sleep, 10000
                            		Send, {;}s b 4{Enter} ;It buys a master ball from the shop.
                        	}
				else if (couleur = "0xFF99CC")
                       		{
                                	Send, mb{Enter} ;The ball to send at shiny Pokémon.
                                	Sleep, 10000
                     			Send, {;}s b 4{Enter}
                    		}
				else
				{
                   			SoundPlay *-1
             				MsgBox, Unexpected color !
                     			Sleep, 2000 ;I let you 2 seconds to decide what you’ll do.
                        	}
			}
        		Random, rand, 0, 500
        		Sleep, 11000 + rand
			}
       		}
        	;Send, {;}s b 2 1{Enter}
        	;Sleep, 10000
    	}
   	Send, {;}q{Enter}
    	Sleep, 10000
}
return

^l::
onoff := 1
return
I don't know what I'm doing wrong. Can you help me ?
Also, it would be nice if you help me optimize it. It's my first macro.
Last edited by BoBo on 24 Jul 2020, 08:56, edited 1 time in total.
Reason: Moved to Gaming section.
User avatar
boiler
Posts: 5933
Joined: 21 Dec 2014, 02:44

Re: Exit a running macro ?

24 Jul 2020, 09:14

Hi. It seems like your approach to breaking the loop with a label would work if it got to the point where you checked the onoff variable, but it looks like it would be stuck in an infinite loop here:

Code: Select all

Loop {
	PixelGetColor, cap, 531, 865, RGB
} Until (captcha = "0x36393F" or captcha = "0x403E3D" or couleur = "0x403E3D")
Nothing inside that loop changes the value of captcha or couleur, so it would never be able to exit that loop. I suppose the variable in that PixelGetColor statement was meant to be captcha instead of cap. With it stuck in that infinite loop, the value of onoff never gets evaluated.

Other than that correction, I don't have any suggestions for optimization at the moment. However, I found your indentation approach to be inconsistent and hard for me to follow, so I indented it the way I would. I also removed braces where they are not necessary because the code block is a single line to help improve readability, in my opinion. Also, a hotkey with only a single command can be on the same line without a return, so I showed that for the ^l:: hotkey.

Code: Select all

^m::
	#IfWinActive A, Discord
	onoff := 0
	macro:
	Loop, 1000
	{
		Loop, 40
		{
			Loop, 25
			{
				if (onoff = 1)
					Break macro
				else
				{
					Send, {;}p{Enter}
					Sleep, 1500
					PixelGetColor, cap, 531, 865, RGB
					PixelGetColor, couleur, 588, 865, RGB
					Loop {
						PixelGetColor, captcha, 531, 865, RGB
					} Until (captcha = "0x36393F" or captcha = "0x403E3D" or couleur = "0x403E3D")
					if (cap = "0x403E3D") 
					{
						SoundPlay *-1
						MsgBox, I think there is a cap !
					}
					else
					{
						PixelGetColor, couleur, 589, 865, RGB
						if (onoff = 1)
							Break macro
						else if (couleur = "0x0855FB")
								Send, pb{Enter} ;The ball to send at common Pokémon.
						else if (couleur = "0x13B4E7")
							Send, pb{Enter} ;The ball to send at uncommon Pokémon.
						else if (couleur = "0xFB8908")
							Send, gb{Enter} ;The ball to send at rare Pokémon.
						else if (couleur = "0xF8F407")
							Send, ub{Enter} ;The ball to send at super rare Pokémon.
						else if (couleur = "0xA007F8")
						{
							Send, mb{Enter} ;The ball to send at legendary Pokémon.
							Sleep, 10000
							Send, {;}s b 4{Enter} ;It buys a master ball from the shop.
						}
						else if (couleur = "0xFF99CC")
						{
							Send, mb{Enter} ;The ball to send at shiny Pokémon.
							Sleep, 10000
							Send, {;}s b 4{Enter}
						}
						else
						{
							SoundPlay *-1
							MsgBox, Unexpected color !
							Sleep, 2000 ;I let you 2 seconds to decide what you’ll do.
						}
					}
					Random, rand, 0, 500
					Sleep, 11000 + rand
				}
			}
			;Send, {;}s b 2 1{Enter}
			;Sleep, 10000
		}
		Send, {;}q{Enter}
		Sleep, 10000
	}
return

^l::onoff := 1
User avatar
Yakshongas
Posts: 587
Joined: 21 Jan 2020, 08:41
Facebook: Yakshongas

Re: Exit a running macro ?

24 Jul 2020, 09:26

Maybe like this?

Code: Select all

#IfWinActive, A, Discord
    ^m::
    While, (OnOff = 0)
    {
        Send, `;p{Enter}
        Sleep, 1500
        PixelGetColor, couleur, 588, 865, RGB
        Loop
        {
            PixelGetColor, Cap, 531, 865, RGB
        } Until (Captcha = "0x36393F" or Captcha = "0x403E3D" or couleur ="0x403E3D")
        If (Cap = "0x403E3D")
        {
            SoundPlay, *-1
            MsgBox, I Think there is a Cap ! 
        }
        Else
        {
            If (OnOff = 1)
            {
                Break
            }
            PixelGetColor, Couleur, 589, 865, RGB
            Switch Couleur
            {
                Case "0x0855FB": Send, pb{Enter}
                Case "0x13B4E7": Send, pb{Enter}
                Case "0xFB8908": Send, gb{Enter}
                Case "0xF8F407": Send, ub{Enter}
                Case "0xA007F8": 
                    Send, mb{Enter}
                    Sleep, 10000
                    Send, `;s b 4{Enter}
                    Return
                Case "0xFF99CC": 
                    Send, mb{Enter}
                    Sleep, 10000
                    Send, `;s b 4{Enter}
                    Return
                Default:
                    SoundPlay *-1
                    MsgBox, Unexpected Color !
                    Sleep, 2000
                    Random, Rnd, 0, 500
                    Sleep, 1100 + Rnd
                    Send, `;q{Enter}
                    Sleep, 10000
                    Return
            }
        }
    }
    Return

    ^l::OnOff := 1

#IfWinActive
Please mark your topics as solved if you don't need any further help. ✅

Need a little more help? Discord : Yakshongas#9893 🕹
LePetitMage
Posts: 4
Joined: 24 Jul 2020, 07:59

Re: Exit a running macro ?

24 Jul 2020, 23:48

boiler wrote:
24 Jul 2020, 09:14
Hi. It seems like your approach to breaking the loop with a label would work if it got to the point where you checked the onoff variable, but it looks like it would be stuck in an infinite loop here:

Code: Select all

Loop {
	PixelGetColor, cap, 531, 865, RGB
} Until (captcha = "0x36393F" or captcha = "0x403E3D" or couleur = "0x403E3D")
Nothing inside that loop changes the value of captcha or couleur, so it would never be able to exit that loop. I suppose the variable in that PixelGetColor statement was meant to be captcha instead of cap. With it stuck in that infinite loop, the value of onoff never gets evaluated.
Oups, I gave a version where I didn't replaced all the "cap" by captcha. So every time there is written "cap", I meant "captcha".
boiler wrote:
24 Jul 2020, 09:14
However, I found your indentation approach to be inconsistent and hard for me to follow, so I indented it the way I would. I also removed braces where they are not necessary because the code block is a single line to help improve readability, in my opinion.
I know, but my keyboard does big indentations and I don't know how to fix it. :D

Thanks for answering !
LePetitMage
Posts: 4
Joined: 24 Jul 2020, 07:59

Re: Exit a running macro ?

25 Jul 2020, 03:24

Yakshongas wrote:
24 Jul 2020, 09:26
Maybe like this?

Code: Select all

#IfWinActive, A, Discord
    ^m::
    While, (OnOff = 0)
    {
        Send, `;p{Enter}
        Sleep, 1500
        PixelGetColor, couleur, 588, 865, RGB
        Loop
        {
            PixelGetColor, Cap, 531, 865, RGB
        } Until (Captcha = "0x36393F" or Captcha = "0x403E3D" or couleur ="0x403E3D")
        If (Cap = "0x403E3D")
        {
            SoundPlay, *-1
            MsgBox, I Think there is a Cap ! 
        }
        Else
        {
            If (OnOff = 1)
            {
                Break
            }
            PixelGetColor, Couleur, 589, 865, RGB
            Switch Couleur
            {
                Case "0x0855FB": Send, pb{Enter}
                Case "0x13B4E7": Send, pb{Enter}
                Case "0xFB8908": Send, gb{Enter}
                Case "0xF8F407": Send, ub{Enter}
                Case "0xA007F8": 
                    Send, mb{Enter}
                    Sleep, 10000
                    Send, `;s b 4{Enter}
                    Return
                Case "0xFF99CC": 
                    Send, mb{Enter}
                    Sleep, 10000
                    Send, `;s b 4{Enter}
                    Return
                Default:
                    SoundPlay *-1
                    MsgBox, Unexpected Color !
                    Sleep, 2000
                    Random, Rnd, 0, 500
                    Sleep, 1100 + Rnd
                    Send, `;q{Enter}
                    Sleep, 10000
                    Return
            }
        }
    }
    Return

    ^l::OnOff := 1

#IfWinActive
You gave me an idea by using the While loop and I didn't know I could use switch in AutoHotkey.
But your macro doesn't do exactly what I need it to.
After throwing 25 Pokéballs, I need it to do ;s b 2 1 but only sometimes that's why I keep it as commentary and remove the ; when I need.
After throwing 40 times 25 Pokéballs, I want it to do ;q.
Then I put the entire macro in a While loop like that, even if my friend says it doesn't stop after doing control + l :

Code: Select all

^m::
#IfWinActive A, Discord
onoff := 0
macro:
While, (onoff = 0)
{
        compteuru := 0
        While, (onoff = 0 and compteuru < 40)
        {
                compteur := 0
                While, (onoff = 0 and compteur < 25)
                {
                        if (onoff = 1)
                        {
                        Break macro
                        }
                        else
                        {
                        Send, {;}p{Enter}
                        Sleep, 1500
                        PixelGetColor, captcha, 531, 865, RGB
                        PixelGetColor, couleur, 588, 865, RGB
                        Loop {
                        PixelGetColor, captcha, 531, 865, RGB
                        } Until (captcha = "0x36393F" or captcha = "0x403E3D" or couleur = "0x403E3D")
                        if (captcha = "0x403E3D") 
                        {
                                SoundPlay *-1
                                MsgBox, I think there is a captcha !
                        }
                        else
                        {
                                PixelGetColor, couleur, 589, 865, RGB
                                if (onoff = 1)
                                {
                                        Break macro
                                }
                                Switch couleur
                                {
                                        Case "0x0855FB": Send, pb{Enter} ;The ball to send at common Pokémon.
                                        Case "0x13B4E7": Send, pb{Enter} ;The ball to send at uncommon Pokémon.
                                        Case "0xFB8908": Send, gb{Enter} ;The ball to send at rare Pokémon.
                                        Case "0xF8F407": Send, ub{Enter} ;The ball to send at super rare Pokémon.
                                        Case "0xA007F8": 
                                                Send, mb{Enter} ;The ball to send at legendary Pokémon.
                                                Sleep, 4000
                                                Send, {;}s b 4{Enter}
                                        Case "0xFF99CC":
                                                Send, mb{Enter} ;The ball to send at shiny Pokémon.
                                                Sleep, 4000
                                                end, {;}s b 4{Enter}
                                        Default:
                                                SoundPlay *-1
                                                MsgBox, Unexpected color !
                                                Sleep, 2000 ;I let you 2 seconds to decide what you’ll do.
                                }
                        }
                        Random, rand, 0, 500
                        Sleep, 11000 + rand
                        }
                        compteur += 1
                }
                ;Send, {;}s b 2 1{Enter}
                ;Sleep, 3000
                compteuru += 1
        }
        Send, {;}q{Enter}
        Sleep, 3000
}
return

^l::onoff := 1
And why did you put a #IfWinActive at the end ? :?:
User avatar
boiler
Posts: 5933
Joined: 21 Dec 2014, 02:44

Re: Exit a running macro ?

25 Jul 2020, 04:25

You keep putting #IfWinActive A, Discord underneath your first hotkey. That means it does not apply to that hotkey, making it only affect ^l. Directives like that apply to all hotkeys that follow it until another #If directive appears. They do not affect the code below the hotkey line like it looks like you are expecting it to. Yakshongas showed it correctly where he put it at the top. The one at the bottom makes it so any hotkeys you might add below that are not subject to the top one (i.e., it turns it off for any that follow since it’s blank).

Also, why do you have parameters A for the WinTitle and Discord for the WinText? That seems like it’s probably not correct, and that would explain why your ^l hotkey isn’t working. If it’s really not matching your window, then that hotkey is not active, and it’s not affecting your ^m hotkey because you didn’t have the #IfWinActive A, Discord above it like it should be. What is the title of your window? Is there really WinText that includes Discord (as shown by the Window Spy tool)?
LePetitMage
Posts: 4
Joined: 24 Jul 2020, 07:59

Re: Exit a running macro ?  Topic is solved

25 Jul 2020, 23:57

boiler wrote:
25 Jul 2020, 04:25
You keep putting #IfWinActive A, Discord underneath your first hotkey. That means it does not apply to that hotkey, making it only affect ^l. Directives like that apply to all hotkeys that follow it until another #If directive appears. They do not affect the code below the hotkey line like it looks like you are expecting it to. Yakshongas showed it correctly where he put it at the top. The one at the bottom makes it so any hotkeys you might add below that are not subject to the top one (i.e., it turns it off for any that follow since it’s blank).

Also, why do you have parameters A for the WinTitle and Discord for the WinText? That seems like it’s probably not correct, and that would explain why your ^l hotkey isn’t working. If it’s really not matching your window, then that hotkey is not active, and it’s not affecting your ^m hotkey because you didn’t have the #IfWinActive A, Discord above it like it should be. What is the title of your window? Is there really WinText that includes Discord (as shown by the Window Spy tool)?
I understand now. :facepalm:
Since I can stop the macro, no need for it to be only active for one window.

I put the parameter A because it was shown in an example in the documentation. :shh:

Now I removed it and it works !
Thanks all for your answers.

Code: Select all

^m::
onoff := 0
macro:
While, (onoff = 0)
{
	compteuru := 0
	While, (onoff = 0 and compteuru < 40)
	{
		compteur := 0
		While, (onoff = 0 and compteur < 25)
        	{
                        if (onoff = 1)
			{
				Break macro
			}
			else
			{
                        Send, {;}p{Enter}
        		Sleep, 1500
        		PixelGetColor, captcha, 531, 865, RGB
        		PixelGetColor, couleur, 588, 865, RGB
        		Loop {
        			PixelGetColor, captcha, 531, 865, RGB
        		} Until (captcha = "0x36393F" or captcha = "0x403E3D" or couleur = "0x403E3D")
        		if (captcha = "0x403E3D") 
        		{
       				SoundPlay *-1
        			MsgBox, I think there is a captcha !
        		}
			else
			{
        			PixelGetColor, couleur, 589, 865, RGB
        			if (onoff = 1)
                                {
                                        Break macro
                                }
				Switch couleur
				{
					Case "0x0855FB": Send, pb{Enter} ;The ball to send at common Pokémon.
					Case "0x13B4E7": Send, pb{Enter} ;The ball to send at uncommon Pokémon.
            				Case "0xFB8908": Send, gb{Enter} ;The ball to send at rare Pokémon.
					Case "0xF8F407": Send, ub{Enter} ;The ball to send at super rare Pokémon.
					Case "0xA007F8": 
						Send, mb{Enter} ;The ball to send at legendary Pokémon.
                            			Sleep, 4000
                     				Send, {;}s b 4{Enter}
					Case "0xFF99CC":
						Send, mb{Enter} ;The ball to send at shiny Pokémon.
                                		Sleep, 4000
                     				Send, {;}s b 4{Enter}
					Default:
						SoundPlay *-1
             					MsgBox, Unexpected color !
                     				Sleep, 2000 ;I let you 2 seconds to decide what you’ll do.
                        	}
			}
        		Random, rand, 0, 500
        		Sleep, 11000 + rand
			}
			compteur += 1
       		}
        	;Send, {;}s b 2 1{Enter}
        	;Sleep, 3000
		compteuru += 1
    	}
   	Send, {;}q{Enter}
    	Sleep, 3000
}
MsgBox, You used your sacred power to stop the macro.
return

^l::onoff := 1

Return to “Gaming”

Who is online

Users browsing this forum: boiler, zaric1 and 30 guests