AHK  New Programmer
AHK New Programmer

Reputation: 27

AHK code issue - Importing and defining variables in AHK?

I'm pretty new to AHK. I just started like a couple days ago, and I've hit a stump and need help debugging.

I would like this code to go to a specified mouse position and click the coordinate, and import my variables since I will have many AHK scripts referring to the same variables.

PROBLEM: The code currently does not move the mouse and click in the desired location. I am not even sure if it is clicking. It simply displays the ToolTip message as well as the msgbox message. (The messages that will display are marked in the code as working via [works]). I think this might be because the variables are somehow not defined... When the variable displayed in the msgbox was defined in the if/else statement, the msgbox would be empty. The Clicks aren't working either, and I think it's because the values aren't existent. I have no idea why in the world this is happening.

What is wrong with my code and what is going on? Please help me!

#include C:\Users\username\Desktop\vars.txt
CoordMode, Mouse, Screen
clickThing(whichThing) { ;START FUNCTION
    Sleep, 1000 ; sleep 1 sec
    Send ^h ; control h
    Sleep, 3000 ; sleep 3 sec
    if (whichThing = 1){
        goTox = %x1%
        goToy = %y1%
        ToolTip, option 1 ; display "option 1" @ cursor tip ; [works]
    } else if (whichThing = 2) {
        goTox = %x2%
        goToy = %y2%
        ToolTip, option 2 ; display "option 2" @ cursor tip ; [works]
    } else if (whichThing = 3) {
        goTox = %x3%
        goToy = %y3%
        ToolTip, option 3; display "option 3" @ cursor tip ; [works]
    } else {
        msgbox that isn't a valid choice. ; [works]
        return
    }
    msgbox %goTox% %goToy% ; msgbox will be empty...
    ; I tried this
    Sleep 1000
    MouseMove, %goTox%, %goToy%, 30
    MouseClick, left, %goTox%, %goToy%
    Sleep 500
    MouseClick, left, %goTox%, %goToy%
    Sleep 1000
    ; I also tried this
    Click %goTox%, %goToy%
} ;END OF FUNCTION

;START MAIN CODE
IfWinActive, window_name {
    clickThing(1)}
else{
    msgbox, window not active ; [works]
}

msgbox, finished ; [works]
return

vars.ahk:

; Test variables
x1 := 1
x2 := 2
x3 := 3
y1 := 4
y2 := 5
y3 := 6

EDIT: Turns out my variable is empty. Why is it empty? This code outputs the correct value

#include C:\Users\user\Desktop\vars.txt
msgbox %x1%

but as soon as as I try to assign to a variable, the msgbox is empty and I get an error that the variable is empty.

#include C:\Users\user\Desktop\vars.txt
newvar = %x1%
msgbox = %newvar% ; gives an error that the var is empty

Upvotes: 0

Views: 429

Answers (1)

576i
576i

Reputation: 8372

Your function needs the global statement for each global you want to access inside of it.

Like this

clickThing(whichThing) { ;START FUNCTION


    global x1
    global x2
    global x3
    global y1
    global y2
    global y3

    Sleep, 1000 ; sleep 1 sec

    ... the rest of your code here

If you still have problems, please provide the content of the "#include C:\Users\user\Desktop\vars.txt" file, without it, your example is not reproducable. (Since you're new to Stack Overflow: Please note that one of the requirement of asking a question is to provide a full example...)

Upvotes: 0

Related Questions