我在函数中使用语句时遇到问题if
,else if
它们无法识别我的变量,尽管我在脚本开头将它们设置为全局......
global AR4toolCur :=
global AR4psetCollCurY :=
global AR4psetCollCurYselect :=
global AR4psetCollCurYfill :=
global AR4psetCollCurYeraser :=
global AR4psetCollCurYknife :=
~l::
AR4toolCur := "Selection" ; means the current selected tool
return
AR4psetCollCurYperToolSelFn() ; Sel means Select, I want to pass the value of AR4psetCollCur*NameOfTheTool* to AR4psetCollCurY
{
msgbox, AR4psetCollCurYperToolSelFn will run`nAR4toolCur = %AR4toolCur%
If (%AR4toolCur% := "Selection") ; this pass the value of AR4psetCollCur*NameOfTheTool* (in this case "select") to AR4psetCollCurY
AR4psetCollCurY := AR4psetCollCurYselect
Else If (%AR4toolCur% := "Fill")
AR4psetCollCurY := AR4psetCollCurYfill
Else If (%AR4toolCur% := "Eraser")
AR4psetCollCurY := AR4psetCollCurYeraser
Else If (%AR4toolCur% := "Knife")
AR4psetCollCurY := AR4psetCollCurYknife
msgbox, AR4psetCollCurY = %AR4psetCollCurY%
return
}
上述功能正在运行。
我的洞脚本运行上面的这个函数以便将AR4psetCollCur*NameOfTheTool*
(当前选定的工具)的值传递给AR4psetCollCurY
然后修改AR4psetCollCurY
值并将其用作另一个函数的参数,然后它应该将修改后的值存储回去,这就是我下面使用这个函数的AR4psetCollCurY
原因AR4psetCollCur*NameOfTheTool*
,但它不起作用,它总是被视为%AR4toolCur%
空的,为什么呢?
AR4psetCollCurYperToolStrFn()
{
msgbox, AR4psetCollCurYperToolStrFn will run`nAR4toolCur = %AR4toolCur%
If (%AR4toolCur% := "Selection")
AR4psetCollCurYselect := AR4psetCollCurY
Else If (%AR4toolCur% := "Fill") ; I have tried AR4toolCur = Fill it doesn't work either
{
AR4psetCollCurYfill := AR4psetCollCurY
msgbox, AR4psetCollCurYfill = %AR4psetCollCurYfill%
}
Else If (%AR4toolCur% := "Eraser")
AR4psetCollCurYeraser := AR4psetCollCurY
Else If (%AR4toolCur% := "Knife")
AR4psetCollCurYknife := AR4psetCollCurY
return
}
谢谢高级。
答案1
我在函数中使用 if、else if 语句时遇到问题,它们无法识别我的变量,尽管我在脚本开头已将它们设置为全局变量……
您认为问题与 if/else if 语句有关,但证据表明问题与局部/全局变量有关。
您需要在函数内将变量定义为全局变量,以便引用该变量的全局版本(而不是该变量的本地版本)。
示例代码:
^q::
vText := "hello" ;the global version of vText
Func1()
Func2()
Func3()
Return
Func1()
{
MsgBox %vText% ;the function's local version of vText is blank
Return
}
Func2()
{
global vText
MsgBox %vText% ;the function uses the global version of vText
Return
}
Func3()
{
global
MsgBox %vText% ;the function uses the global versions for all variables
Return
}
注意:
'if' 行应采用以下形式:
If (AR4toolCur = "Selection")
不是:
If (%AR4toolCur% := "Selection")