Global variables are not global Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
Avtem
Posts: 43
Joined: 01 Jun 2021, 02:20

Global variables are not global

Post by Avtem » 05 Jul 2022, 14:08

So, i learned that "global" variables must be in auto-execution section in order to work.
But i don't want to declare global variables at the top of the file. Is it possible to declare global variables below hotkey F24 which makes the end of auto-execution section?

Code: Select all

; str:= "WORKS!"		; works fine here
f24::exitApp
str:= "WORKS!"		; doesn't work here (even with "global")

#IfWinActive ahk_exe mspaint.exe
1::msgbox % str

User avatar
mikeyww
Posts: 26848
Joined: 09 Sep 2014, 18:38

Re: Global variables are not global

Post by mikeyww » 05 Jul 2022, 14:11

You are confused on multiple accounts.
1. Variables are global by default (not true inside functions). https://www.autohotkey.com/docs/Functions.htm#Global
2. Code to execute when the script first runs belongs at the top, in auto-exec. https://www.autohotkey.com/docs/Scripts.htm#auto
3. Unlabeled code following Return is unreachable and will never execute.

False:
"global" variables must be in auto-execution section in order to work.
True:
After the script has been loaded, it begins executing at the top line, continuing until a Return, Exit, hotkey/hotstring label, or the physical end of the script is encountered (whichever comes first).

Code: Select all

str := "WORKS!"

; =========================
; End of auto-exec
; =========================

F24::ExitApp

#IfWinActive ahk_exe mspaint.exe
1::MsgBox, % str
#IfWinActive

User avatar
Avtem
Posts: 43
Joined: 01 Jun 2021, 02:20

Re: Global variables are not global

Post by Avtem » 05 Jul 2022, 14:46

mikeyww wrote:
05 Jul 2022, 14:11
You are confused on multiple accounts.
1. Variables are global by default (not true inside functions). https://www.autohotkey.com/docs/Functions.htm#Global
2. Code to execute when the script first runs belongs at the top, in auto-exec. https://www.autohotkey.com/docs/Scripts.htm#auto
3. Unlabeled code following Return is unreachable and will never execute.

False:
"global" variables must be in auto-execution section in order to work.
True:
After the script has been loaded, it begins executing at the top line, continuing until a Return, Exit, hotkey/hotstring label, or the physical end of the script is encountered (whichever comes first).

Code: Select all

str := "WORKS!"

; =========================
; End of auto-exec
; =========================

F24::ExitApp

#IfWinActive ahk_exe mspaint.exe
1::MsgBox, % str
#IfWinActive
Sorry, i did not emphasize the word DECLARE enough. i know all how all those 3 concepts work, and i shouldn't say that global variables don't work below auto-exec section. They do work, but what i want to do is DECLARE a global variable after auto-exec section.

User avatar
mikeyww
Posts: 26848
Joined: 09 Sep 2014, 18:38

Re: Global variables are not global  Topic is solved

Post by mikeyww » 05 Jul 2022, 15:01

Try my script; see if it works.

You would only declare as global if needed in a function. You can declare it in auto-exec or in any labeled routine or function-- because those are the only three places where code is executed.

Your "doesn't work here" has nothing to do with global scope. The bug exists because you put the code in an unlabeled area below Return.

Hotkeys:
Return serves to finish the hotkey. However, if a hotkey needs to execute only a single line, that line can be listed to the right of the double-colon. In other words, the return is implicit.

User avatar
Avtem
Posts: 43
Joined: 01 Jun 2021, 02:20

Re: Global variables are not global

Post by Avtem » 05 Jul 2022, 15:19

mikeyww wrote:
05 Jul 2022, 15:01
Try my script; see if it works.

You would only declare as global if needed in a function. You can declare it in auto-exec or in any labeled routine or function-- because those are the only three places where code is executed.

Your "doesn't work here" has nothing to do with global scope. The bug exists because you put the code in an unlabeled area below Return.

Hotkeys:
Return serves to finish the hotkey. However, if a hotkey needs to execute only a single line, that line can be listed to the right of the double-colon. In other words, the return is implicit.
Your script works, and it's exactly the same as one in my original post (i put there a comment so people would know that i understand the "solution").
But i understand it now - as you said: because those are the only three places where code is executed. So in any script there can be "dead" places where you can not declare a variable.

Thanks for your help!

User avatar
mikeyww
Posts: 26848
Joined: 09 Sep 2014, 18:38

Re: Global variables are not global

Post by mikeyww » 05 Jul 2022, 16:03

You're right that the scripts are the same, except where they are different: one works, and one doesn't-- due to the differences in the order of the code. Thus, a falsehood:
it's exactly the same as one in my original post
Just like syntax matters, order matters....

You are right: an AutoHotkey v1 script often has dead areas because AHK is event-driven by design. I believe that v2 changes how that works as commands and subroutines are transformed into functions.

Post Reply

Return to “Ask for Help (v1)”