Help please

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Phasecoder
Posts: 7
Joined: 19 Sep 2017, 12:27

Help please

19 Sep 2017, 12:32

I am totaly new here I know basics etc

I am trying to make a script loop pressing f5 untill and image is found and then to pause untill the image is not found and conntinue?

Can someone please give me the basics to go off

thanks
User avatar
Exaskryz
Posts: 2882
Joined: 17 Oct 2015, 20:28

Re: Help please

19 Sep 2017, 17:47

Use a Loop and Block. And use ImageSearch (and Sleep). Use If and the variable ErrorLevel, which is set by ImageSearch to a "success" value of 0 or a "failure" value of 1 or 2. If you use If ErrorLevel, any actions you associate with that will be for when the image is not found; for you that is Send {F5}. If the image is found, use either else or If !ErrorLevel where the ! (logical-not operator) inverts true statements to false and false statements to true; if the image is found, ErrorLevel is 0. If 0 is false, and If !0 is true. So when the image is found, you want it to just do like a Sleep 500 until trying again.

With If/else statements, if you'd like to take more than one action, wrap those actions in Blocks like you would for the Loop.

An example of all this put together, although you'd change the actions, should be at the bottom of the ImageSearch documentation.
Phasecoder
Posts: 7
Joined: 19 Sep 2017, 12:27

Re: Help please

22 Sep 2017, 11:45

This is something I put together it finds the image when it pops up, but does not loop.
I need to keep my finger pressed on numpad0

Code: Select all

Numpad0::
CoordMode, Pixel, Window
ImageSearch, FoundX, FoundY, 10, 94, 1032, 776, C:\Users\home\AppData\Roaming\MacroCreator\Screenshots\Screen_20170922172311.png
If ErrorLevel = 0
Loop
{
MsgBox, 49, Continue?, Image / Pixel Found at %FoundX%x%FoundY%.`nPress OK to continue.
IfMsgBox, Cancel
	Return
} else
	if Errorlevel =1
	{
		;MoveMousePoint(629, 171, 7)
    MoveMousePoint(636, 182, 7)
    Sleep, 350
   click
   Sleep, 350
   return

}
User avatar
Exaskryz
Posts: 2882
Joined: 17 Oct 2015, 20:28

Re: Help please

22 Sep 2017, 15:21

Well the ImageSearch only happens once, it is not inside the loop.

Right now your Loop is kind of silly. It only happens when the image is found when you press Numpad0. It prompts you with a MsgBox, to which you can say OK or Cancel. If you hit Cancel, the thread is done. That's it, nothing else to see here. If you hit OK, then the MsgBox will pop up again with the exact same message. And it will repeat as long as you hit OK.

Also, as an aside, your shared code is incomplete because there's no MouseMovePoint() function defined.

Oh, and I will say that in the event that ErrorLevel = 2 for some technical reason (such as the reference image file not being found), you have no If conditions for that.
Phasecoder
Posts: 7
Joined: 19 Sep 2017, 12:27

Re: Help please

22 Sep 2017, 16:59

Can you please sort my code. The message box I wan rid of that.

I want the script to pause when image is found.
Then continue when its not there.

MouseMovePoint I have in an include.

Please can you lay my code out?
User avatar
Exaskryz
Posts: 2882
Joined: 17 Oct 2015, 20:28

Re: Help please

23 Sep 2017, 01:45

Let's drop the entire MsgBox. Which means we can drop the entire If ErrorLevel = 0 because the MsgBox is basically the entirety of that. And we don't need the else now. This reduces your code to:

Code: Select all

Numpad0::
CoordMode, Pixel, Window
ImageSearch, FoundX, FoundY, 10, 94, 1032, 776, C:\Users\home\AppData\Roaming\MacroCreator\Screenshots\Screen_20170922172311.png

	if Errorlevel =1
	{
		;MoveMousePoint(629, 171, 7)
    MoveMousePoint(636, 182, 7)
    Sleep, 350
   click
   Sleep, 350
   return

}
OK, cool, let's look at what we have here. We no longer have a Loop. Our goal is to repeat the ImageSearch and we will take different actions depending on what that is. So let's put a Loop and a Block to wrap around the ImageSearch:

Code: Select all

Numpad0::
CoordMode, Pixel, Window
Loop
{
ImageSearch, FoundX, FoundY, 10, 94, 1032, 776, C:\Users\home\AppData\Roaming\MacroCreator\Screenshots\Screen_20170922172311.png

	if Errorlevel =1
	{
		;MoveMousePoint(629, 171, 7)
    MoveMousePoint(636, 182, 7)
    Sleep, 350
   click
   Sleep, 350
   return

}
}
Right now, that code has the ImageSearch run constantly until the image is NOT found. Once the Image is not found, it does all that MouseMoving and clicking, then it hits the return which means stop. Even the loop stops.

But now I don't know how to fit that with your original request. You want F5 to be pressed while the image is not found. Once it is found, you want to wait until the image is gone. This is my best attempt at reconciling that. You are already taking the Click actions when image is not found. We'll add in a Send {F5} to that. But then we don't want the return, because we want to be able to repeat the ImageSearch! So we scrap the return.

Code: Select all

Numpad0::
CoordMode, Pixel, Window
Loop
{
ImageSearch, FoundX, FoundY, 10, 94, 1032, 776, C:\Users\home\AppData\Roaming\MacroCreator\Screenshots\Screen_20170922172311.png

	if Errorlevel =1
	{
		;MoveMousePoint(629, 171, 7)
    MoveMousePoint(636, 182, 7)
    Sleep, 350
   click
   Sleep, 350
   Send {F5}

}
}
Alright, so now we need to add the If statement for when the image is found. Inside of that, we'll take actions for repeating the ImageSearch by literally copypasting the ImageSearch above.

Code: Select all


Numpad0::
CoordMode, Pixel, Window
Loop
{
ImageSearch, FoundX, FoundY, 10, 94, 1032, 776, C:\Users\home\AppData\Roaming\MacroCreator\Screenshots\Screen_20170922172311.png

	if Errorlevel =1
	{
		;MoveMousePoint(629, 171, 7)
    MoveMousePoint(636, 182, 7)
    Sleep, 350
   click
   Sleep, 350
   Send {F5}

}
else if Errorlevel = 0
{
ImageSearch, FoundX, FoundY, 10, 94, 1032, 776, C:\Users\home\AppData\Roaming\MacroCreator\Screenshots\Screen_20170922172311.png
}
}
Alright, cool. We've repeated the ImageSearch, but what are we going to do with that? Well, what you want is to not do anything until the Image disappears again. In which case, what should be done is repeating another Loop inside this If statement and we will take action when the image is NOT found.

Code: Select all

Numpad0::
CoordMode, Pixel, Window
Loop
{
ImageSearch, FoundX, FoundY, 10, 94, 1032, 776, C:\Users\home\AppData\Roaming\MacroCreator\Screenshots\Screen_20170922172311.png

	if Errorlevel =1
	{
		;MoveMousePoint(629, 171, 7)
    MoveMousePoint(636, 182, 7)
    Sleep, 350
   click
   Sleep, 350
   Send {F5}

    }
else if Errorlevel = 0
    {
    Loop
        {
        ImageSearch, FoundX, FoundY, 10, 94, 1032, 776, C:\Users\home\AppData\Roaming\MacroCreator\Screenshots\Screen_20170922172311.png
        If ErrorLevel = 1 ; image is not found
            Msgbox now we need to do something here
        }
    }
}
OK, now we're close. What we need to be able to do is go from this inside Loop to the outside Loop when the image disappears. How do we do that? Well, the Break command will get us out of a loop.

Code: Select all

Numpad0::
CoordMode, Pixel, Window
Loop
{
ImageSearch, FoundX, FoundY, 10, 94, 1032, 776, C:\Users\home\AppData\Roaming\MacroCreator\Screenshots\Screen_20170922172311.png

	if Errorlevel =1
	{
		;MoveMousePoint(629, 171, 7)
    MoveMousePoint(636, 182, 7)
    Sleep, 350
   click
   Sleep, 350
   Send {F5}

    }
else if Errorlevel = 0
    {
    Loop
        {
        ImageSearch, FoundX, FoundY, 10, 94, 1032, 776, C:\Users\home\AppData\Roaming\MacroCreator\Screenshots\Screen_20170922172311.png
        If ErrorLevel = 1 ; image is not found
            Break
        }
    }
}
And this should be functional code. It is untested. You may find that it runs pretty high on CPU -- this is because we're giving no breaks to it. What I recommend is putting some Sleeps in there. I like them right after each ImageSearch. Probably a 100 millisecond sleep. But if it is critical your action is highly in sync with the presence or absence of the image, maybe you put the Sleeps after the If statements. So, yeah, I'll put them there:

Code: Select all

Numpad0::
CoordMode, Pixel, Window
Loop
{
ImageSearch, FoundX, FoundY, 10, 94, 1032, 776, C:\Users\home\AppData\Roaming\MacroCreator\Screenshots\Screen_20170922172311.png
	if Errorlevel =1
	{
		;MoveMousePoint(629, 171, 7)
    MoveMousePoint(636, 182, 7)
    Sleep, 350
   click
   Sleep, 350
   Send {F5}

    }
else if Errorlevel = 0
    {
    Loop
        {
        ImageSearch, FoundX, FoundY, 10, 94, 1032, 776, C:\Users\home\AppData\Roaming\MacroCreator\Screenshots\Screen_20170922172311.png
        If ErrorLevel = 1 ; image is not found
            Break
         Sleep 100
        }
    }
Sleep 100
}
Now, an interesting point here is that when you are put into the inner loop (because the image IS present), once the image disappears, you are going to Break out of that Loop. This puts you back into the Else Block. The Else Block immediately ends. Then the very bottom Sleep 100 executes. Maybe we want to be as tight as possible to resuming the clicks and F5 to when the image disappears. We can actually use the Continue command to start at the top of a loop again. Normally Continue as is works on just the Loop it is inside. But if we say Continue 2, then execution jumps to the top of the outer loop. In fact, I have never used Continue 2 before, so this is cool I've come to an example where it may be used. We're going to use this instead of the Break command:

Code: Select all

Numpad0::
CoordMode, Pixel, Window
Loop
{
ImageSearch, FoundX, FoundY, 10, 94, 1032, 776, C:\Users\home\AppData\Roaming\MacroCreator\Screenshots\Screen_20170922172311.png
	if Errorlevel =1
	{
		;MoveMousePoint(629, 171, 7)
    MoveMousePoint(636, 182, 7)
    Sleep, 350
   click
   Sleep, 350
   Send {F5}

    }
else if Errorlevel = 0
    {
    Loop
        {
        ImageSearch, FoundX, FoundY, 10, 94, 1032, 776, C:\Users\home\AppData\Roaming\MacroCreator\Screenshots\Screen_20170922172311.png
        If ErrorLevel = 1 ; image is not found
            Continue 2
         Sleep 100
        }
    }
Sleep 100
}
The inside ImageSearch will keep checking every 100 milliseconds for the Image to be absent. Once that happens, it jumps back up to the outer ImageSearch (the very first one), which will immediately redo the search, and then take action from there.
Phasecoder
Posts: 7
Joined: 19 Sep 2017, 12:27

Re: Help please

23 Sep 2017, 10:57

Wow man thank you very much, ill take a good rread of that later.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Chunjee, ShatterCoder and 128 guests