Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Global variable does not have global scope

Tags:

autohotkey

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?

like image 380
P.Brian.Mackey Avatar asked Apr 17 '12 20:04

P.Brian.Mackey


People also ask

Do global variables have scope?

A global variable has Global Scope: All scripts and functions on a web page can access it.

Which variables have global scope in Python?

A variable created in the main body of the Python code is a global variable and belongs to the global scope. Global variables are available from within any scope, global and local.

What is a global variable scope?

In computer programming, a global variable is a variable with global scope, meaning that it is visible (hence accessible) throughout the program, unless shadowed. The set of all global variables is known as the global environment or global state.

What is the scope of global variable in C++?

Global scope A global name is one that is declared outside of any class, function, or namespace. However, in C++ even these names exist with an implicit global namespace. The scope of global names extends from the point of declaration to the end of the file in which they are declared.


2 Answers

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.

like image 136
Honest Abe Avatar answered Oct 02 '22 09:10

Honest Abe


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"
like image 40
Yane Avatar answered Oct 02 '22 10:10

Yane