cant temporarily stop event observations

Asked by geo on 2016-12-07


    I'm trying to automate a game on facebook by setting an observe region. The game is quite simple. eggs drop from the top of the screen and all you have to do is catch them at the bottom of the screen. The glove that catches them its move by the mouse. All the eggs have the same shape and size but they all have different design patterns so I can't do a search for a particular egg since all of them are different and the design appears randomly. Plus there must be thousands of designs.

So here is what I did:
    I set up a rectangular region across the screen close to the glove. I then set up an regionX.onchage event. Once an egg crosses that region the glove is put underneath it. This is not a perfect solution because you can get multiple hits so the glove tends to jump around but, its good enough. The problem is that once you collect 100 eggs a button at the bottom of the screen shows up. You need to click that button and navigate through some menus to collect some of the eggs and ignore others. Here is where I'm having problems. It seems that the regionX.onchange keeps running so I can't navigate through this menu. I need for the program to stop searching for changes completely until I exit the egg collection menu. Below is part of the code. I'm omiting the searches for other buttons since they are commented out (not being used for now) anyways.

Settings.ObserveScanRate = 0.4
Settings.ObserveMinChangedPixels = 65

runGame = True

#define regions
regionX = Region(261,453,813,26) #look for changes
regionA = Region(585,629,165,62) #start game button region
regionB = Region(589,538,166,79) #keep playing region
regionC =Region(562,532,251,144) #bag full
regionD =Region(478,298,503,384) #rare eggs

#Set the observations
regionX.onChange(FOREVER, onchangedFunction)

#define pertinent functions
#Move glove to collect the eggs
def onchangedFunction(event):
    regionX.stopObserver() #<--- stop event temporarily

    for ch in event.getChanges():
        hover(Location(ch.x+10, ch.y))

    regionX.onChange(onchangedFunction) #<-- restore egg search event
    regionX.observe(FOREVER, background=True)

#get the rarest eggs
def getRareEggs():
    regionD.wait(Pattern("1467043168899.png").targetOffset(-320,3), 10)

    while regionD.exists(Pattern("1467043168899.png").targetOffset(-320,3), 0):
        click(regionD.getLastMatch()) # avoids another find() operation
        wheel(WHEEL_DOWN, 1)

#-----RUN GAME------
    #Check if 100 eggs have been collected
    if regionC.exists("1481068803504.png"):
        regionX.stopObserver() #<-- Does this stops observations??? # avoids another find() operation
        regionX.onChange(onchangedFunction) #<-- restore observations
        regionX.observe(FOREVER, background=True)

Question information

English Edit question
Sikuli Edit question
No assignee Edit question
Solved by:
Last query:
Last reply:
geo (mrgees100peas) said : #1

Additional info. I'm running on
OS: Ubuntu 16 (I think 16.10)
browser: firefox
CPU: 1.9 GHz A-Series Quad-Core A8-4500M

geo (mrgees100peas) said : #3

hhmmm I wonder if I'm over doing the observer as in

 instead of:
#Do something here
regionX.onChange(onchangedFunction) #<-- I don't think this is needed
regionX.observe(FOREVER, background=True)

just Do:
#Do something here
regionX.observe(FOREVER, background=True)

Also, are the observers global? for example

I'm doing the following:
def eventHandler(event)
    #Do something here
    regionX.observe(FOREVER, background=True)

Where I should be doing:
def eventHandler(event)
    #Do something here
    event.observe(FOREVER, background=True)

RaiMan (raimund-hocke) said : #4

really read the docs?

the minimum is:

def handler(evt):
    print evt

regionX.onChange(handler) # the event to observe

regionX.observe(FOREVER, background=True) # now observe in background

following this has to be something, that keeps the script alive

version 1.1.1 has some additional features to better control what should happen.

The challenge in your case is to coordinate the actions in the handler with what the main script does.

... BUT NEVER EVER start an observer in an handler !!!

geo (mrgees100peas) said : #5

Yes, I read it multiple times. It talks about defining and starting the observer and even stopping it but, not how to restart it properly. However, I think I got it with your last message of never starting the observation in the handler. Sooooo would the following code be "legal" as far as restarting the observer. Below should be a complete program.

regionX = Region(261,453,813,26) #for event handling purposes
regionY =Region(562,532,251,144) #some button

#Set the observations
regionX.onChange(FOREVER, handler)

def handler(evt):
    evt.stopObserver() #stops regionX event???
    print("in event handler")

    regionX.observe(FOREVER, background=True) #<-- Is This OK here???? it should return false if event already exist

    if regionY.exists("1481068803504.png"):
        print("found the button")

geo (mrgees100peas) said : #6

The solution above worked. Thanks