1
votes

I have created a small function that clicks a certain portion of the screen. It will be used but clicked a different number of times throughout the program. I would like to be able to input the amount of clicks as a parameter, however, when I try to do this rather than hard coding the amount of loops that part of the code does not run and the screen does not get clicked.

Note changes to function ClickMainBuilding in definition and as it's called below

Here is what works:

; ######## SETTINGS ########
#SingleInstance Force
#NoEnv
SetWorkingDir %A_ScriptDir%
CoordMode, Mouse, Window
SendMode Input
SetTitleMatchMode 2
#WinActivateForce
SetControlDelay 1
SetWinDelay 0
SetKeyDelay -1
SetMouseDelay -1
SetBatchLines -1

; ######## FUNCTIONS ########

ClickMainBuilding(){
    Loop, 5 
    {
        Click, 927, 538 Left, , Down
        Sleep, 250
        Click, 927, 538 Left, , Up
        Sleep, 250
    }
}

ClickTradeDepot(){
    Click, 420, 705
    Sleep, 750
}

ExitTradeDepot(){
    Click, 1512, 129
    Sleep, 750
}

^!u:: ; ### MAIN ###

WinActivate,  ahk_class ThumbnailDeviceHelperWnd
Sleep, 333
WinActivate, BlueStacks ahk_class HwndWrapper[Bluestacks.exe;;5b32fde4-2355-48c5-be51-8927697e9914]
Sleep, 500
ClickMainBuilding()
Sleep, 1000
ClickTradeDepot()
Sleep, 1000
ExitTradeDepot()

Return

Esc::ExitApp  ; Exit script with Escape key

Here is what doesn't:

; ######## SETTINGS ########
#SingleInstance Force
#NoEnv
SetWorkingDir %A_ScriptDir%
CoordMode, Mouse, Window
SendMode Input
SetTitleMatchMode 2
#WinActivateForce
SetControlDelay 1
SetWinDelay 0
SetKeyDelay -1
SetMouseDelay -1
SetBatchLines -1

; ######## FUNCTIONS ########

ClickMainBuilding(x){
    Loop, x
    {
        Click, 927, 538 Left, , Down
        Sleep, 250
        Click, 927, 538 Left, , Up
        Sleep, 250
    }
}

ClickTradeDepot(){
    Click, 420, 705
    Sleep, 750
}

ExitTradeDepot(){
    Click, 1512, 129
    Sleep, 750
}

^!u:: ; ### MAIN ###

WinActivate,  ahk_class ThumbnailDeviceHelperWnd
Sleep, 333
WinActivate, BlueStacks ahk_class HwndWrapper[Bluestacks.exe;;5b32fde4-2355-48c5-be51-8927697e9914]
Sleep, 500
ClickMainBuilding(5)
Sleep, 1000
ClickTradeDepot()
Sleep, 1000
ExitTradeDepot()

Return

Esc::ExitApp  ; Exit script with Escape key
1
Change Loop, x with Loop, % xYane

1 Answers

2
votes

Loop takes as it's first parameter the amount of times it needs to be iterated. If you put 5, then AHK converts this 5 to an integer, and then iterates 5 times.

In your code, you wrote Loop, x, which tells AHK to loop a total of x times. However unlike 5, x can not be converted to an integer, so AHK will probably throw an error. In order to specify the variable x, and instead use it's value as the amount of iterations, you need to specify that this specific x is actually a variable. You can do this like so:

; BOTH OF THESE EXAMPLES WORK

; Legacy
Loop, %x%

; Newest
Loop, % x