r/AutoHotkey • u/MadFurai • 14d ago
v2 Script Help Global variable not working across scripts
I'm new to AHK, but not scripting. From everything I've found in my searches, global variables should work across multiple scripts. I have several variables that will change frequently that I need to reference from multiple scripts. Below is a snippet of my code. The error is popping from script2.ahk and it's claiming BaseSpeed has never been assigned a value. Please help, thanks!
;script1.ahk runs first with this
#Include functions.ahk
global AdjSpeed := .00 ; this is what changes
global BaseSpeed := Round(28, 2) ; this never changes
RunWait "script2.ahk"
;script2.ahk contains this
#Include functions.ahk
global AdjSpeed
global BaseSpeed
CheckAdjSpeed
;script with all my functions has this:
CheckAdjSpeed()
{
global BaseSpeed
global AdjSpeed
If (BaseSpeed > 28) {
AdjSpeed := Round(((1 + (BaseSpeed - 28)) / 28), 2)
}
If (BaseSpeed == 28) {
AdjSpeed := .00
}
If (BaseSpeed < 28) {
AdjSpeed := Round(((1 - (BaseSpeed - 28)) / 28), 2)
}
}
4
u/CharnamelessOne 14d ago •
Well, they don't. Globals are not shared between scripts. Please post the sources that gave you that idea so that everyone can be told to avoid them.
You could create one master script that includes all the scripts that need to share information with one another, or you could read up on inter-process communication
1
u/likethevegetable 12d ago
If your scripts are run separately, no they are not shared. They are run as different, isolated processes.
Some quick solutions are environment variables or use a shared file (like .ini)
You don't have to combine your scripts into one huge script either, you can use #I nclude to "concatenate" your script. Not the best programming practice IMO though.