How can I do multiple imageSearches using a function parameter

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Kriss
Posts: 3
Joined: 25 Jul 2017, 23:07

How can I do multiple imageSearches using a function parameter

25 Jul 2017, 23:19

I am trying to use imagesearch to buy certain items from an item shop in a game. There are 8 slots therefore I need to loop it 8 times. There are 4-5 different items I need to buy if they appear.

This is the function I use to do the buying:

Code: Select all

marketFunc(item)
{
    Loop, 8
    {
        ImageSearch, OutputVarX, OutputVarY, 0, 0, %A_ScreenWidth%, %A_ScreenHeight%, item
        if !ErrorLevel
        {
            outY = %OutputVarY%
            outY += 119
            MouseMove, %OutputVarX%, %OutY%
            Sleep, 250
            Click
            Sleep, 750
            MouseClick, left,  797,  456
            Sleep, 750
            MouseClick, left,  687,  473
            Sleep, 100
        }
        Sleep, 250
    }
}
I've then been calling the function like this:

Code: Select all

marketfunc(chip.png)
sleep, 500
marketfunc(ticket.png)
sleep, 500
marketfunc(scroll.png)
sleep, 500
marketfunc(masterScroll.png)
sleep, 500
marketfunc(otherItemHere.png)
I know I can do it without the function like this:

Code: Select all

Loop, 8
{
    ImageSearch, OutputVarX, OutputVarY, 0, 0, %A_ScreenWidth%, %A_ScreenHeight%, ticket.png
    if !ErrorLevel
    {
        outY = %OutputVarY%
        outY += 119
        MouseMove, %OutputVarX%, %OutY%
        Sleep, 250
        Click
        Sleep, 750
        MouseClick, left,  797,  456
        Sleep, 750
        MouseClick, left,  687,  473
        Sleep, 100

    }
    Sleep, 250

    ImageSearch, OutputVarX, OutputVarY, 0, 0, %A_ScreenWidth%, %A_ScreenHeight%, chip.png
    if !ErrorLevel
    {
        outY = %OutputVarY%
        outY += 119
        MouseMove, %OutputVarX%, %OutY%
        Sleep, 250
        Click
        Sleep, 750
        MouseClick, left,  797,  456
        Sleep, 750
        MouseClick, left,  687,  473
        Sleep, 100

    }
    Sleep, 250

    ImageSearch, OutputVarX, OutputVarY, 0, 0, %A_ScreenWidth%, %A_ScreenHeight%, scroll.png
    if !ErrorLevel
    {
        outY = %OutputVarY%
        outY += 119
        MouseMove, %OutputVarX%, %OutY%
        Sleep, 250
        Click
        Sleep, 750
        MouseClick, left,  797,  456
        Sleep, 750
        MouseClick, left,  687,  473
        Sleep, 100

    }
    Sleep, 250

    ImageSearch, OutputVarX, OutputVarY, 0, 0, %A_ScreenWidth%, %A_ScreenHeight%, masterScroll.png
    if !ErrorLevel
    {
        outY = %OutputVarY%
        outY += 119
        MouseMove, %OutputVarX%, %OutY%
        Sleep, 250
        Click
        Sleep, 750
        MouseClick, left,  797,  456
        Sleep, 750
        MouseClick, left,  687,  473
        Sleep, 100

    }
    Sleep, 250

    ImageSearch, OutputVarX, OutputVarY, 0, 0, %A_ScreenWidth%, %A_ScreenHeight%, otherItemHere.png
    if !ErrorLevel
    {
        outY = %OutputVarY%
        outY += 119
        MouseMove, %OutputVarX%, %OutY%
        Sleep, 250
        Click
        Sleep, 750
        MouseClick, left,  797,  456
        Sleep, 750
        MouseClick, left,  687,  473
        Sleep, 100

    }
    Sleep, 250
}
But I don't like the looks of it, and it feels too hard code-y.

When I use the function "method" it does not find the images. However when I do it using the example below it works fine. So I assume I've set the function parameters wrong somehow.

Any help would be appriciated.
User avatar
Exaskryz
Posts: 2882
Joined: 17 Oct 2015, 20:28

Re: How can I do multiple imageSearches using a function parameter

26 Jul 2017, 00:39

In your function, I believe you need to be using %item% as the last parameter in the ImageSearch.

Additionally, I believe you need to call the functions using marketfunc("chip.png") and such.

If that doesn't quite fix it, try some diagnostics like finding out what the ErrorLevel is for ImageSearch with MsgBox % ErrorLevel and/or using MsgBox on other variables.
Kriss
Posts: 3
Joined: 25 Jul 2017, 23:07

Re: How can I do multiple imageSearches using a function parameter

26 Jul 2017, 00:56

Exaskryz wrote:In your function, I believe you need to be using %item% as the last parameter in the ImageSearch.

Additionally, I believe you need to call the functions using marketfunc("chip.png") and such.

If that doesn't quite fix it, try some diagnostics like finding out what the ErrorLevel is for ImageSearch with MsgBox % ErrorLevel and/or using MsgBox on other variables.
Thank you, that worked.

Code: Select all

	marketFunc("chip.png")
	marketFunc("ticket.png")
	marketFunc("basicScroll.png")
	marketFunc("heroicScroll.png")

Loop, 8
{
	ImageSearch, OutputVarX, OutputVarY, 0, 0, %A_ScreenWidth%, %A_ScreenHeight%, %item%
	if !ErrorLevel
	{
		outY = %OutputVarY%
		outY += 119
		MouseMove, %OutputVarX%, %OutY%
		Sleep, 250
		Click
		Sleep, 750
		MouseClick, left,  797,  456
		Sleep, 750
		MouseClick, left,  687,  473
		Sleep, 100

	}
	Sleep, 250
}

	
BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: How can I do multiple imageSearches using a function parameter

26 Jul 2017, 02:42

Thank you, that worked.
Let's doubt this, as in this code you're using a function call, without setting a (receiving) function. Correct me if I'm wrong :)
Spoiler

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: filipemb, ShatterCoder, Xaucy and 177 guests