if exists (A,0), if exists (B,0) not working within a loop

Asked by Josh on 2017-03-21

Hey Raimund,

Thanks for your help the last time! I have been playing around with Sikulix since the last time i got a reply from you and it has been an amazing experience! Have a number of questions I would like to get your advice on, but thought I would space them into individual questions so that it will be useful to file into the faq.

My program is intended to automize a game. How the game goes is as follows.

Loop
1) Start scenario
2) Scenario runs for ~ 2minutes
3) Success (1st possible Outcome) - followed by a variation in rewards which require their own respective actions
4) Failure (2nd possible Outcome) - press some buttons to move on to next run
5) Press End scenario to reach start scenario page - potential request to refill Energy level

The issue is sometimes the Success page appears but the programme does not respond. I think it might have to do with my coding of the two possible outcomes statement, as below. I have tried using exists(image,0) and exists (image,1-5) to no avail. I have checked the images multiple times when the program failed to respond, and the images were matched to those i have saved, so that should not be the issue.

My guess of the problem:

    waitUntil = time.time()+ 120
    while time.time() < waitUntil:
        if rewardicon.exists(reward image,0):
        elif reg2.exists(failed image,0):

Script:

#Begins scenario

for runs in range(1,10):

if regStart.exists(image):
        time.sleep(0.5)
        click(pointA)
        time.sleep(100)

    rewardicon = (region of reward image)
    reg2 = Region(764,501,180,101)
    waitUntil = time.time()+ 120
    while time.time() < waitUntil:
        if rewardicon.exists(reward image,0):
            time.sleep(1)
            doubleClick(pointA)
            time.sleep(1)
            doubleClick(pointA)
            time.sleep(1)
            doubleClick(pointA)

#different rewards and what actions to take with each

            if regphone.exists(Image):
                regphone.click(Image)
                time.sleep(0.5)
                break
            if regphone.exists(Image):
                regphone.click(Image)
                time.sleep(0.5)
                click(PointB)
                time.sleep(0.5)
                break
            elif region.exists(Image,0):
                regphone.click(Pattern("1490061651602.png").similar(0.61))
                runes=runes+1
                time.sleep(0.5)
                break
            break

#Failed scenario (no reward)

        elif reg2.exists(failed image,0):
            time.sleep(1)
            click(PointA)
            time.sleep(1)
            break

#Press End scenario to reach start scenario page
    reg3 = Region()
    if reg3.exists(image):
        time.sleep(1)
        click(Replay)
#If require top up of Energy
    reg4 = Region()
    if reg4.exists(Image):
            time.sleep(1)
            click(PA)
            click(Replay)
            time.sleep(1)

Question information

Language:
English Edit question
Status:
Solved
For:
Sikuli Edit question
Assignee:
No assignee Edit question
Solved by:
Josh
Solved:
2017-03-27
Last query:
2017-03-27
Last reply:
2017-03-21
RaiMan (raimund-hocke) said : #1

sorry, not possible to see, where you might have a problem, based on such code snippets.

If a script does not respond anymore this is usually due to an infinite loop, that only has an expected workflow, but does not have any precautions for not expected situations.

When using exists(), you have to be careful, since you do not get any feedback about problems with FindFailed exceptions.

So in doubt every

if exists():

should have a corresponding
else:

where you take corrective actions or break a loop with related information.

Another option is to use the user logging feature, to track your workflow.

BTW: (version 1.1.1):
         if regphone.exists(Image):
                regphone.click(Image)

can be replaced by:
         if regphone.exists(Image):
                regphone.click() # clicks lastmatch in regphone

Josh (joshtan11) said : #2

yea i realised my script doesnt provide feedback. just started picking up coding through sikulix, so making many basic mistakes :( haha thanks for the advice Raimund!!