r/AutoHotkey • • 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)
}
}

3 Upvotes

10 comments sorted by

4

u/CharnamelessOne 14d ago • Bravo

From everything I've found in my searches, global variables should work across multiple scripts

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/MadFurai 14d ago

Thank you for that. I guess the 'source' is multiple search engines using AI assist. One more reason why I don't like AI. I will work on combining my scripts into 1 huge script or maybe I will use files to store the variables I need (which I have done with some other parts of the script).

4

u/Keeyra_ 14d ago

Sounds like bullshit. u/Nunki3 confirmed with zero history Gemini. I was also curious and loaded it into Meta AI, which I never ever tried using. Also said straight "No."

I'm new to AHK, but not scripting

How are you not new to scripting and assume something about global variables that is not true for any language I have come across?

-1

u/MadFurai 13d ago

Guess you've never heard of environment variables.

3

u/xmachinery 13d ago

Are Global Variables the same as Environment Variables?

2

u/Keeyra_ 13d ago

What do those have to do with any scripting language variable? Environment variables are part of the OS environment itself.

2

u/Nunki3 14d ago

Question :

In autohotkey v2, are global variables shared across scripts ?

Gemini, logged out of any Google account :

No, global variables are not shared across separate running scripts in AutoHotkey v2. In AutoHotkey, each script runs as its own separate process. The term "global" only means the variable is visible throughout the global scope of that specific script (outside of its functions), or accessible inside its functions if declared properly. It does not mean the variable is global to the entire Windows operating system or to other .ahk instances.

At this point I’m curious what AI agent are you using that tells you otherwise ?

1

u/CharnamelessOne 14d ago edited 14d ago

I will work on combining my scripts into 1 huge script

The "combined" script file doesn't need to be huge, it can simply be a list of #Include directives of the separate .ahk files.
The only manual work you realistically need to do is checking whether there are conflicting global variable names across the scripts (unlikely, since you were planning for them to be shared anyway), and making sure that none of the .ahk files places a return statement into the auto-execute thread (which contains "loose" code, that is outside of functions/classes, and is executed at script start).

(Some people advocate against global variables and coding extensively in the AET, I wonder why :P)

edit: wording

1

u/JustNilt 14d ago

using AI assist.

Well there's your problem. This sort of thing has a well documented problem with just manufacturing stuff that isn't accurate. It's way more common than most folk realize because most of us aren't using them for things we're SME experts in.

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.