1

I am having troubles with #include. I am using this documentation for syntax: docs. I have a simple test program which includes the vars.ahk file.

vars.ahk:

; Test vars
global x1 := 1
global x2 := 2

my_ahk_program.ahk:

#include C:\Users\user\Desktop\vars.ahk

function(x, x1, x2) {

    ; global x1, x2

    If (x=1) {
        newvar := %x1%
    }
    else if (x=2) {
        newvar := %x2%
    }
    msgbox, the value is %newvar% 
}

function(1, %x1%, %x2%)

msgbox, finished

My goal is to display the variable from the vars.ahk file in a msgbox, but it is not working. I get the error when I run this code. If I try to define any of the variables in vars.ahk or my_ahk_program.ahk as global instead of passing them into the function, the msgbox will show up with no value. How can I get #include to work with variables? Thanks ahead of time!

1 Answers1

1

You're using legacy syntax in a modern expression statement.
Referring to a variable by wrapping it in % is the correct way in legacy AHK.
But when you're using e.g := or a function you're not writing legacy AHK and you want to use the modern expression syntax.

So ditch the %s, as the warning messages suggests, and it'll be fine.
Except in your MsgBox you're fine to use legacy syntax, since commands are kind of legacy and every parameter in any command uses legacy syntax by default (unless otherwise specified in the documentation).
Of course you could (should?) ditch the legacy syntax in the MsgBox command as well by forcing the parameter to evaluate an expression by start it off with a single % followed up by a space. Like so:
MsgBox, % "the value is " newvar

Here's a previous answer of mine for some more info on using using %% or % .

0x464e
  • 5,948
  • 1
  • 12
  • 17
  • This worked! Thank you so much. I did not realize there was a v1 and v2 for autohotkey! – AHK New Programmer Jun 27 '20 at 08:28
  • Not sure if you accidentally said v1 and v2, or found that from Google. But there indeed is a v2. It's still in alpha stage. What we're writing here is strictly v1. v1 includes both the legacy syntax, and the newer expression syntax. v2 doesn't have legacy syntax at all. So I guess that's another reason to fully ditch the legacy syntax. – 0x464e Jun 27 '20 at 23:30
  • Oops, I thought by legacy, you meant v1 syntax but now I'm realizing legacy syntax is something different. Something to do with forcing commands. After your post, I realized that the docs have an option to view the v1 or v2 syntax, so I confused the two. Turns out there is a lot I still don't understand/know about autohotkey! – AHK New Programmer Jun 28 '20 at 05:39