Global variable inside the function

Get help with using AutoHotkey and its commands and hotkeys
john_c
Posts: 175
Joined: 05 May 2017, 13:19

Global variable inside the function

13 Dec 2017, 15:39

I want to create a function, which will check settings specified in the beginning of the script, and if setting is true, then enable some hotkey. This works:

Code: Select all

mySetting1 = true
mySetting2 = true

CheckSetting(setting) {
	Global mySetting1
	Global mySetting2
	If setting = true
		Return, 1
}

#If, CheckSetting(mySetting1)
    a:: Send, b
#If
But, when I try to use "Global %setting%", instead of "Global mySetting1" and "Global mySetting2", it doesn't work:

Code: Select all

mySetting1 = true
mySetting2 = true

CheckSetting(setting) {
	Global %setting%
	If setting = true
		Return, 1
}

#If, CheckSetting(mySetting1)
    a:: Send, b
#If
Is there way to fix it?
User avatar
Spawnova
Posts: 339
Joined: 08 Jul 2015, 00:12
Contact:

Re: Global variable inside the function

13 Dec 2017, 15:44

I don't think you can use global like that, however this works exactly the same

Code: Select all

mySetting1 := true
mySetting2 := true

#If mySetting1
    f1:: msgbox mySettings1 = true
#If

f2::
mySetting1 := false
return
Osprey
Posts: 294
Joined: 18 Nov 2017, 05:50

Re: Global variable inside the function

13 Dec 2017, 15:58

The simplest way to fix it is to just have Global with nothing after it. That will declare that all variables used by the function are global variables.
john_c
Posts: 175
Joined: 05 May 2017, 13:19

Re: Global variable inside the function

13 Dec 2017, 16:22

Spawnova, Osprey - both solutions works. Thank you very much!

Return to “Ask For Help”

Who is online

Users browsing this forum: pneumatic and 30 guests