Page 1 of 1

Function only working when called from gui

Posted: 05 May 2024, 03:50
by bowie1123
When I call `updateOptions()` from a g-label from a Gui button it works properly, however when it is called from a hotkey definition, MsgBox prints `v` correctly but `val` is blank. Why is this?

Code: Select all

global guiToOptions := {"ItemSearchCheckbox":"ItemSearch"
    ,"CraftCheckbox":"Craft"}

updateOptions() {
    for i, v in guiToOptions {
        GuiControlGet, val,,%i%
        MsgBox % v ", " . val
        options[v] := val
    }
}

Re: Function only working when called from gui

Posted: 05 May 2024, 05:58
by mikeyww
Welcome to this AutoHotkey forum!

The bug is in the part of your script that you have not posted.

Your "options" array has no role in the script that you posted. It would also be local to your function unless declared as global.

If you are new to AHK, I recommend using its current version, which is v2, instead of this older deprecated version that is no longer developed.

Re: Function only working when called from gui

Posted: 05 May 2024, 11:12
by Chunjee
If your GUI exists in the global scope I think you can fix this code by the following:

Code: Select all

guiToOptions := {"ItemSearchCheckbox":"ItemSearch"
    ,"CraftCheckbox":"Craft"}

updateOptions() {
    global
    for i, v in guiToOptions {
        GuiControlGet, val,,%i%
        MsgBox % v ", " . val
        options[v] := val
    }
}
I would avoid superglobals personally

Re: Function only working when called from gui

Posted: 06 May 2024, 03:04
by just me
Chunjee wrote:I would avoid superglobals personally
I would avoid assume-global mode personally. ;)

@bowie1123: Does your GUI have a name or a number other than 1?