Strange Global variable bug

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
Gaik
Posts: 25
Joined: 14 Jan 2024, 10:05

Strange Global variable bug

17 Jan 2024, 17:07

I declared variables at the top.

Code: Select all

 #Requires AutoHotkey v2.0

    Global Self_edit := "Q"  ;编辑模式开关
    MsgBox "D:" . Self_edit
    
    
When called below, an error message will appear.

Code: Select all

    Button3_Click(GuiCtrlObj, *) {
        MsgBox "D:" . Self_edit

        if (Self_edit = "Q"){
        
        }
        
        
     }
Error: This variable has not been assigned a value.


E:\AutoHotkey\demo\Mococo\main.ahk (126) : ==> This variable has not been assigned a value.
Specifically: local Self_edit (same name as a global)

Specifically: local Self_edit (same name as a global)

122: }
125: {
▶ 126: MsgBox("D:" . Self_edit)
128: If (Self_edit = "Q")
128: {
User avatar
mikeyww
Posts: 27294
Joined: 09 Sep 2014, 18:38

Re: Strange Global variable bug

17 Jan 2024, 19:27

Hello,

I recommend posting a script that the forum reader can run to demonstrate the problem that you are experiencing.
User avatar
Seven0528
Posts: 412
Joined: 23 Jan 2023, 04:52
Location: South Korea
Contact:

Re: Strange Global variable bug

17 Jan 2024, 20:08

 Scope (en)
 作用域 (zh)
Changes from v1.1 to v2.0 wrote: Super-global variables have been removed (excluding built-in variables, which aren't quite the same as they cannot be redeclared or shadowed).
超级-全局 变量已被删除(不包括内置变量, 因为它们不能被重新声明或隐藏, 所以不完全一样).
This is not a bug. It is a difference that arose from v2 onwards.
这不是一个错误。这是从v2开始出现的差异。
Last edited by Seven0528 on 20 Jan 2024, 17:36, edited 4 times in total.
  • English is not my native language. Please forgive any awkward expressions.
  • 영어는 제 모국어가 아닙니다. 어색한 표현이 있어도 양해해 주세요.
User avatar
Seven0528
Posts: 412
Joined: 23 Jan 2023, 04:52
Location: South Korea
Contact:

Re: Strange Global variable bug

17 Jan 2024, 20:21

Code: Select all

#Requires AutoHotkey v2.0
#SingleInstance Force
gVar := "q"
gProp.var := "Q"
someFunc()

Class gProp
{
}
someFunc()    {
    global gVar
    Msgbox gVar
    /*
    To "edit" variables outside the function, a global declaration is required.
    要在函数外部“编辑”变量,需要进行全局声明。
    */
    Msgbox gProp.var
    /*
    When using a Class object, it is treated as a global constant (read-only variable), 
    so it can be used without a separate global declaration.
    在使用类对象时,它被视为全局常量(只读变量),
    因此无需额外的全局声明即可使用。
    */
}
Last edited by Seven0528 on 20 Jan 2024, 17:37, edited 7 times in total.
  • English is not my native language. Please forgive any awkward expressions.
  • 영어는 제 모국어가 아닙니다. 어색한 표현이 있어도 양해해 주세요.
User avatar
mikeyww
Posts: 27294
Joined: 09 Sep 2014, 18:38

Re: Strange Global variable bug

17 Jan 2024, 20:23

To use variables from outside the function, a global declaration is required.
This is actually not correct. It is required only to write the variable, not to read it.

Easily demonstrated:

Code: Select all

#Requires AutoHotkey v2.0
x := 1
y

y() {
 MsgBox x
}
User avatar
Seven0528
Posts: 412
Joined: 23 Jan 2023, 04:52
Location: South Korea
Contact:

Re: Strange Global variable bug

17 Jan 2024, 20:33

 @mikeyww
You're right. It may have been an inappropriate explanation. I'm sorry...
I have appropriately revised the explanation.

Code: Select all

#Requires AutoHotkey v2.0
#SingleInstance Force
x := 1
y()
y()    {
    MsgBox x ;  1
}

Code: Select all

#Requires AutoHotkey v2.0
#SingleInstance Force
x := 1
y()
MsgBox x ;  1
y()    {
    x := 2
}

Code: Select all

#Requires AutoHotkey v2.0
#SingleInstance Force
x := 1
y()
y()    {
    MsgBox x ;  Error: This variable has not been assigned a value.  Specifically: local x  (same name as a global)
    x := 2
}

Code: Select all

#Requires AutoHotkey v2.0
#SingleInstance Force
x := 1
y()
MsgBox x ;  2
y()    {
    global x
    MsgBox x ;  1
    x := 2
}
Last edited by Seven0528 on 20 Jan 2024, 17:23, edited 7 times in total.
  • English is not my native language. Please forgive any awkward expressions.
  • 영어는 제 모국어가 아닙니다. 어색한 표현이 있어도 양해해 주세요.
User avatar
mikeyww
Posts: 27294
Joined: 09 Sep 2014, 18:38

Re: Strange Global variable bug

17 Jan 2024, 20:40

The problem with the OP is that it is missing the part of the script that is causing the problem that is reported, or it shows code that does not actually execute as shown. This explains my original recommendation. Instead of seeing one script, we now see two script fragments that are insufficient, independently, to troubleshoot the issue as described. We must guess what the real script is, instead of just seeing it so that it can be tested.

There may be good reasons to omit code-- script too long, private information, etc. In these circumstances, one can still post a script that demonstrates the problem at hand, and this is what I have recommended.
coffee
Posts: 133
Joined: 01 Apr 2017, 07:55

Re: Strange Global variable bug

17 Jan 2024, 20:50

Seven0528 wrote:
17 Jan 2024, 20:33
 @mikeyww
You're right. It may have been an inappropriate explanation. I'm sorry...
I have appropriately revised the explanation.

Code: Select all

...

Code: Select all

...

Code: Select all

#Requires AutoHotkey v2.0
#SingleInstance Force
x := 1
y()
y()    {
    MsgBox x ;  Error: This variable has not been assigned a value.  Specifically: local x  (same name as a global)
    x := 2
}

Code: Select all

...
I believe third example fails due to a concept akin to javascript's hoisting. The runtime sees the variable is being assigned a value without a global declaration and it automatically creates a local binding for it hoisted at the top of the scope, which msgbox tries to use but it has not reached the point where the variable is defined so it has no value.
It may be possible that's what causing the problem in OP's example, since stuff was omitted in the example, and if they are writing to self_edit further below in the function without declaring it global within the function first, it creates a local binding for it and fails on first use.
All they would have to do is global self_edit at the top of the function for clarity.
User avatar
Seven0528
Posts: 412
Joined: 23 Jan 2023, 04:52
Location: South Korea
Contact:

Re: Strange Global variable bug

17 Jan 2024, 21:09

 @coffee
Thank you for the kind explanation!
When I was studying dynamic variables in v1 before, I had a similar impression... (I understand that it's not exactly the same concept.)
I appreciate your help in fostering a deeper understanding! (I'm not well-versed in other programming languages.)

v1.1
v2.0
  • English is not my native language. Please forgive any awkward expressions.
  • 영어는 제 모국어가 아닙니다. 어색한 표현이 있어도 양해해 주세요.
Gaik
Posts: 25
Joined: 14 Jan 2024, 10:05

Re: Strange Global variable bug

20 Jan 2024, 12:12

Seven0528 wrote:
17 Jan 2024, 21:09
 @coffee
Thank you for the kind explanation!
When I was studying dynamic variables in v1 before, I had a similar impression... (I understand that it's not exactly the same concept.)
I appreciate your help in fostering a deeper understanding! (I'm not well-versed in other programming languages.)

v1.1
v2.0

Thank you very much for your guidance. You have correctly helped me solve the problem. I understand that V2 needs to indicate the use of global variables in the function in order to be introduced correctly :bravo: :bravo: :bravo: :bravo: :bravo: :dance: :dance: :dance:
User avatar
boiler
Posts: 17327
Joined: 21 Dec 2014, 02:44

Re: Strange Global variable bug

20 Jan 2024, 12:27

Gaik wrote: I understand that V2 needs to indicate the use of global variables in the function in order to be introduced correctly
Not necessarily. If the function only references a global variable rather than assigning a value to it, it can usually determine that is referencing a global variable.
Gaik
Posts: 25
Joined: 14 Jan 2024, 10:05

Re: Strange Global variable bug

20 Jan 2024, 14:15

boiler wrote:
20 Jan 2024, 12:27
Gaik wrote: I understand that V2 needs to indicate the use of global variables in the function in order to be introduced correctly
Not necessarily. If the function only references a global variable rather than assigning a value to it, it can usually determine that is referencing a global variable.
If I don't reference global in the function, I keep reporting errors. His answer solved my troubles. Thank you everyone
User avatar
boiler
Posts: 17327
Joined: 21 Dec 2014, 02:44

Re: Strange Global variable bug

20 Jan 2024, 15:09

@Gaik — As has been pointed out, you have only shown fragments of your script, so there is likely another part of your function that is assigning a value to that variable, causing it to require that declaration. That’s fine that it fixes this particular script, but you seemed to state a conclusion as if it’s generally true, so I thought you’d want to know that it’s not the case, for future reference.
User avatar
Seven0528
Posts: 412
Joined: 23 Jan 2023, 04:52
Location: South Korea
Contact:

Re: Strange Global variable bug

20 Jan 2024, 17:21

 @Gaik
I'm glad to hear that your problem has been resolved!
However, as @boiler mentioned, there are cases where the global declaration is necessary, and there are cases where it is not necessary.
I'll leave it here just in case there might be any misunderstandings...
我很高兴听到您的问题已经解决!
然而,正如@boiler提到的,有些情况下global声明是必要的,而有些情况下则不是必要的。
我将这里写下,以防有任何误解...

Code: Select all

#Requires AutoHotkey v2.0
#SingleInstance Force
gVar := "q"
someFuncA()
someFuncB()

someFuncA()    {
    Msgbox A_ThisFunc "`t" gVar
    /*
    If you only want to "read" variables outside the function, a global declaration is not necessary.
    要在函数外部“读取”变量,不需要进行全局声明。
    */
}
someFuncB()    {
    global gVar
    Msgbox A_ThisFunc "`t" gVar
    /*
    To "edit" variables outside the function, a global declaration is required.
    要在函数外部“编辑”变量,需要进行全局声明。
    */
}
  • English is not my native language. Please forgive any awkward expressions.
  • 영어는 제 모국어가 아닙니다. 어색한 표현이 있어도 양해해 주세요.

Return to “Ask for Help (v2)”

Who is online

Users browsing this forum: vmech, wilkster, william_ahk and 53 guests