← Back to team overview

sikuli-driver team mailing list archive

Re: [Question #279248]: [1.1.0] HowTo use onAppear with a lambda handler

 

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

    Status: Open => Answered

RaiMan proposed the following answer:
The onXXX... have to be associated with the same Region object as the
observe:

hence this is correct:
reg = Region(1309,107,271,52)
reg.onAppear("1450123985359.png", lambda:click("1450124572739.png"))
reg.observe(125)

but the same can be achieved by:
reg = Region(1309,107,271,52)
if reg.exists("1450123985359.png", 125): # wait max 125 secs for the image to appear in reg
    reg.click() # if appeared, click the last match in reg

... but if you use observe, then you should know:
in the current implementation, each onXXX registers the given event with the given region and it is not checked, wether the "same" event was already registered. Hence if onXXX is called in a loop, each turn adds a new event (in your case worst case 1000), which might lead to short memory crashes and gets slower and slower, because each registered event is checked with every internal observe cycle (Settings.ObserveScanRate).

so this would be safe:
reg = Region(1309,107,271,52) # it is constant anyways
onAppear("1450123985359.png", lambda:click("1450124572739.png")) # only needed once

for x in range(1000):
    if exists("1450123792558.png"):
        click("1450123792558.png")
        wait(10)
        if exists("1450123911447.png"):
            click("1450123911447.png")
            reg.observe(125)

Nevertheless: the exists() solution is more transparent, when you look
at the script.

In general: An inline (the script waits until the observe ended) observe() for only some events can always be substituted by using exists().
Observe() only really makes sense, if it is used in the background version (the observe is done in parallel to the running script.

see:
http://sikulix-2014.readthedocs.org/en/latest/region.html#observing-visual-events-in-a-region

-- 
You received this question notification because your team Sikuli Drivers
is an answer contact for Sikuli.