Solarplex
Solarplex

Reputation: 107

Passing an Array through Function Then Looping

I am writing a function that would loop imagesearch but I am having trouble figuring out how to pass a dynamic variable with the options allowed with Arrays (Such as Array0 which retrieves the total count of records in array, and Array%A_Index% which when used with a Loop displays each name as it goes through the list)

arrowList = C:\AHK\LeftArrow.png|C:\AHK\LeftArrow1.png|C:\AHK\GreenLeftArrow.png
StringSplit, arrowArray, arrowList, |
buildList = C:\AHK\build1.png|C:\AHK\build2.png|C:\AHK\build3.png|C:\AHK\build4.png|C:\AHK\build5.png
StringSplit, buildArray, buildList, |


SearchArray("arrowArray","buildArray")


SearchArray(ByRef x, ByRef y) 
{
Loop, %x%
{
    x2get := %xA_Index%
    ImageSearch, imageX, imageY, 0, 0, A_ScreenWidth, A_ScreenHeight, *25 %x2get%
    tooltip, searching for %x2get% , 0, 0
    If ErrorLevel = 0
    {
        Loop, % y%0%
        {
            y2get := % y%A_Index%
            ImageSearch, imageX, imageY, 0, 0, A_ScreenWidth, A_ScreenHeight, *25 %y2get%
            tooltip, searching for %y2get% , 0, 0
            If ErrorLevel = 0
            {
                MouseClick, Left, imageX, imageY, 
                Sleep 1000
            }
        }
    }
}   
}

Upvotes: 0

Views: 1469

Answers (1)

Sid
Sid

Reputation: 1719

You have some problems with how you're calling the variables. You're not actually using "real" arrays there, you're using what's called "pseudo-arrays". You can read about them in the documentation, here.

They're an old way AHK handled arrays, and I strongly suggest you try to move over to using "real" arrays in AHK. You should also update your version of AHK to the latest if you haven't already - http://ahkscript.org/download/.

I changed how the script called some variables, and it should work now, try this, note I've commented the lines I changed:

arrowList = C:\AHK\LeftArrow.png|C:\AHK\LeftArrow1.png|C:\AHK\GreenLeftArrow.png
StringSplit, arrowArray, arrowList, |
buildList = C:\AHK\build1.png|C:\AHK\build2.png|C:\AHK\build3.png|C:\AHK\build4.png|C:\AHK\build5.png
StringSplit, buildArray, buildList, |

SearchArray("arrowArray", "buildArray")

SearchArray(ByRef x, ByRef y) 
{
    Loop, % %x%0 ; Changed this line
    {
        x2get := %x% A_Index ; Changed this line
        ImageSearch, imageX, imageY, 0, 0, %A_ScreenWidth%, %A_ScreenHeight%, *25 %x2get% ; Changed this line
        tooltip, searching for %x2get% , 0, 0
        If ErrorLevel = 0
        {
            Loop, % %y%0 ; Changed this line
            {
                y2get := % %y% A_Index ; Changed this line
                ImageSearch, imageX, imageY, 0, 0, %A_ScreenWidth%, %A_ScreenHeight%, *25 %y2get%  ; Changed this line
                tooltip, searching for %y2get% , 0, 0
                If ErrorLevel = 0
                {
                    MouseClick, Left, %imageX%, %imageY% ; Changed this line
                    Sleep 1000
                }
            }
        }
    }   
}

If you're interested in how a solution using "real" arrays would look, here's an example of that. Just make sure you're running the latest version of AHK before you try it, otherwise it might fail.

arrowList := "C:\AHK\LeftArrow.png|C:\AHK\LeftArrow1.png|C:\AHK\GreenLeftArrow.png"
arrowArray := StrSplit(arrowList, "|")
buildList := "C:\AHK\build1.png|C:\AHK\build2.png|C:\AHK\build3.png|C:\AHK\build4.png|C:\AHK\build5.png"
buildArray := StrSplit(buildList, "|")

SearchArray(arrowArray, buildArray)

SearchArray(firstArray, secondArray) {

    ; Iterate through the first array
    for outerIndex, outerValue in firstArray {
        ; outerIndex = Index of the current element; 1, 2, etc...
        ; outerValue = The value of the string at that index

        Tooltip, Searching for %outerValue%, 0, 0
        ImageSearch, imageX, imageY, 0, 0, %A_ScreenWidth%, %A_ScreenHeight%, *25 %outerValue%
        if (ErrorLevel = 0) {

            ; Iterate through the second array
            for innerIndex, innerValue in secondArray {
                ; innerIndex = Index of the current element; 1, 2, etc...
                ; innerValue = The value of the string at that index

                Tooltip, Searching for %innerValue%, 0, 0
                ImageSearch, imageX, imageY, 0, 0, %A_ScreenWidth%, %A_ScreenHeight%, *25 %innerValue%
                if (ErrorLevel = 0) {
                    MouseClick, Left, %imageX%, %imageY%
                    Sleep, 1000
                }
            }
        }
    }
}

Upvotes: 3

Related Questions