while not exists(image, FOREVER) seems not to work

Asked by Geirdaz

RegionA = Region(1,2,3,4)
RegionB = Region(4,5,6,7)

while True:
    while not RegionA.exists(Blue.png,FOREVER):
         RegionB.wait(Yellow.png,FOREVER)
         RegionB.click(getLastMatch())

Is this correct? because even when RegionA exists the Blue.png Yellow.png get clicked when Yellow.png appears. I want the Yellow.png to be clicked only when when Blue.png is not seen. Thanks

Question information

Language:
English Edit question
Status:
Solved
For:
SikuliX Edit question
Assignee:
No assignee Edit question
Solved by:
RaiMan
Solved:
Last query:
Last reply:
Revision history for this message
RaiMan (raimund-hocke) said :
#1

Even if it would work correctly, it will never do, what you want:

while not RegionA.exists(Blue.png,FOREVER):

since exists(image, FOREVER), will wait forever until image appears, it will not come back, if image does not appear and your script will hang in the evaluation of the while condition.

This is better and works:

while True:
    if not RegionA.exists(Blue.png, 0): # only one try to find it
         RegionB.wait(Yellow.png,FOREVER)
         RegionB.click(getLastMatch())
    wait(1) # or some other value

The loop while True: loops forever, so you need some conditional break inside, if you do not want to kill the IDE to stop it:
if some_condition: break

--- Your problem
I will test exists(image, FOREVER). might have a problem.

Revision history for this message
RaiMan (raimund-hocke) said :
#2

I made the following test wish latest build of rc3:

while not exists(img, FOREVER):
    print "not found"
print "found"

It works as expected:
-- "not found" is not printed as long as img is not visible
-- "found" is printed, when img gets visible

Revision history for this message
RaiMan (raimund-hocke) said :
#3

I just realized another bug in your script:

It should be:
         click(RegionB.getLastMatch())

because you want to click the last match that was recorded in RegionB with the RegionB.wait() before.

Revision history for this message
Geirdaz (icatchacold) said :
#4

Thanks i think i have another problem here

while True:
   while not RegionA.exists(Blue.png,FOREVER):
      RegionB.wait(Yellow.png,FOREVER) <---------------
      click(RegionB.getLastMatch())

I realized my script has a weakness whereby the loop is very much dependent on whether the Yellow.png would appear or not.
While Blue.png could be absent, it would could also reappear again before Yellow.png appears. Apologies for the confusion. How can i modify this script so regardsless of how many times Blue.png appear or disappear Yellow.png would still get clicked.

Thanks

Revision history for this message
RaiMan (raimund-hocke) said :
#5

I do not know, what is happening on your machine, but Yellow should never be clicked with your snippet:

RegionA.exists(Blue.png,FOREVER) only comes back, if Blue appears and returns the match which is taken as True and as False together with the not. So the while loop should never be processed.

--- How can i modify this script so regardsless of how many times Blue.png appear or disappear Yellow.png would still get clicked
I do not understand: this means, Yellow should be clicked in any case, when it appears, no matter if Blue is there or not:

while True:
     click(RegionB.wait(Yellow.png,FOREVER))

Revision history for this message
Geirdaz (icatchacold) said :
#6

My script
1. Blue (absent) in RegionA
2. Yellow (present) in RegionB
3. Click Yellow
4. Loop

eg of my situation
1. Blue (absent) in RegionA
2. Blue (present) in RegionA
3. Blue (absent) in RegionA
4. Blue (present) in RegionA <------
5. Yellow (present) in RegionB
6. Clicked Yellow
7. Loop

The thing is as long as blue is absent in the first place, my script will wait indefinitely for yellow to appear in RegionB. My situation is that at no.4 Yellow still gets clicked even when Blue is present. The occurance of blue appearing or dissapearing are totally random and are independent of whether Yellow appears or not. So 2,3&4 are ignored until Yellow appears. I hope this is clear. Thanks

Revision history for this message
Best RaiMan (raimund-hocke) said :
#7

--- Your script

Again: if this is still your script:
while True:
   while not RegionA.exists(Blue.png,FOREVER):
      RegionB.wait(Yellow.png,FOREVER)
      click(RegionB.getLastMatch())

then Yellow should never be clicked.
Since your script here is not your original script (Blue.png and Yellow.png will make a syntax error), I think your script has a bug that is not obvious (because it seems to do something).

In the IDE it should look like this (and it should not do, what you think it should do)
while True:
<tab>while not RegionA.exists(Blue.png,FOREVER):
<tab><tab>RegionB.wait(Yellow.png,FOREVER)
<tab><tab>click(RegionB.getLastMatch())

where <tab> means: having the text left justified, use one tab for each <tab> to indent it.

-- your situation
still not clear what you want:

this seems to be clear: when yellow appears, it should be clicked, but the dependency from Blue is not clear.

for Blue I see 2 options:
1. should it only be clicked, if Blue is visible at the same time?
2. or should it only be clicked, if Blue has been visible before at least once? (does not matter if Blue is still visible now)

-- for option 1:

while True:
<tab>regionB.wait(Yellow, FOREVER)
<tab>if regionA.exists(Blue, 0):
<tab><tab>click(RegionB.getLastMatch())

-- for option 2:

isBlue = False
while True:
<tab>if not isBlue and RegionA.exists(Blue, 0): isBlue = True
<tab>if regionB.exists(Yellow, 0):
<tab><tab>if isBlue:
<tab><tab><tab>click(RegionB.getLastMatch())
<tab><tab><tab>isBlue = False
<tab>wait(1)

Another question: If you successfully (as you think it should be) clicked Yellow, should you wait until Yellow vanishes before you start allover again or does it vanish with the click?
If it makes sense, add this after the click(RegionB.getLastMatch())
<tab><tab>RegionB.waitVanish(Yellow, 10) # or another time that makes sense (option 1)
<tab><tab><tab>RegionB.waitVanish(Yellow, 10) # or another time that makes sense (option 2)

Revision history for this message
Geirdaz (icatchacold) said :
#8

Thanks RaiMan, that solved my question.