Wait function Topic is solved

Get help with using AutoHotkey and its commands and hotkeys
Chumlee
Posts: 6
Joined: 23 Jul 2019, 06:46

Wait function

Post by Chumlee » 23 Jul 2019, 13:13

Hello everyone,
I'm trying to make a function based from MasterFocus' "WaitPixelColor" and I am wondering if there is a way to simplify this.

Code: Select all

WaitPixelColor(p_DesiredColor,p_PosX,p_PosY,p_TimeOut=0,p_GetMode="",p_ReturnColor=0) {
    l_Start := A_TickCount
    Loop {
        PixelGetColor, l_OutputColor, %p_PosX%, %p_PosY%, %p_GetMode%
        If ( ErrorLevel )
        PixelGetColor, l_OutputColor, %p_PosX%+2, %p_PosY%, %p_GetMode%
        If ( ErrorLevel )
        PixelGetColor, l_OutputColor, %p_PosX%+4, %p_PosY%, %p_GetMode%
        If ( ErrorLevel )
        PixelGetColor, l_OutputColor, %p_PosX%+6, %p_PosY%, %p_GetMode%
        If ( ErrorLevel )
        PixelGetColor, l_OutputColor, %p_PosX%+8, %p_PosY%, %p_GetMode%
        If ( ErrorLevel )
        PixelGetColor, l_OutputColor, %p_PosX%+10, %p_PosY%, %p_GetMode%
        If ( ErrorLevel )
            Return ( p_ReturnColor ? l_OutputColor : 1 )
        If ( l_OutputColor = p_DesiredColor )
            Return ( p_ReturnColor ? l_OutputColor : 0 )
        If ( p_TimeOut ) && ( A_TickCount - l_Start >= p_TimeOut )
            Return ( p_ReturnColor ? l_OutputColor : 2 )
    }
}
Last edited by Chumlee on 24 Jul 2019, 06:40, edited 1 time in total.

User avatar
evilC
Posts: 4787
Joined: 27 Feb 2014, 12:30

Re: Wait function

Post by evilC » 24 Jul 2019, 06:11

All those ERRORLEVEL checks make little sense to me.
ERRORLEVEL is only likely to be true if you do something like checking colour of a pixel that is not a valid coordinate.
It's nothing to do with whether the pixel is the colour you are look for, for example, because that's not what PixelGetColor does...

Chumlee
Posts: 6
Joined: 23 Jul 2019, 06:46

Re: Wait function

Post by Chumlee » 24 Jul 2019, 06:40

I need it to function like it a finish line at a race. Once the pixel color crosses the row, I need it to continue. I am fairly new to scripting.

User avatar
evilC
Posts: 4787
Joined: 27 Feb 2014, 12:30

Re: Wait function

Post by evilC » 24 Jul 2019, 07:18

Most of the PixelGetColor commands will do nothing
PixelGetColor, l_OutputColor, %p_PosX%, %p_PosY%, %p_GetMode% does not in any way check if a certain pixel is a certain colour, all it does is get the colour of a pixel at a certain coordinate and store it in a variable.
The lines below...

Code: Select all

        If ( ErrorLevel )
        PixelGetColor, l_OutputColor, %p_PosX%+2, %p_PosY%, %p_GetMode%
... will only do anything if there was an ERROR (eg p_PosX is a letter not a number, or is off the edge of the screen)
They absolutely do not say "If the pixel was not the colour we are looking for"

The line at the end checks if the colour found was the one we are looking for
If ( l_OutputColor = p_DesiredColor )

Chumlee
Posts: 6
Joined: 23 Jul 2019, 06:46

Re: Wait function

Post by Chumlee » 24 Jul 2019, 12:13

Is there a way to make this WaitPixelSearch? The same function but with a search ?


Chumlee
Posts: 6
Joined: 23 Jul 2019, 06:46

Re: Wait function

Post by Chumlee » 24 Jul 2019, 12:32

I really appreciate the help. I need it to function like this, loop the search on the timer.

Value Description
0 The desired color was found
1 There was a problem during PixelSearch
2 The function timed out

https://github.com/MasterFocus/AutoHotkey/blob/master/Functions/WaitPixelColor/README.md
Last edited by Chumlee on 25 Jul 2019, 07:41, edited 2 times in total.

User avatar
evilC
Posts: 4787
Joined: 27 Feb 2014, 12:30

Re: Wait function

Post by evilC » 24 Jul 2019, 12:47

You can return whatever you like from your own function, and these values do not need to be the same as the ERRORLEVEL codes from PixelSearch
Your PixelSearch seems wrong though, surely instead of l_OutputColor you want p_DesiredColor?
As you have a bunch of parameters for the function anyway, may as well add the one for variation too instead of hard-coding it to 0. You may need some variation, it may not always be identical
And the mode almost always wants to be RGB. You may or may not want fast mode (Note it flips the direction of scanning)

Chumlee
Posts: 6
Joined: 23 Jul 2019, 06:46

Re: Wait function

Post by Chumlee » 24 Jul 2019, 13:02

So like this? I'm super lost at this point. :headwall:
Last edited by Chumlee on 25 Jul 2019, 07:39, edited 1 time in total.

User avatar
evilC
Posts: 4787
Joined: 27 Feb 2014, 12:30

Re: Wait function

Post by evilC » 24 Jul 2019, 14:15

Your first if just checks if ErrorLevel is "true", which both 1 (Found) and 2 (Error) are
The second IF check is also always true as you check if something is itself: If ( p_DesiredColor = p_DesiredColor )

I think you want

Code: Select all

        If ( ErrorLevel == 2 ) ; Error
            Return ( p_ReturnColor ? p_DesiredColor  : 1 )
        If (  ErrorLevel == 1 ) ; Found
            Return ( p_ReturnColor ?  p_DesiredColor  : 0 )
        If ( p_TimeOut ) && ( A_TickCount - l_Start >= p_TimeOut )
            Return ( p_ReturnColor ? p_DesiredColor  : 2 )

Chumlee
Posts: 6
Joined: 23 Jul 2019, 06:46

Re: Wait function

Post by Chumlee » 24 Jul 2019, 15:02

So basically this image https: // ibb. co/ GQZ6dMF will sometimes appear and scroll upward when you complete a task. I just want the script use the function to wait for the little man (0#010000) to appear, then proceed if he (0x010000) was found or go back if he wasn't found in the allotted time. Currently it isn't waiting now. It's just moving past. Also RGB mode seemed too slow to find him at all.

Code: Select all

Goto, wait2

wait2:
Icon2 := WaitPixelSearch(0x010000, 475, 170, 495, 180, 7500)
If (Icon2 = 0)
{
   Goto, Forward
}
Else If Icon2 = 1)
{
   MsgBox There was a problem
}
Else If (Icon2 = 2)
{
	Goto, Backward
}
Last edited by Chumlee on 25 Jul 2019, 07:39, edited 1 time in total.


Post Reply

Return to “Ask For Help”