Autohotkey – problems when using if, else if statements inside Functions

autohotkey

I have problems when using if, else if statements inside Functions they don't recognize my vars, despite I have set them to global at the beginning of the script…

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
}

this function above is working.

My hole script runs this function above in order to pass the value of AR4psetCollCur*NameOfTheTool* (current selected tool) to AR4psetCollCurY
then modifies AR4psetCollCurY value and use it as parameter of another function, and then it should store back the modified value of AR4psetCollCurY into AR4psetCollCur*NameOfTheTool*
that's why I use this function below, but it doesn't work, it always sees %AR4toolCur% as empty, why is that?

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
}

Thanks Advanced.

Best Answer

I have problems when using if, else if statements inside Functions they don't recognize my vars, despite I have set them to global at the beginning of the script...

You believe that the problem is to do with if/else if statements, however the evidence points to problems with local/global variables.

You need to define a variable as global, within a function in order to refer to the global version of the variable (rather than the local version of the variable).

Example code:

^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
}

Note:
The 'if' lines should be of the form:

If (AR4toolCur = "Selection")

not:

If (%AR4toolCur% := "Selection")
Related Question