| View previous topic :: View next topic |
| Author |
Message |
Icarus
Joined: 24 Nov 2005 Posts: 507
|
Posted: Sun Sep 28, 2008 9:19 am Post subject: Problem with GUI Global Variables |
|
|
I am not 100% sure this is a bug, but it may be.
When making a function that then calls a label that attempts to build a GUI with variables for controls, I get an error that variables must be global, even though they are (since they are inside a label and not a function).
Here is a reproduction code:
| Code: |
#SingleInstance Force
#NoEnv
#NoTrayIcon
Test()
msgbox %TestVar%
Return
Test() {
;Global
Gosub BuildGui
}
BuildGui:
; This global variable works and will be displayed in the msg box above
TestVar := "hello"
; This line will only work if "Global" decleration is added to Test()
; Otherwise it generates an error "Gui Variables must be global"
; Uncomment the line below to get an error.
;Gui Add, Edit, w100 vGuiVar, hello world
;Gui Show
Return
ESC::ExitApp
|
_________________ Sector-Seven (Music and Utilities) |
|
| Back to top |
|
 |
Lexikos
Joined: 17 Oct 2006 Posts: 2737 Location: Australia, Qld
|
Posted: Sun Sep 28, 2008 9:47 am Post subject: |
|
|
Although the subroutine is not defined inside a function, when it is called, a function is executing, so the variable is created as a local. If you refer to the variable outside of any functions at least once (which you'd no doubt need to do at some point), the variable is created as global at load-time, and the Gui command succeeds.
I think it would be more appropriate for the subroutine to execute in the global context, rather than the context of the function. This adds some complexity, since AutoHotkey must take note of where the subroutine was defined when executing it. |
|
| Back to top |
|
 |
Icarus
Joined: 24 Nov 2005 Posts: 507
|
Posted: Sun Sep 28, 2008 10:13 am Post subject: |
|
|
So TestVar was working just because it was called once outside of a function.... thats confusing.
I did not know that labels inherit the scope of their caller. I was under the assumption that labels are global all the time. _________________ Sector-Seven (Music and Utilities) |
|
| Back to top |
|
 |
Lexikos
Joined: 17 Oct 2006 Posts: 2737 Location: Australia, Qld
|
Posted: Sun Sep 28, 2008 12:43 pm Post subject: |
|
|
| Quote: | | I did not know that labels inherit the scope of their caller. | I prefer to think of a label as a simple marker in the code, and a subroutine as a concept/one use of a label. AutoHotkey is not aware of a subroutine until it is actually running, so labels (or subroutines) do not affect variable scope.
More specifically, entering a function sets g.CurrentFunc. Since gosub does not affect g.CurrentFunc, execution is still "in the function." |
|
| Back to top |
|
 |
|