A bit lost regarding toggling Loops

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Dreamlight
Posts: 3
Joined: 08 Mar 2019, 13:50

A bit lost regarding toggling Loops

08 Mar 2019, 13:56

So I am trying to build a larger script that is basically a togglable loop of actions. For some reason toggling it on and off doesn't work so I tried do some research and first get it going with smaller scripts:

Code: Select all


#MaxThreadsPerHotkey 2
F5::
Toggle := !Toggle
loop
{
    If not Toggle
        break

    tooltip,% A_Index
	
}
return
This code works. It lets me toggle on and off and I get shown the tooltip. (I found this example in an older tutorial on how to do toggleable loops). So I tried to make a slight variation on this:

Code: Select all

#MaxThreadsPerHotkey 2
F5::
Toggle := !Toggle
loop
{
    If not Toggle
        break

    tooltip,% A_Index
	send {q}
}
return
This code already does not toggle anymore it just keeps ticking up and sending q.

I also tried a third script that also didn't toggle for some reason:

Code: Select all

#MaxThreadsPerHotkey 2
F5::
Toggle := !Toggle
loop
{
    If not Toggle
        break

    send {q}
}
return
I really am lost here because for all I understand both of these scripts should allow toggle via F5. Can anyone shed light on what my issue may be here?
User avatar
sinkfaze
Posts: 616
Joined: 01 Oct 2013, 08:01

Re: A bit lost regarding toggling Loops

08 Mar 2019, 15:03

A loop like that, generally, is not a good way to run a toggle.

Might I suggest using SetTimer instead?

Code: Select all

F5::
Toggle := !Toggle
SetTimer, SendQ, %	Toggle ? 10 : "Off"
return

SendQ:
Send q
return
Dreamlight
Posts: 3
Joined: 08 Mar 2019, 13:50

Re: A bit lost regarding toggling Loops

08 Mar 2019, 15:47

Well this largely worked for me. Now I have one issue: My routine is roughly 10s long to be executed every 32s. If I set the timer like that the first time it will be executed will be after 32s and if I just call the function I can't toggle it off while the function is running the first time.

Also if someone else runs into this issue, I had to declare toggle as global to get it to work inside my function.
User avatar
sinkfaze
Posts: 616
Joined: 01 Oct 2013, 08:01

Re: A bit lost regarding toggling Loops

08 Mar 2019, 16:06

Just out of curiosity, why do you wish to embed this in a function?
Dreamlight
Posts: 3
Joined: 08 Mar 2019, 13:50

Re: A bit lost regarding toggling Loops

08 Mar 2019, 16:16

Because it is a routine for a game that I want to bea ble to cancel at some points.

That said another issue seems to arise. I set the timer to 32000, but it didn't got off precisely after 32000 ms (more like 31900 ms I guess?) Any idea why that might happen?

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: No registered users and 177 guests