Why Handler is not working in region.onAppear.?

Asked by Leonardo Castro on 2020-02-21

Hello.

When I run my code, nothing happens, no erros, but also no results.
I already checked "comparing" values of image, and image is aways being detected, but handler does not run.

What I'm doing wrong?

CODE:

def Quests(event = None):
    if not event:
        r.stopObserver()
        doubleClick("1581564293714.png")
        while exists(Pattern("1581657244124.png").similar(0.85)):
            click(Pattern("1581657244124-1.png").similar(0.84))
            sleep(0.5)
        if exists("1582077186091.png"):
            click ("1582077186091.png")
            sleep(0.5)
            click("1582077240628.png")
        else:
            if exists("1582257077497.png"):
                sleep(0.5)
                click(Pattern("1582257077497.png").similar(0.62))
                sleep(0.5)
            else:
                click("1582257086061.png")
                sleep(1)
        while exists(Pattern("1581657244124.png").similar(0.83)):
            click(Pattern("1581657244124-1.png").similar(0.84))
            sleep(0.5)

while (1):
    r.onAppear("1581564293714.png", Quests)
    r.observeInBackground(); wait(10)
    r.stopObserver()

Question information

Language:
English Edit question
Status:
Solved
For:
Sikuli Edit question
Assignee:
No assignee Edit question
Solved by:
Leonardo Castro
Solved:
2020-02-29
Last query:
2020-02-29
Last reply:
2020-02-29
RaiMan (raimund-hocke) said : #1

using r.observeInBackground makes only sense, if you want to wait for something, that might appear/vanish/change in PARALLEL to your normal workflow (some popup for example).

In your case you are simply waiting for "1581564293714.png" and if it comes up, want to do something.

This is how one would do it normally:

while (1):
  if r.exists("1581564293714.png", 10):
    r.doubleClick() # click last match
    while exists(Pattern("1581657244124.png").similar(0.85)):
      click() # click last match (I guess)
      #sleep(0.5) # not needed, next exists waits max 3 seconds anyways
    if exists("1582077186091.png"):
      click ()
      click("1582077240628.png")
    else:
      if exists("1582257077497.png"):
        click()
      else:
        click("1582257086061.png")
        sleep(1)
      while exists(Pattern("1581657244124.png").similar(0.83)):
        click()

Be aware: while the handler is doing its job, the main workflow hangs until the handler finishes its work. So it usually does not make sense, to have longer running handlers.

Workflows like mine above are much easier to track anyways.

RaiMan (raimund-hocke) said : #2

... and I checked your usage of observeInBackground: this principally works as it should

RaiMan (raimund-hocke) said : #3

... and I am using the latest SikuliX 2.0.3

Leonardo Castro (leokastro) said : #4

Hello RaiMan,
As always, thank you for support.

When i started using sikuli, I was not using definition at all, only alot of if-while statements, and it was working pretty good.

So looking at documents, i found that I could use region.observer, and with that, maybe I could improve my code.
My goal was to, observe all my screen, and run definitions according with events that pops on screen.

I'm using sikulix 2.0.2, I will download 2.0.3 to check if my script still has same behavior.

RaiMan (raimund-hocke) said : #5

I principally understand.

But be aware:
not using observe is always a better choice (more complex, harder to debug, less obvious with relation to the workflow, ...), if the same can be achieved without it.

When trying to use more than one observe in parallel, you have to take care for synchronization between the handlers, since with SikuliX at one time only one handler should have access to screen, mouse and keyboard. Otherwise you wold get some action chaos.

Definitions as you say (the expert term is functions), are a way, to hide away complexity and to avoid repetitions of same or similar code (DRY principle = don't repeat yourself).

So feel free to use functions whereever it makes sense - has nothing todo with SikuliX nor observe.

Leonardo Castro (leokastro) said : #6

Hello RaiMan.

Could you please check the download link for Sikuli 2.0.3?, i think its down.

Thanks

RaiMan (raimund-hocke) said : #7

thanks for the pointer - repaired.

Leonardo Castro (leokastro) said : #8

RaiMan, Its possible to configure delay for "if" statement?.

I know that is 3 seconds, but maybe i could configure for less than 3 seconds.

RaiMan (raimund-hocke) said : #9

The docs would have told you, that exists() has a timing parameter.

Leonardo Castro (leokastro) said : #10

Thank you RaiMan!