← Back to team overview

sikuli-driver team mailing list archive

Re: [Question #195347]: wait for image A or wait for image B

 

Question #195347 on Sikuli changed:
https://answers.launchpad.net/sikuli/+question/195347

    Status: Open => Answered

RaiMan proposed the following answer:
--- inside() is no longer necessary with Sikuli X

--- to check wether something might appear and let the script wait for
it use exists() instead of wait(), if you want to keeps the script
running.

while True:
   RegA.exists(imgA.png,FOREVER) or RegB.exists(imgB.png,FOREVER)
   click(imgA.png)

But this will never work either as you might expect:
- expressions like something or something_else are evaluated from left to right. So first Sikuli waits for imgA. If this never comes, the script will wait forever.
- if imgA comes to existence, the or condition is satisfied and the second expression (wait for imgB) is not evaluated at all.
- so click(imgA) will only be processed, if imgA comes up.

You need the searches be done in parallel and the first one that
succeeds lets the script continue:

This is the feature observe() run in background.

def handler(e):
    global isImgA_or_ImgB
    isImgA_or_ImgB = True

RegA.onAppear(imgA, handler)
RegB.onAppear(imgB, handler)
isImgA_or_ImgB = True

while True:
    RegA.observe(FOREVER, background=True)
    RegB.observe(FOREVER, background=True)
    while not isImgA_or_ImgB:
        wait(1)
    RegA.stopObserver()
    RegB.stopObserver()
    click(imgA)

-- 
You received this question notification because you are a member of
Sikuli Drivers, which is an answer contact for Sikuli.