18
supposedlyGlobalVariable := "blah"

ARoutine()
{
   localVariable := "asdf"
   MsgBox, The global variable value is %supposedlyGlobalVariable%.  The local variable value is %localVariable%.
}


^!X:: ;This assigns the hotkey CTRL + ALT + X to run the routine
ARoutine()
return

Run the code and the result is:

"The global variable value is .  The local variable value is asdf."

The documentation states:

Variable scope and declarations: With the exception of local variables in functions, all variables are global; that is, their contents may be read or altered by any part of the script.

Why does my global variable not have scope within the function?

P.Brian.Mackey
  • 43,228
  • 68
  • 238
  • 348
  • 1
    Did you designate "`supposedlyGlobalVariable`" as Global at the top? If you add the line "`Global supposedlyGlobalVariable`" prior to declaring its value, it will work. EDIT: Oh, I see, it looks like you didn't. – PGilm Feb 19 '19 at 20:34

4 Answers4

15

The documentation for global variables can be found here:
https://autohotkey.com/docs/Functions.htm#Global

Global variables

To refer to an existing global variable inside a function (or create a new one), declare the variable as global prior to using it. For example:

LogToFile(TextToLog)
{
    global LogFileName
    FileAppend, %TextToLog%`n, %LogFileName%
}

I believe the concept of global, with AHK, is a bit different than in other languages. With AHK you can create a variable and use it within multiple hotkeys, and subroutines, without declaring it as global.

Gv := 0

f1::SetTimer, Action, % (on:=!on) ? (1000) : ("Off")

Action:
    Gv++
    trayTip,, % Gv
Return

f2::Msgbox, % Gv

Explaination of code:

  • The F1 key toggles a timer to run the subroutine: Action every 1000ms.
  • % starts an expression.
  • on:=!on reverses the binary value of variable on every time F1 is pressed.
  • ?: together is called the ternary operator.
  • When on=1 delay is set to 1000ms; when on=0 the timer is turned Off.

The ++ operator adds 1 to variable Gv.

Syscall
  • 19,327
  • 10
  • 37
  • 52
Honest Abe
  • 8,430
  • 4
  • 49
  • 64
  • That is counter-intuitive. If a variable is global, but not inside a function...then where is it global without requiring a modifier? – P.Brian.Mackey Apr 18 '12 at 19:09
  • @P.Brian.Mackey I've updated my answer in an attempt to address your comment. – Honest Abe Apr 18 '12 at 20:53
  • Don't know why this answers OP question? Putting the Global inside a function is not usually needed, and not what OP wants. – PGilm Feb 19 '19 at 20:36
9

This makes things easier:

https://www.autohotkey.com/docs/Functions.htm#SuperGlobal

Super-global variables [v1.1.05+]: If a global declaration appears outside of any function, it takes effect for all functions by default (excluding force-local functions). This avoids the need to redeclare the variable in each function. However, if a function parameter or local variable with the same name is declared, it takes precedence over the global variable. Variables created by the class keyword are also super-global.

Just declare your variable as global in the main script:

global supposedlyGlobalVariable := "blah"
Yane
  • 807
  • 8
  • 16
  • YES! This is all OP needs to do: modify as shown. Or as I suggest in my comment, just add the `Global supposedlyGlobalVariable` at top before declaring its value. – PGilm Feb 19 '19 at 20:38
  • This doesn't work: `global test := "1"` `f11:: msgbox test` Am I missing something? – JinSnow Oct 02 '19 at 06:47
  • you need `f11::msgbox, % test` or `f11::msgbox, %test%` – Yane Oct 03 '19 at 07:47
-1

P.Brian, It works when you do this.. I know it doesn't explain why, but this might be your workaround.

#Persistent
GlobalVariable = "blah"
RETURN

ARoutine:
{
   localVariable := "asdf"
   MsgBox, The global variable value is %GlobalVariable%.  The local variable value is %localVariable%.
}
Return

^!X:: ;This assigns the hotkey CTRL + ALT + X to run the routine
gosub, ARoutine
return
Robert Ilbrink
  • 7,738
  • 2
  • 22
  • 32
  • Sure, this works - but isn't an answer. Why change to using subroutine here? OP wants to use the variable in a function. And you don't even need to make it Global for using it in subroutines, in the first place. – PGilm Feb 19 '19 at 20:48
-1

You just need to declare the variable as global inside your function

supposedlyGlobalVariable := "blah"

ARoutine()
{
   global supposedlyGlobalVariable
   localVariable := "asdf"
   MsgBox, The global variable value is %supposedlyGlobalVariable%.  The local variable 
value is %localVariable%.
}


^!X:: ;This assigns the hotkey CTRL + ALT + X to run the routine
ARoutine()
return
Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
  • Totally defeats the purpose. OP wants the declaration OUTSIDE the function. You CAN declare it inside a function, but then you have to use the function at least once before you need that variable anywhere else. – PGilm Feb 19 '19 at 20:39
  • It doesn't work. I'm getting an error "This variable has not been assigned a value" and the global variable is empty. – Kirby Dec 11 '19 at 12:10