Reputation: 27
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!
Upvotes: 1
Views: 1024
Reputation: 6489
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 %
.
Upvotes: 1