Repeat a def problem

Asked by Clonex

Hello there.
I'm trying to code a simple code.
The problem happens when it has repeated the check a couple of times, and the image doseng exists.
Then it just stops the code.

Image of the code: http://clonex.info/greenfruit.png

Thanks, in advance.

Question information

Language:
English Edit question
Status:
Answered
For:
SikuliX Edit question
Assignee:
No assignee Edit question
Last query:
Last reply:
Revision history for this message
RaiMan (raimund-hocke) said :
#1

what image does not exist?

the script cannot stop without an error message (e.g. FindFailed with the wait() and click()'s)
so what happens - error?

tip 1: for effectiveness:

if exists(img): click(img)

makes to find()'s on the same image in the same moment - better:

if exists(img): click(getLastMatch()) # reuse the match object generated by exists()

tip 2: timing:

use exists(img, 0), to reduce the waiting time in cases where img should be there and otherwise look for something else. If it is not, this costs you less than 0.5 seconds.

your def() lasts more than 10 seconds, if the last img (snup) is found.

use both tips together and you will speed it up to an average of about 1-2 seconds.

Revision history for this message
Clonex (clonex-kontakt) said :
#2

Hello RaiMan.
First, thanks for help. It helped me alot.

I've been around reading some things, how to make the script faster ect.

Sometimes I'm getting this error "[sikuli] Exited with code 0", when it dosent come with the error it wil only click the last in the fruits. Dont know what i did wrong? ..

Heres the code: http://clonex.info/newscript.png

And the code without images:

with :
 pickups = [ , , , , ]
 fruits = [ , , , , ]

 def pickup(target):
  click(target)
  for i in pickups:
   if exists(i):
    click(getLastMatch())

 while True:
  for p in fruits:
   target = exists(p)
  if target:
   pickup(target)
  if exists( , 0):
   exit()
  sleep(1)

Thanks, Clonex.

Revision history for this message
RaiMan (raimund-hocke) said :
#3

"[sikuli] Exited with code 0" is printed when an exit() ends the script, only info - no error

your loops:

def pickup(target):
  click(target)
  for i in pickups:
   if exists(i, 0): # <----------- missed the 0?
    click(getLastMatch())

it clicks on every i that exists - is that what you want? or should it be the first one and then exit? if yes:

def pickup(target):
  click(target)
  for i in pickups:
   if exists(i):
    click(getLastMatch())
    break

but looking to the end, it seems you mean it.

your other loop:
while True:
  for p in fruits:
   target = exists(p, 0) # <----------- missed the 0?
  if target:
   pickup(target)
  if exists( , 0):
   exit() # <------- this exits the script
  sleep(1)

I guess you mean:

while True:
# ------------------------- loop start
  for p in fruits:
   target = exists(p)
   if target:
     pickup(target)
#------------------------- loop end
  if exists( , 0):
   exit()
  sleep(1)

so mind indentation for inner while/for/if statements!

Can you help with this problem?

Provide an answer of your own, or ask Clonex for more information if necessary.

To post a message you must log in.