Unable to create/change global variable from within function Topic is solved

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
bl33pingcomputer
Posts: 32
Joined: 29 Dec 2022, 05:36

Unable to create/change global variable from within function

10 Jan 2023, 04:38

While I've been making pretty good progress learning AHK2 as a beginner, I have become stumped on what should be an easy solution:

The purpose of my code is to capture the ahk_id of the current window when ^` are pressed, and assign the ahk_id to a global variable, window_id, so that the id can be accessed outside of the function (I plan to use the captured ahk_id with ControlSend).

My code

Code: Select all

^`:: {
global window_id
window_id := WinGetID("A")
MsgBox "All future uses of ControlSend will now act upon this window."
}

MsgBox window_id ; Throws the following error: "Error: This variable has not been assigned a value." Obviously, it is not able to access the variable.
I have tried declaring my "global window_id" outside of the script but it is not behaving the way I want it to. Any feedback would be appreciated.
User avatar
mikeyww
Posts: 27190
Joined: 09 Sep 2014, 18:38

Re: Unable to create/change global variable from within function  Topic is solved

10 Jan 2023, 06:42

Code: Select all

#Requires AutoHotkey v2.0
window_id := ""
F3:: MsgBox window_id
^`:: {
 Global window_id := WinGetID("A")
 MsgBox window_id
}
Explained: Uninitialized variables:
To initialize a variable is to assign it a starting value. A variable which has not yet been assigned a value is uninitialized (or unset for short). Attempting to read an uninitialized variable is considered an error.
bl33pingcomputer
Posts: 32
Joined: 29 Dec 2022, 05:36

Re: Unable to create/change global variable from within function

11 Jan 2023, 00:51

mikeyww wrote:
10 Jan 2023, 06:42

Code: Select all

#Requires AutoHotkey v2.0
window_id := ""
F3:: MsgBox window_id
^`:: {
 Global window_id := WinGetID("A")
 MsgBox window_id
}
Explained: Uninitialized variables:
To initialize a variable is to assign it a starting value. A variable which has not yet been assigned a value is uninitialized (or unset for short). Attempting to read an uninitialized variable is considered an error.
Thank you, you are an absolute godsend. I had attempted to initialize window_id by declaring it outside of the function as simply

Code: Select all

global window_id
but it was not doing the trick. I will take a look at the documentation for initialized variables.
User avatar
mikeyww
Posts: 27190
Joined: 09 Sep 2014, 18:38

Re: Unable to create/change global variable from within function

11 Jan 2023, 07:16

Variable scope in AHK v2

AHK v2 handles scope a bit differently from v1. In v2, declaring a variable as global outside a function is redundant. It only matters inside a function, where it is required if you want to use the variable outside the function, or if you want to access an existing global variable.

Thus, the script has the following three points.
1. Initialize variables before reading them.
2. To create a new global variable inside a function, or to write an existing global variable inside a function, declare the variable as global, inside the function, prior to using it.
3. If a variable in an assume-local function is only read (not written), then it may resolve to a global variable inside the function.

Code: Select all

#Requires AutoHotkey v2.0

window_id := ""                    ; Global by default; initialize the global variable

F3:: {                             ; Function is assume-local
 MsgBox window_id                  ; Variable is read; it resolves to existing *global* variable
}

^`:: {                             ; Function is assume-local
 Global window_id                  ; To write a global variable, *must* declare it as global here
 window_id := WinGetID("A")        ; Write the global variable
 MsgBox window_id                  ; Read the global variable
}
Nested functions may refer to local and static variables created by an enclosing function.

Return to “Ask for Help (v2)”

Who is online

Users browsing this forum: becritical, Draken, Hiroki-H, WarlordAkamu67 and 45 guests