Error: This dynamic variable is blank. If this variable was not intended to be dynamic, remove the % symbols from it. (AHK)

Error: This dynamic variable is blank. If this variable was not intended to be dynamic, remove the % symbols from it. (AHK)

我在使用 #include 时遇到了问题。我正在使用此文档的语法:docs。我有一个简单的测试程序,其中包含 vars.ahk 文件。

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

我的目标是在 msgbox 中显示 vars.ahk 文件中的变量,但它不起作用。当我 运行 此代码时出现错误。如果我尝试将 vars.ahk 或 my_ahk_program.ahk 中的任何变量定义为全局变量而不是将它们传递给函数,则 msgbox 将显示为没有值。如何让 #include 使用变量?提前致谢!

您在现代表达式语句中使用了旧语法。
通过将变量包装在 % 中来引用变量是遗留 AHK 中的正确方法。
但是,当您使用 := or a function 时,您并不是在编写遗留 AHK,而是想使用现代表达式语法。

因此,如警告消息所示,放弃 %s,一切都会好起来的。
除了在您的 MsgBox 中,您可以使用旧语法,因为命令是一种旧语法,默认情况下任何命令中的每个参数都使用旧语法(除非文档中另有说明)。
当然,您也可以(应该?)放弃 MsgBox 命令中的旧语法,方法是强制参数以单个 % 开始,然后是 space 来评估表达式。像这样:
MsgBox, % "the value is " newvar

我之前的回答,了解更多关于使用 %%% .

的信息