Toggle Loop inerception after !toggle and hotkey hey not working after hotkey started Topic is solved

Ask gaming related questions (AHK v1.1 and older)
noo0b

Toggle Loop inerception after !toggle and hotkey hey not working after hotkey started

15 Jan 2018, 01:22

Hi,
I am having trouble to stop a Toggle loop subroutine after the hotkey is set to !toggle.
The !toggle subroutine would intercept the toggle loop in the middle of its execution.
It would swap toggle states because there is such if¨statement inside the loop.
the only way I found to fix it was to insert ¨if¨ breaks before each line of the loop, but I´m certain there is another way to do it, if someone could help me please.
I recoded it without the toggles and its more elegant, but I still would like to get the toggle version working because I have another hotkey I use that has toggles and loops, funny is that I didn´t have any problems using that one.
Also my both these hotkeys block ¨t¨ after the hotkey gets started and I have to close the script to get ¨t¨ working again. It was working fine before :oops:
If I couls also get an opinion if I´m using too many returns it would be appreciated.

here´s my code:

Code: Select all

{
#IfWinActive Path of Exile
SetBatchLines, 20ms
SetDefaultMouseSpeed, 1
SetKeyDelay, 50, 50
SetMouseDelay, 50
CoordMode, Mouse
SetWorkingDir c:\Users\FX-8120\My Documents\My Games\Path of Exile\Screenshots
#MaxThreadsperHotkey 2
#MaxThreads 2
	{
	t & XBUTTON2::
	ttoggle := !ttoggle
	BlockInput MouseMove
	MouseGetPos, Xpos, Ypos
	GetKeyState, LButtonstate, LButton
		if LButtonstate = D
			Mouseclick, left, , , , , U
		If tToggle
			{
			MouseMove, 1660, 1025 ; t
			MouseClick, left, 1660, 1025 ; t
			ImageSearch, FrostWallX, FrostWallY, 1412, 480, 1604, 933, *10 FrostWallL3.png
				if errorlevel = 1
					ImageSearch, FrostWallX, FrostWallY, 1412, 480, 1604, 933, *10 FrostWallR4.png
						if errorlevel = 0
							{
							MouseMove, FrostWallX, FrostWallY
							MouseClick, left,FrostWallX, FrostWallY
							MouseMove, Xpos, Ypos
							Send, {t}
								if LButtonstate = D
									Mouseclick, left, , , , , D
							BlockInput MouseMoveOff
							Loop
							{
								If not tToggle
									Break
							Sleep, 2000
								If not tToggle
									Break
							ImageSearch, , , 1400, 1015, 1680, 1075, *10 FrostWall-T.png
								If ErrorLevel = 1
									{
										If not tToggle
											Break
									ttoggle := !ttoggle
										If not tToggle
											Break
									Exit
									}
							}
							}
						if ErrorLevel = 1
							{
							MsgBox, 2, FrostWall, FrostWall could not be found on the screen., 0.2
							MouseMove, Xpos, Ypos
								if LButtonstate = D
									Mouseclick, left, , , , , D
							BlockInput MouseMoveOff
							}
			Return
			}
		If not tToggle
			{
			MouseMove, 1660, 1025 ; t
			MouseClick, left, 1660, 1025 ; t
			ImageSearch, FeetX, FeetY, 1412, 480, 1604, 933, *100 FeetR2.png
				if ErrorLevel = 1
					ImageSearch, FeetX, FeetY, 1412, 480, 1604, 933, *100 FeetL1.png
						if ErrorLevel = 1
							ImageSearch, FeetX, FeetY, 1412, 480, 1604, 933, *100 FeetM1.png
								if ErrorLevel = 0
									{
									MouseMove, FeetX, FeetY
									MouseClick, left, FeetX, FeetY
									}
								if ErrorLevel = 1
									MsgBox, 2, Feet, Feet could not be found on the screen., 0.2
			MouseMove, Xpos, Ypos
				if LButtonstate = D
					Mouseclick, left, , , , , D
			BlockInput MouseMoveOff
			Return
			}
	Return
	}
	{
	XBUTTON1 & t::
	BlockInput MouseMove
	MouseGetPos, Xpos, Ypos
	GetKeyState, LButtonstate, LButton
		if LButtonstate = D
			Mouseclick, left, , , , , U
	ImageSearch, , , 1400, 1015, 1680, 1075, *10 FrostWall-T.png
		If ErrorLevel = 1
			{
			MouseClick, left, 1660, 1025 ; t
			ImageSearch, FrostWallX, FrostWallY, 1412, 480, 1604, 933, *10 FrostWallL3.png
				if errorlevel = 1
					ImageSearch, FrostWallX, FrostWallY, 1412, 480, 1604, 933, *10 FrostWallR4.png
						if errorlevel = 0
							{
							MouseClick, left,FrostWallX, FrostWallY
							MouseMove, Xpos, Ypos
							Send, {t}
							}
						if ErrorLevel = 1
							{
							MsgBox, 2, FrostWall, FrostWall could not be found on the menu., 0.2
							MouseMove, Xpos, Ypos
							}
				if LButtonstate = D
					Mouseclick, left, , , , , D
			BlockInput MouseMoveOff
			Return
			}
		If ErrorLevel = 0
			{
			MouseClick, left, 1660, 1025 ; t
			ImageSearch, FeetX, FeetY, 1412, 480, 1604, 933, *100 FeetR2.png
				if ErrorLevel = 1
							ImageSearch, FeetX, FeetY, 1412, 480, 1604, 933, *100 FeetL1.png
								if ErrorLevel = 1
									ImageSearch, FeetX, FeetY, 1412, 480, 1604, 933, *100 FeetM1.png
										if ErrorLevel = 0
											{
											MouseClick, left, FeetX, FeetY
											}
										if ErrorLevel = 1
											MsgBox, 2, Feet, Feet could not be found on the screen., 0.2
			MouseMove, Xpos, Ypos
				if LButtonstate = D
					Mouseclick, left, , , , , D
			BlockInput MouseMoveOff
			Return
			}
	}
Return
}
¨t¨ was working fine before, I´m not sure what I did wrong?
Please help, I´m hopeless :bravo: :bravo:
Thanks
User avatar
evilC
Posts: 4823
Joined: 27 Feb 2014, 12:30

Re: Toggle Loop inerception after !toggle and hotkey hey not working after hotkey started  Topic is solved

15 Jan 2018, 07:15

No, this code could never have worked with the loops set up like this.
You ABSOLUTELY CANNOT have an endless loop in a hotkey. When it is interrupted by another hotkey, it does not resume until the other hotkey ends.

See here: https://autohotkey.com/boards/viewtopic.php?t=19745
This code in your script is functionally identical to a GetKeyState loop:

Code: Select all

	Loop
	{
		If not tToggle
			Break
By putting this code in your hotkey, the hotkey never ends processing, and so when the next hotkey fires, it interrupts the first hotkey.
Seeing as the SECOND hotkey also has an infinite loop in it, the first hotkey never resumes, because the second hotkey never finishes.

TLDR: You need to use SetTimer instead of loops
noo0b

Re: Toggle Loop inerception after !toggle and hotkey hey not working after hotkey started

18 Jan 2018, 21:08

Hi,
Thank you for the reply.
I got it working like this, look:

Code: Select all


	{
	XBUTTON1 & 1::
	1toggle := !1toggle
	MouseGetPos, Xpos, Ypos
	GetKeyState, LButtonstate, LButton
		if LButtonstate = D
			Mouseclick, left, , , , , U
		If 1Toggle
			{
			BlockInput, MouseMove
			MouseClick, left, 1660, 1025 ; t
			BlockInput, MouseMoveOff
			ImageSearch, MoltenShellX, MoltenShellY, 1412, 480, 1604, 933, *10 MoltenShellR2.png
				if errorlevel = 1
					ImageSearch, MoltenShellX, MoltenShellY, 1412, 480, 1604, 933, *10 MoltenShellR5.png
						if errorlevel = 0
							{
							BlockInput, MouseMove
							MouseClick, left, MoltenShellX, MoltenShellY
							MouseMove, Xpos, Ypos
								if LButtonstate = D
									Mouseclick, left, , , , , D
							BlockInput, MouseMoveOff
							Loop
							{
								If not 1Toggle
									Break
							ImageSearch, , , 0, 0, 1000, 80, *10 MoltenShellTimer.png
								If ErrorLevel = 1
									ImageSearch, , , 0, 0, 1000, 80, *10 MoltenShellTimer3.png
										If ErrorLevel = 0
											{
											ImageSearch, , , 1400, 1015, 1680, 1075, *10 MoltenShell-T.png
												If ErrorLevel = 0
													break
												If ErrorLevel = 1
													{
													1toggle := !1toggle
													Break
													}
											}
										If ErrorLevel = 1
											{
											ImageSearch, , , 1400, 1015, 1680, 1075, *10 MoltenShell-T.png
												If ErrorLevel = 0
													{
													Send, {t}
													}
												If ErrorLevel = 1
													{
													1toggle := !1toggle
													Break
													}
											}
							Sleep, 1000
							}
							}
			Return
			}
		If not 1Toggle
			{
			BlockInput, MouseMove
			MouseClick, left, 1660, 1025 ; t
			BlockInput, MouseMoveOff
			loop
			{
			ImageSearch, FeetX, FeetY, 1412, 480, 1604, 933, *10 FeetR2.png
				if ErrorLevel = 1
					ImageSearch, FeetX, FeetY, 1412, 480, 1604, 933, *10 FeetL1.png
						if ErrorLevel = 1
							ImageSearch, FeetX, FeetY, 1412, 480, 1604, 933, *10 FeetM1.png
								if ErrorLevel = 0
									{
									BlockInput, MouseMove
									MouseMove, FeetX, FeetY
									MouseClick, left, FeetX, FeetY
									MouseMove, Xpos, Ypos
										if LButtonstate = D
											Mouseclick, left, , , , , D
									BlockInput, MouseMoveOff
									break
									}
			}
			Return
			}
	Return	}
	
It works well and doesn´t get the toggle states jumbled or crashes even if I mash the hotkey repeatedly. It only gets a delay after a few quick presses, but continue functional. The only problem with it is that it may miss an ImageSearch due to latency, but it still works and doesn´t crash.
Toggle is supposed to repeat every 1000ms, and either send, shift toggle states and break, or do nothing
!toggle finishes once the ImageSearch ErrorLevel is 0.
I tried adding nested loops with

Code: Select all


		If not tToggle
			Break
to the beginning of the nested Toggle scan loops to avoid missing ImageSearch scans, I got it working, but it would crash after a few quick presses when the !toggle would intercept the loop between the nested ImageSearch array. I´m not sure why it would be okay and not behave this way on the above example, maybe because of the retundant ImageSearch toggle.
I suppose the only way to get this nested scan loop script working is to add

Code: Select all


		If not tToggle
			Break
between every line of every loop, It doesn´t look good, and I deleted that attempt. I´m going to get back to it though.
The other one is working just fine, although without Toggle. I also managed to get it to work with Toggle.

Code: Select all


	{
	XBUTTON1 & t::
	MouseGetPos, Xpos, Ypos
	GetKeyState, LButtonstate, LButton
		if LButtonstate = D
			Mouseclick, left, , , , , U
	ImageSearch, , , 1400, 1015, 1680, 1075, *10 FrostWall-T3.png
		If ErrorLevel = 1
			ImageSearch, , , 1400, 1015, 1680, 1075, *10 FrostWall-T2.png
				If ErrorLevel = 1
					ImageSearch, , , 1400, 1015, 1680, 1075, *10 FrostWall-T1.png
						If ErrorLevel = 1
							ImageSearch, , , 1400, 1015, 1680, 1075, *10 FrostWall-T0.png
								If ErrorLevel = 1
									ImageSearch, , , 1400, 1015, 1680, 1075, *2 FrostWall-NoMana.png
										if ErrorLevel = 0
											{
											BlockInput, MouseMove
											MouseClick, left, 1660, 1025 ; t
											BlockInput, MouseMoveOff
											loop
											{
											ImageSearch, FeetX, FeetY, 1412, 480, 1604, 933, *10 FeetR2.png
												if ErrorLevel = 1
													ImageSearch, FeetX, FeetY, 1412, 480, 1604, 933, *10 FeetL1.png
														if ErrorLevel = 1
															ImageSearch, FeetX, FeetY, 1412, 480, 1604, 933, *10 FeetM1.png
																if ErrorLevel = 0
																	{
																	MouseClick, left, FeetX, FeetY
																	Break
																	}
											}
											MouseMove, Xpos, Ypos
											}
										If ErrorLevel = 1
											{
											BlockInput, MouseMove
											MouseClick, left, 1660, 1025 ; t
											BlockInput, MouseMoveOff
											loop
											{
											ImageSearch, FrostWallX, FrostWallY, 1412, 480, 1604, 933, *10 FrostWallL3.png
												if errorlevel = 1
													ImageSearch, FrostWallX, FrostWallY, 1412, 480, 1604, 933, *10 FrostWallR4.png
														if errorlevel = 0
															{
															BlockInput, MouseMove
															MouseClick, left,FrostWallX, FrostWallY
															MouseMove, Xpos, Ypos
															Send, {t}
															BlockInput, MouseMoveOff
															break
															}
											}
											}
		if LButtonstate = D
			Mouseclick, left, , , , , D
	Return
	}
	
It doesn´t need Toggle because there is no infinite loop to undo and auto toggle. It toggles on ImageSearch results. I might be able to quickly rewrite this last one with toggle and get it tested. I will post both scripts here once finished.
Thanks!
Noo0b

Re: Toggle Loop inerception after !toggle and hotkey hey not working after hotkey started

18 Jan 2018, 21:51

Here´s the toggle version of that last hotkey. It works perfect and I can mash the hotkey and it just gets delayed and doesn´t crash or gets stuck.

Code: Select all

	{
	XBUTTON1 & t::
	1toggle := !1toggle
	MouseGetPos, Xpos, Ypos
	GetKeyState, LButtonstate, LButton
		if LButtonstate = D
			Mouseclick, left, , , , , U
		If 1toggle
			{
			ImageSearch, , , 1400, 1015, 1680, 1075, *10 FrostWall-T3.png
				If ErrorLevel = 1
					ImageSearch, , , 1400, 1015, 1680, 1075, *10 FrostWall-T2.png
						If ErrorLevel = 1
							ImageSearch, , , 1400, 1015, 1680, 1075, *10 FrostWall-T1.png
								If ErrorLevel = 1
									ImageSearch, , , 1400, 1015, 1680, 1075, *10 FrostWall-T0.png
										If ErrorLevel = 1
											ImageSearch, , , 1400, 1015, 1680, 1075, *2 FrostWall-NoMana.png
												If ErrorLevel = 1
													{
													BlockInput, MouseMove
													MouseClick, left, 1660, 1025 ; t
													BlockInput, MouseMoveOff
													loop
													{
													ImageSearch, FrostWallX, FrostWallY, 1412, 480, 1604, 933, *10 FrostWallL3.png
														if errorlevel = 1
															ImageSearch, FrostWallX, FrostWallY, 1412, 480, 1604, 933, *10 FrostWallR4.png
																if errorlevel = 0
																	{
																	BlockInput, MouseMove
																	MouseClick, left,FrostWallX, FrostWallY
																	MouseMove, Xpos, Ypos
																	Send, {t}
																	BlockInput, MouseMoveOff
																	break
																	}
													}
													}
			}
		If !1toggle
			{
			ImageSearch, , , 1400, 1015, 1680, 1075, *10 FrostWall-T3.png
				If ErrorLevel = 1
					ImageSearch, , , 1400, 1015, 1680, 1075, *10 FrostWall-T2.png
						If ErrorLevel = 1
							ImageSearch, , , 1400, 1015, 1680, 1075, *10 FrostWall-T1.png
								If ErrorLevel = 1
									ImageSearch, , , 1400, 1015, 1680, 1075, *10 FrostWall-T0.png
										If ErrorLevel = 1
											ImageSearch, , , 1400, 1015, 1680, 1075, *2 FrostWall-NoMana.png
												if ErrorLevel = 0
													{
													BlockInput, MouseMove
													MouseClick, left, 1660, 1025 ; t
													BlockInput, MouseMoveOff
													loop
													{
													ImageSearch, FeetX, FeetY, 1412, 480, 1604, 933, *10 FeetR2.png
														if ErrorLevel = 1
															ImageSearch, FeetX, FeetY, 1412, 480, 1604, 933, *10 FeetL1.png
																if ErrorLevel = 1
																	ImageSearch, FeetX, FeetY, 1412, 480, 1604, 933, *10 FeetM1.png
																		if ErrorLevel = 0
																			{
																			MouseClick, left, FeetX, FeetY
																			Break
																			}
													}
													MouseMove, Xpos, Ypos
													}
			}
		if LButtonstate = D
			Mouseclick, left, , , , , D
	Return
	}
I tried to do the same method with the first hotkey.
I added nested loops and Ifs and its still the same, if I press the hotkey repeatedly fast the !toggle intercepts the Toggle loop and it gets stuck.

Return to “Gaming Help (v1)”

Who is online

Users browsing this forum: No registered users and 80 guests