Goto/gosub not allowed, but is allowed via SetTimer

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
pneumatic
Posts: 338
Joined: 05 Dec 2016, 01:51

Goto/gosub not allowed, but is allowed via SetTimer

21 Jun 2017, 16:55

The following code is not allowed in Autohotkey:

Code: Select all

gosub SubRoutine

Function(){
SubRoutine:
msgbox Hello
return
}
However this is:

Code: Select all

SetBatchLines, -1
SetTimer, SubRoutine, 1000
loop
{
counter += 1  ;keeps execution here. The timer hasn't fired yet since it has to wait for its duration to expire first, therefore we are testing execution jumping from this line to the block below "which doesn't enclose it" 
}

Function(){
SubRoutine:
msgbox Hello
SetTimer,SubRoutine,Off
return
}

This seems to me a bug, because in the first instance Autohotkey says execution cannot jump to a block which doesn't enclose it, and yet we clearly can do such a thing with a timer. It would be nicer if we didn't have to use this workaround.
kon
Posts: 1756
Joined: 29 Sep 2013, 17:11

Re: Goto/gosub not allowed, but is allowed via SetTimer

21 Jun 2017, 17:24

Not a bug.

Again. Questions belong in the "Ask for Help" section. :roll:

Explained clearly here:
:terms: https://autohotkey.com/docs/Functions.htm#gosub
pneumatic
Posts: 338
Joined: 05 Dec 2016, 01:51

Re: Goto/gosub not allowed, but is allowed via SetTimer

22 Jun 2017, 06:25

The first thing I always do is look at the help file, followed by a forum search. Unfortunately I didn't find anything that explains why such an execution jump is allowed in the first instance but not the second. That's not to say there isn't some justifiable reason, only that I can't imagine a possible reason.
Guest

Re: Goto/gosub not allowed, but is allowed via SetTimer

22 Jun 2017, 06:33

It can be done somewhat differently

Code: Select all

Function("SubRoutine")

Function(label){
if IsLabel(label)
	{
	 Gosub, %label%
	 Return
	}
MsgBox Not Called	
SubRoutine:
MsgBox Hello
return
}
kon
Posts: 1756
Joined: 29 Sep 2013, 17:11

Re: Goto/gosub not allowed, but is allowed via SetTimer

22 Jun 2017, 11:28

pneumatic wrote:The first thing I always do is look at the help file, followed by a forum search.
And the second thing you did was post a bug report, insinuating that the problem lies with AHK, not your understanding.

If you are at the limits of your understanding, it is hubristic to assume there is a problem with the programming language itself.
"Thousands of people have hit a target. A single marksman attempts the shot and shoots himself in the foot. He then concludes that the gun is faulty, not his aim."
User avatar
Exaskryz
Posts: 2882
Joined: 17 Oct 2015, 20:28

Re: Goto/gosub not allowed, but is allowed via SetTimer

22 Jun 2017, 11:40

I do not know the answer, and reading over the docs didn't give me that eureka moment, so this could be wrong.

My guess is you can start a new thread at any point inside a function, so SetTimer works.

The difference is that GoSub cannot jump from outside the function (where the GoSub command is used) to inside of a function, though it is possible to use GoSub to jump around different parts of the same function. (These same limits may apply to GoTo.)

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: mmflume, ShatterCoder and 123 guests