Toggle script with nested loops

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Starbuck1991
Posts: 12
Joined: 12 Apr 2020, 07:26

Toggle script with nested loops

18 Apr 2021, 06:50

Hello everyone!

I've been struggling for a while trying to toggle a script with a series of nested loops. I've tried several methods, but somehow I still can't get this thing working ("send a" and "send b" are just placeholders for code).

Any tips will be very welcome!

Code: Select all

F2::
loop {

StartA := A_TickCount
loop {
TimeA := A_TickCount - StartA
if TimeA < 1000
send a
else
break
}

StartB := A_TickCount
loop {
TimeB := A_TickCount - StartB
If TimeB < 1000 
send b
else 
break
}

}

return
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: Toggle script with nested loops

18 Apr 2021, 07:10

can't get this thing working
is very nondescript. for all i care, its working just fine - once F2 is pressed, proceeds to spam(execute code) as for a second, then spams(executes code) bs for a second and back again, indefinitely
User avatar
boiler
Posts: 16957
Joined: 21 Dec 2014, 02:44

Re: Toggle script with nested loops

18 Apr 2021, 07:15

I think the issue is getting F2 to start and stop it (toggle it). Try this:

Code: Select all

Pause
loop {

	StartA := A_TickCount
	loop {
		TimeA := A_TickCount - StartA
		if TimeA < 1000
		send a
		else
		break
	}
	
	StartB := A_TickCount
	loop {
		TimeB := A_TickCount - StartB
		If TimeB < 1000 
		send b
		else 
		break
	}

}

return

F2::Pause
Starbuck1991
Posts: 12
Joined: 12 Apr 2020, 07:26

Re: Toggle script with nested loops

18 Apr 2021, 10:48

boiler wrote:
18 Apr 2021, 07:15
I think the issue is getting F2 to start and stop it (toggle it). Try this:
Yes, this was the issue, toggling the script with F2.

Your solution is quite simple, thank you very much, but there's a problem with the stopwatch that I've set up with A_TickCount. The script gets paused, but A_TickCount keeps growing, so when I resume the script, it doesn't run from the beginning, it just continues checking the current Time variable and, because now is bigger than the maximum value stablished in the if statement, it jumps to the next loop. The expected behavior is to restart all the counts when resuming the script. I don't know if that's possible using pause, because it doesn't affect A_TickCount.
Starbuck1991
Posts: 12
Joined: 12 Apr 2020, 07:26

Re: Toggle script with nested loops

18 Apr 2021, 10:51

swagfag wrote:
18 Apr 2021, 07:10
can't get this thing working
is very nondescript. for all i care, its working just fine - once F2 is pressed, proceeds to spam(execute code) as for a second, then spams(executes code) bs for a second and back again, indefinitely
I'm sorry that I've not explained myself well enough.

The script runs well indeed, but I need to toggle it using the same hotkey (in this case, F2), so it pauses & resets every time I press the key, as I explained in my previous answer to boiler.
User avatar
boiler
Posts: 16957
Joined: 21 Dec 2014, 02:44

Re: Toggle script with nested loops

18 Apr 2021, 12:34

This version restarts from the beginning by reloading the script when it's toggled off.

Code: Select all

#If Started
F2::Reload

#If !Started
F2::
	Started := 1
	loop {
	
		StartA := A_TickCount
		loop {
			TimeA := A_TickCount - StartA
			if TimeA < 1000
			send a
			else
			break
		}
		
		StartB := A_TickCount
		loop {
			TimeB := A_TickCount - StartB
			If TimeB < 1000 
			send b
			else 
			break
		}
	
	}

return

Esc::ExitApp
Starbuck1991
Posts: 12
Joined: 12 Apr 2020, 07:26

Re: Toggle script with nested loops

19 Apr 2021, 07:17

boiler wrote:
18 Apr 2021, 12:34
This version restarts from the beginning by reloading the script when it's toggled off.
It works flawlessly, thank you so, so much.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Google [Bot] and 188 guests