Search code examples
pythonpython-2.7if-statementsikuliobservers

Python - Code ignores `if`-statement?


I just got into Python (Jython) coding a few hours ago and I'm trying to automate Kik messenger (using an Android emulator) using Sikuli IDE.

I am trying to make a region observer that scans for changes, if a change is made, it will check if any commands are found. I am not really sure what I'm doing, but this is the code I got with some help all around the web and documentations:

cmdScanLoc = Region(Region(65,762,167,59))

def cmdHelp():
    type("Help")
    type(Key.ENTER)
    cmdScanLoc.stopObserver()

def cmdPing():
    type("Pong.")
    type(Key.ENTER)
    cmdScanLoc.stopObserver()

def changeDetected(event):
    print("Change")
    if cmdScanLoc.exists("1440090739688.png"):
        cmdHelp()
    elif cmdScanLoc.exists("1440090725124.png"):
        cmdPing()
    else:
        print("No Command Found")

def startObserver():
    cmdScanLoc.onChange(50,changeDetected)
    cmdScanLoc.observe(10,background=False)
    Settings.ObserveScanRate = 10

startObserver()

Here is the log, after typing !ping:

Change

!help
[log] TYPE "Help"

[log] TYPE "#ENTER."

It seems to go to cmdHelp(), even though I typed !ping. How is that possible? It just completely ignores the if-statement.

And here is an image of the region I'm scanning: https://i.sstatic.net/y6xmo.png And an image of the images I'm scanning for: https://i.sstatic.net/n1aH6.png (code in this image is no longer accurate as you can see)

I would greatly appreciate it if someone could guide me in the right direction with this "command scanner" where if a certain command is detected, the appropiate function is called.

Thanks a lot in advance and sorry if this is a really nooby question, I've just been trying for hours and hours, looking up documentation of Sikuli and Python and I just can't get it to work...


Solution

  • It's much smarter and much faster to do this kind of thing with the region observer than with an if-statement. Example code:

    def cmd1(event):
        print("Command One")
        event.cmdRegion.stopObserver()
        waitCmdAppear()
    
    def cmd2(event):
        print("Command Two")
        event.cmdRegion.stopObserver()
        waitCmdAppear()
    
    def cmd3(event):
        print("Command Three")
        event.cmdRegion.stopObserver()
        waitCmdAppear()
    
    def waitCmdAppear():
        cmdRegion.onAppear(Pattern("1.png").exact(), cmd1)
        cmdRegion.onAppear(Pattern("2.png").exact(), cmd2)
        cmdRegion.onAppear(Pattern("3.png").exact(), cmd3)
        cmdRegion.observe(FOREVER)
    
    waitCmdAppear()
    

    Things to not forget:

    1. The (event) part when defining a function that's going to be called by the region observer.
    2. Stopping the Observer in the event, even if you are going to need it again. Just restart it.
    3. In the onAppear, (region.onAppear([PS], [handler])) type the handler (ex. cmd3) not the function (ex. cmd3())

    I hope this will help other people. :)