Why Does Loop Exit without meeting requirements?

Hints, tips and tricks for newbies

Moderators: Dorian (MJT support), JRL

Post Reply
Franklin
Newbie
Posts: 18
Joined: Mon Dec 14, 2015 5:14 pm

Why Does Loop Exit without meeting requirements?

Post by Franklin » Tue Oct 27, 2020 1:33 pm

Hello Everyone.

I have a small head scratching puzzle. It's beyond my noob skills at the moment.


The goal of this script is to search the screen for image1.bmp and click on image1.bmp every time its found on the screen until it finds image2.bmp then the script should exit the mainloop and find image2.bmp then click on it to exit the script.

Something so simple is causing me hair-loss. Because it exits after click on image1.bmp


Logic flow:
The following code loop looks for image2.bmp
If doesn't find image2.bmp then it looks for image1.bmp
If it finds image1.bmp then it clicks on it
the Numfound variable is reset to 0 returns to the main loop.
If it finds image2.bmp then it jumps to FinishUp clicks image2.bmp
then script stops.

The Puzzle:
From the coding, it should not exit the script unless it finds image2.bmp & clicks on it.
What's happening is the script will exit after it finds image1.bmp and clicks it.

After debugging, i can't find out why this is happening.
The only value would be the NumFound is greater than zero, however debugging shows the value is reset to zero after it clicks image1.bmp and the script stops.

The Question: Why? What am I missing? Is there a better/more reliable approach?

Code: Select all

Label>MainLoop
Let>NumFound=0
//Find and Left Click Center of
FindImagePos>%BMP_DIR%\image_2.bmp,SCREEN,0.7,1,XArr,YArr,NumFound,CCOEFF
If>NumFound>0
    Goto>FinishUp
Else
    //Find and Left Click Center of
    FindImagePos>%BMP_DIR%\image_1.bmp,SCREEN,0.7,1,XArr,YArr,NumFound,CCOEFF
    If>NumFound>0
      MouseMove>XArr_0,YArr_0
      LClick
      Let>NumFound=0
    Endif
Endif
Wait 2
Goto>MainLoop

Label>FinishUp
//Find and Left Click Center of
FindImagePos>%BMP_DIR%\image_2.bmp,SCREEN,0.7,1,XArr,YArr,NumFound,CCOEFF
If>NumFound>0
  MouseMove>XArr_0,YArr_0
  LClick
Endif
//Eject from script


User avatar
Dorian (MJT support)
Automation Wizard
Posts: 1378
Joined: Sun Nov 03, 2002 3:19 am
Contact:

Re: Why Does Loop Exit without meeting requirements?

Post by Dorian (MJT support) » Tue Oct 27, 2020 2:22 pm

The goal of this script is to search the screen for image1.bmp and click on image1.bmp every time its found on the screen until it finds image2.bmp then the script should exit the mainloop and find image2.bmp then click on it to exit the script.
If I am understanding correctly, that is what it was doing when I tested your script. It looks for image2 first, if not found, click image 1 and look for image 2 again. If image 2 was found, click and exit.

Does this do what you're looking for?

Look for image 2.
If found, click and exit

If not found, look for image1 and click on it, then repeat.

Code: Select all

Let>NumFound2=0

While>NumFound2=0
  FindImagePos>%BMP_DIR%\image_2.bmp,SCREEN,0.7,1,X2Arr,Y2Arr,NumFound2,CCOEFF
  If>NumFound2>0
    MouseMove>X2Arr_0,Y2Arr_0
    LClick
    Exit
  Else

    FindImagePos>%BMP_DIR%\image_1.bmp,SCREEN,0.7,1,X1Arr,Y1Arr,NumFound1,CCOEFF
    If>NumFound1>0
      MouseMove>X1Arr_0,Y1Arr_0
      LClick
    Endif
  Endif
Endwhile
Yes, we have a Custom Scripting Service. Message me or go here

Franklin
Newbie
Posts: 18
Joined: Mon Dec 14, 2015 5:14 pm

Re: Why Does Loop Exit without meeting requirements?

Post by Franklin » Wed Oct 28, 2020 11:01 am

Dorian!

Great example of using individually defined variables as parameters within a command.
i.e. NumFound1 & NumFound2...

Thank you for a much cleaner example of code!

User avatar
Dorian (MJT support)
Automation Wizard
Posts: 1378
Joined: Sun Nov 03, 2002 3:19 am
Contact:

Re: Why Does Loop Exit without meeting requirements?

Post by Dorian (MJT support) » Wed Oct 28, 2020 11:27 am

You're welcome. Hopefully I understood what you were looking for and it does what you wanted. At the very least it should be a good starting point. You could also replace numfound1 with something more meaningful. SaveButtonFound, BigRedStarFound etc etc.
Yes, we have a Custom Scripting Service. Message me or go here

Post Reply
Sign up to our newsletter for free automation tips, tricks & discounts