0

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.

  • Variable names in an expression are not enclosed in percent signs. Try If (AR4toolCur = "Selection"), if "Selection" is a literal string. – user3419297 Dec 1 '16 at 12:06
  • it's funny, but without the percepnt signs it didn't work, I tried it first. I have seen on google something about ByRef what is it used for?? – litu16 Dec 1 '16 at 14:02
  • is it a problem if I have 16 else if statements? both functions have 17 if statements each, is it ok?? – litu16 Dec 1 '16 at 14:29
  • it seems that somehow ahk didn't allow me to have 16 else if statetemts one after the other, I had to change them to if statements all, put them curly braces an return in each of them, and change the sentence to If (AR4toolCur = "Selection") only then (with all that together) worked. Thanks user3419297 – litu16 Dec 1 '16 at 15:17
  • btw in the msgbox, var1 = %var1%'nvar2 = %var2%'n and so on 17 times is it possible to write the code (parameters) of the msgbox in multiples lines? I mean in the code itself (not only in the msgbox that will be displayed, but in the code itself) cuz its very disturbing and confusing use the 'n all togheter with the variables, thanks advanced. – litu16 Dec 1 '16 at 18:33
0

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")
| improve this answer | |
  • man, but I set the to global, sorry I don't know how to close a question, actually I solved while ago by using just if statements, not a bunch of 13 else if statements. thanks anyway man, thanks – litu16 Jan 4 '17 at 14:46
  • -- You could answer the question yourself, and then click the tick to accept that as the answer. -- Or you could accept this answer, if it answers your question. -- This global/local issue really confused me when I was a beginner, so I provide information generally for anyone who might benefit from it. – vafylec Jan 4 '17 at 18:01
  • yes man, many thanks, but I stated in the question at the beginning that I did set the variables as global in the auto executable part of the script (top part of the script). Currently I'm getting hell with this issue superuser.com/questions/1149519/… hope you could spot it out, please look at the last edited part of the question. Thanks Advanced. – litu16 Jan 4 '17 at 18:10
  • Look at Func2, it may be that you need to set the variables as global within the context of the function, in order to access the global version of the variable. Compare Func2 and Func1. – vafylec Jan 4 '17 at 18:27
  • -- I've proposed a solution to your alt+space+q problem, at the url above, I think it works pretty well. -- Btw I strongly suggest that you observe the difference between Func1() and Func2(). -- (E.g. you can set vText as a global variable in the auto-execute section, but the function reads a local version of vText, unless it is told by the line global vText to read the global version of vText.) – vafylec Jan 4 '17 at 20:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.