Hotkeys to switch betwen labels v2 Topic is solved

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
User avatar
Jinjiro
Posts: 8
Joined: 11 May 2023, 20:01
Contact:

Hotkeys to switch betwen labels v2

13 May 2024, 04:11

Hi, I want to run a script with diferent labels and include hotkeys to switch betwen them, for example:

Code: Select all

^0::
{Soundbeep 550
Goto Wait
}

^1::
{Soundbeep 650
Goto Action1
}

Action0:
If ( x=0)...

Action1:
If ( x=1)...

Wait:
Sleep 1000
Goto Wait

:?
But the PC keeps giving me this error message:

"Error: Label not found in current scope."

Could sombody help me.

Thanks
User avatar
boiler
Posts: 17212
Joined: 21 Dec 2014, 02:44

Re: Hotkeys to switch betwen labels v2

13 May 2024, 06:19

Don’t use Goto. At all. Ever.

Why are you putting the code to be executed outside the hotkey functions anyway? Just put the code you want to execute inside the functions themselves.
User avatar
Seven0528
Posts: 394
Joined: 23 Jan 2023, 04:52
Location: South Korea
Contact:

Re: Hotkeys to switch betwen labels v2  Topic is solved

13 May 2024, 07:03

 @Jinjiro
As the language moved from version 1 to version 2, the Gosub command was completely removed,
but the Goto command survived because maybe, there are still limited scenarios where it might be useful.

To my knowledge, this command can be effectively used to escape from multiple loops.
It could be considered when handling code that needs to be executed commonly at the end of a thread.
It's the only usage of Goto that I know of that doesn't compromise readability.

However, it's strongly advised not to use Goto in any other scenario.
In fact, even in this case, its use is not highly recommended.

You can always use a Break statement to exit nested loops, and you can even use Break itself akin to a Goto.
As @boiler mentioned, without a single exception, every situation can be handled without Goto.

If programming without Goto seems difficult, it's purely due to a lack of skill.
Even in highly exceptional cases where the use of Goto might improve readability, code can always be written without it,
and the difference in readability between the two codes would likely be minimal.
Study Functions and other programming techniques.
Think about how you can refactor your code.
I recommend reconsidering your approach and asking questions again from there.

Code: Select all

F3::  {
    prevIC := critical("On")
    loop    {
        loop    {
            loop    {
                goto break_outer ;  In this case, you might consider using `goto` instead of `break 3` or 'break outer'.
            }
        }
    }
break_outer:
    critical(prevIC)
    ;  ...
}

Code: Select all

F3::  {
    prevIC := critical("On")
    loop    {
        loop    {
            loop    {
                break 3
            }
        }
    }
    critical(prevIC)
    ;  ...
}

Code: Select all

F3::  {
    prevIC := critical("On")
    outer:
    loop    {
        loop    {
            loop    {
                break outer
            }
        }
    }
    critical(prevIC)
    ;  ...
}
  • English is not my native language. Please forgive any awkward expressions.
  • 영어는 제 모국어가 아닙니다. 어색한 표현이 있어도 양해해 주세요.
User avatar
kunkel321
Posts: 1140
Joined: 30 Nov 2015, 21:19

Re: Hotkeys to switch betwen labels v2

13 May 2024, 07:30

Not sure if this will help, but you can have a hotkey activate a named function. With this example, myFunction runs at start up, because myFunction() calls it. Also though, you can run it by pressing Alt+Ctrl+1.

Code: Select all

#SingleInstance
#Requires AutoHotkey v2+

myFunction()

!^1::
myFunction(*)
{
	soundbeep
}
ste(phen|ve) kunkel
User avatar
Jinjiro
Posts: 8
Joined: 11 May 2023, 20:01
Contact:

Re: Hotkeys to switch betwen labels v2

14 May 2024, 13:14

boiler wrote:
13 May 2024, 06:19
Don’t use Goto. At all. Ever.

Why are you putting the code to be executed outside the hotkey functions anyway? Just put the code you want to execute inside the functions themselves.
Becuase i am runing a long script and what i need is that, based in some events, go directly to a label in particular.

Anyway i will try to find the way do it as you recomend.

Thanks.

J
User avatar
Jinjiro
Posts: 8
Joined: 11 May 2023, 20:01
Contact:

Re: Hotkeys to switch betwen labels v2

14 May 2024, 13:25

Seven0528 wrote:
13 May 2024, 07:03
[Mod edit: Removed huge quote contents of a whole post, which is still linked.]

Thank you for the advise. I have been practicing hours but, as you said, I need more skill.

The thing is that some loop scripts, usualy short ones, work great but in other cases where i need more conditions to be done in order to do an action into the lopp, I can't make them work, then i return to Goto stuff and it usually works. So i am getting used to use Goto. Accually i have been looking for examples of loop but still can't find many as v1.

Greatings
j
User avatar
Seven0528
Posts: 394
Joined: 23 Jan 2023, 04:52
Location: South Korea
Contact:

Re: Hotkeys to switch betwen labels v2

14 May 2024, 15:23

 @Jinjiro
Goto is almost taboo for beginners learning programming.
In fact, many might not even teach it, and if they do, there's always a warning against its usage.
https://www.autohotkey.com/docs/v2/lib/Goto.htm#Remarks wrote: The use of Goto is discouraged because it generally makes scripts less readable and harder to maintain. Consider using Else, Blocks, Break, and Continue as substitutes for Goto.
While there are appropriate uses for goto, they're not relevant for beginners.
AutoHotkey is a language first and foremost, even before being a programming language. There's a reason why it's not recommended, and even cautioned against using it. Even if you're not a programmer, it's a genuinely bad habit. (I'm not a programmer myself either.) If you can't write the same structure of code without using goto, never use it.
And if you can write that code without using goto, you'll understand how much goto harms readability, yourself.

Using labels itself is problematic. It's better to use functions.
Starting from v2, functions can only reference labels within their own scope. (And all hotkeys are functions.)
What you want isn't achievable nor necessary from the outset in v2.
https://www.autohotkey.com/docs/v2/misc/Labels.htm#syntax-and-usage wrote: Scope: Each function has its own list of local labels. Inside a function, only that function's labels are visible/accessible to the script.
Failing to manage long scripts properly is an issue in itself.
Instead, divide them into shorter scripts using functions or (slightly more advanced) classes.
When similar commands repeat, you can shorten the code length by encapsulating the commands' parameters into for-loops with arrays.
Looping over specific segments isn't limited to just loop;
you can also use settimer to periodically execute a specific thread.

If you don't want to study or want to settle for the status quo, that's your choice and it's respected.
But you should also respect the choices of countless respondents who don't want to write excessively inefficient, unreadable, non-standard, and hard-to-maintain spaghetti code. (To be honest, almost everyone here will think the same.)
I strongly recommend boldly discarding outdated, incorrect customs you're accustomed to and stepping into a new world.
How about posting a functional v1 code here and asking how you can refactor it?
Thank you.
Last edited by Seven0528 on 14 May 2024, 15:34, edited 2 times in total.
  • English is not my native language. Please forgive any awkward expressions.
  • 영어는 제 모국어가 아닙니다. 어색한 표현이 있어도 양해해 주세요.
User avatar
boiler
Posts: 17212
Joined: 21 Dec 2014, 02:44

Re: Hotkeys to switch betwen labels v2

14 May 2024, 15:26

Jinjiro wrote: Becuase i am runing a long script and what i need is that, based in some events, go directly to a label in particular.
As Seven0528 mentioned, you can do this without labels, and you should. You could what you described by having the code in a series of functions, and you just call the function that is the right place to jump into.

Return to “Ask for Help (v2)”

Who is online

Users browsing this forum: cgx5871, Draken, Nandiman, niCode and 33 guests