set global variable in a function, when the name of the variable is given as a parameter. Topic is solved

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
Spitzi
Posts: 309
Joined: 24 Feb 2022, 03:45

set global variable in a function, when the name of the variable is given as a parameter.

Post by Spitzi » 07 Jun 2023, 11:35

How can I set a global variable in a function, knowing the name of the variable (which varies). I have this code which does not work.

Code: Select all

global a,b,c,d,e

SetGlobal(varName, varVal) {
	global %varName%
	%varName% := varVal
}
is there any way to accomplish this?

WKen
Posts: 182
Joined: 21 Feb 2023, 00:01

Re: set global variable in a function, when the name of the variable is given as a parameter.

Post by WKen » 07 Jun 2023, 12:41

Is varName not %varName% in v2.
Last edited by WKen on 07 Jun 2023, 12:41, edited 1 time in total.

User avatar
boiler
Posts: 16767
Joined: 21 Dec 2014, 02:44

Re: set global variable in a function, when the name of the variable is given as a parameter.

Post by boiler » 07 Jun 2023, 12:41

What do you mean try setting a global variable from a function? Are you trying to make it global across functions, which was known as a super-global in v1? There are no super-global variables in v2.

I’m not seeing what you mean when you say your code doesn’t work. What is it supposed to do? The function isn’t even called.

User avatar
boiler
Posts: 16767
Joined: 21 Dec 2014, 02:44

Re: set global variable in a function, when the name of the variable is given as a parameter.

Post by boiler » 07 Jun 2023, 12:46

WKen wrote: Is varName not %varName% in v2.
It is for double dereferencing, which is what OP was going for. As in the following:

Code: Select all

#Requires AutoHotkey v2.0
VarName := "MyVar"
MyVar := "hello"
MsgBox %VarName% ; output is "hello"

WKen
Posts: 182
Joined: 21 Feb 2023, 00:01

Re: set global variable in a function, when the name of the variable is given as a parameter.

Post by WKen » 07 Jun 2023, 12:55

Well, I'm a rookie and I've learned it. :)

Spitzi
Posts: 309
Joined: 24 Feb 2022, 03:45

Re: set global variable in a function, when the name of the variable is given as a parameter.

Post by Spitzi » 07 Jun 2023, 13:34

In my app, I have many user settings as global variables
those settings can be changed by checkboxes or edits in a gui, example:

Code: Select all

		
		checkBoxObj := MyGui.Add("CheckBox", "vAutoConfirmNoPicsLoaded " . (checked:=AutoConfirmNoPicsLoaded ? "checked" : ""), "Automatisch die Meldung 'Keine neuen Bilder' quittieren.")
		checkBoxObj.OnEvent("Click", SaveCheckboxSetting.Bind(, , sectionName:="Auto Confirm No Pics Loaded Message", IniFilePath, secondaryFunctionName:="DoSomeMoreStuff"))

All checkboxes call the same function when clicked:

Code: Select all

	; saves the setting of a checkbox/textbox/combobox to a given ini-File
	; - ctrlObj: the control Object whose value needs to be saved
	; - SectionName: the name of the section under which the setting needs to be saved in the ini-file
	; - FilePath: the path to an ini.File
	; - optional SecondaryFunctionName: the name of a function to be called after this one - with the value of the control as a paramenter
	SaveCheckboxSetting(ctrlObj, info, SectionName, FilePath, SecondaryFunctionName:="") {
		global IniFilePath
		OutputDebug("SaveCheckboxSetting(GuiName,CheckboxHWND, SectionName, FilePath, SecondaryFunctionName)`n")
		; MsgBox, CheckboxHWND:%CheckboxHWND%`nIniHeadingName:%SectionName%`nSecondaryFunctionName:%SecondaryFunctionName%`nFilePath:%FilePath%
		FilePath := (FilePath == "") ? IniFilePath: FilePath

		controlValue :=  (ctrlObj.Type == "ComboBox") ? ctrlObj.Text : ctrlObj.Value																	; get the value of the checkbox / edit control. if it is a combobox, get it's text
		IniWrite(controlValue, FilePath, SectionName, A_UserName)																						; write the value to the ini file
		if (SecondaryFunctionName != "") {																												; call a secondary function if there is one
			FuncToBeCalled := %SecondaryFunctionName%.Bind(controlValue)
			FuncToBeCalled()
		}
	}
The function stores the changed setting in the ini-File and, if necessary, calls another function to do some more stuff


Withing this function, I would also like to to set the global variable (in the example 'AutoConfirmNoPicsLoaded')
So the name of the global variable would come from ctrlObj.Name, in general



Simplified, this boils down to this code:

Code: Select all

global a,b,c,d,e

a := 5
SetGlobal("a", 6)
MsgBox(a)

SetGlobal(varName, varVal) {
	varName := varVal
}
I would like for the global a to set it to 6. The code prints 5 instead.

ntepa
Posts: 406
Joined: 19 Oct 2022, 20:52

Re: set global variable in a function, when the name of the variable is given as a parameter.  Topic is solved

Post by ntepa » 07 Jun 2023, 15:12

Code: Select all

global a,b,c,d,e

a := 5
SetGlobal("a", 6)
MsgBox(a)

SetGlobal(varName, varVal) {
    global
    %varName% := varVal
}

Spitzi
Posts: 309
Joined: 24 Feb 2022, 03:45

Re: set global variable in a function, when the name of the variable is given as a parameter.

Post by Spitzi » 07 Jun 2023, 17:25

Thanks @ntepa , that's what I have been looking for.

I ended up putting all my settings in a global map:

Code: Select all

global Settings := Map()

Settings["a"] := 5
SetGlobal("a", 6)
MsgBox(Settings["a"])

SetGlobal(varName, varVal) {
	global Settings
	Settings[varName] := varVal
}
The code is nicer like that and more readable.

Post Reply

Return to “Ask for Help (v2)”