Loop script, tried but can't understand it

Asked by Mike

Really sorry but i am having a hard time understanding the information i have been given and looked up about looping sikuli scripts

i am a bit slow and have trouble understanding theory.....

I have tried many scripts about looping and nothing is working for me.....

All i wish to do is loop my script that works very well... and i tried to add the stuffs so that if this image exsits it'll brake the loop and stop, then else continue.

So what i wanted to do is stop when see's image, otherwise continue..... below is the script i manage to do and wanted it done with.

# This makes sure your in Space Base
click( )
wait(2)

# This refuels your fleet
click( )
wait(2)
click( )
wait(2)
click( )
wait(2)
click( )
wait(2)

# This starts your Instances
click( )
wait(2)
click( )
wait(2)
click( )#Instance Lvl
wait(2)
click( )
wait(2)
click( )#Fleet1
wait(1)
click( )#Fleet2
wait(1)
click( )#Fleet3
wait(1)
click( )#Fleet4
wait(1)
click( )#Fleet5
wait(1)
click( )#Fleet6
wait(2)
click( )
wait(2)
click( )
wait( , FOREVER)
click( )

#This will sort Instances mail and box
click( )
wait(2)
click( )
wait(2)
click( )
wait(2)
click( )
wait(2)
click( )
wait(3)

I have tried stuff like:

# --- this loops forever
while True:
   if not exists( , 0):
      break # but ends if img gets visible
      popup("UNDER ATTACK!!!")
   else:
      pass # we will see
   wait(5) # check every 5 seconds
print , "finally came up"

But it would not loop, could someone please help me out and add to the script in the way it needs to be to make it loop non stop and brake when it see the right image..... I have been at it for 12hrs, yet i still not managed to get it to loop...

Any help on this would be greatly thanked.....

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
Mike (mikewebbcp) said :
#1

I have finally figured it out, got it too loop script and then break if under attack, sorry for being a pain about this today.......

I have loads i want to do with it by adding more to it, i hope i can work it out. But i have saved it as a working type and will copy to another saved name when working on it some more.

Below is the working script and its AWESOME......!!! only thing i know that doesn't happen is popup("UNDER ATTACK!!!") and i believe thats because i got break, so stops there.

I plan to, do another small script where break is and get rid of it, to replace with new script when done. One small question is, would it be possible that when i have while exist( ,0): then my new script i want to do. can i get it to link back in once that small script is done and do the whole thing over again.....

# --- this loops forever
while exists( , 0):
      break # but ends if img gets visible
      popup("UNDER ATTACK!!!")
while not exists( , 0):
   click( )# This makes sure your in Space Base
   wait(2)

   # This refuels your fleet
   click( )
   wait(2)
   click( )
   wait(2)
   click( )
   wait(2)
   click( )
   wait(2)

   # This starts your Instances
   click( )
   wait(2)
   click( )
   wait(2)
   click( )#Instance Lvl
   wait(2)
   click( )
   wait(2)
   click( )#Fleet1
   wait(1)
   click( )#Fleet2
   wait(1)
   click( )#Fleet3
   wait(1)
   click( )#Fleet4
   wait(1)
   click( )#Fleet5
   wait(1)
   click( )#Fleet6
   wait(2)
   click( )
   wait(2)
   click( )
   wait( , FOREVER)
   click( )

   #This will sort Instances mail and box
   click( )
   wait(2)
   click( )
   wait(2)
   click( )
   wait(2)
   click( )
   wait(2)
   click( )
   wait(3)

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

Congratulations.

--- Tip: if you want to comment your script, just put a sort action name behind the statement like
click () #action1

so you can tell e.g.

when my script has reached the end, I want to start all over again at #action1

--- break things up in smaller pieces
To have a better overview over your workflow, it helps, from start to use references to pieces of code, that are rather stable and that live somewhere else in the script: functions. The functions are defined with def a_name(): followed by the code, that should be processed, when this function is called later on using a_name(). So the same piece of code can be reused at different places all over again, even with different parameters.

your code revised with functions:

def refuelFleet() # This refuels your fleet
    click( )
    wait(2)
    click( )
    wait(2)
    click( )
    wait(2)
    click( )
    wait(2)

def startInstances() # This starts your Instances
    click( )
    wait(2)
    click( )
    wait(2)
    click( )#Instance Lvl
    wait(2)
    click( )
    wait(2)
    click( )#Fleet1
    wait(1)
    click( )#Fleet2
    wait(1)
    click( )#Fleet3
    wait(1)
    click( )#Fleet4
    wait(1)
    click( )#Fleet5
    wait(1)
    click( )#Fleet6
    wait(2)
    click( )
    wait(2)
    click( )
    wait( , FOREVER)
    click( )

def sortMail() #This will sort Instances mail and box
    click( )
    wait(2)
    click( )
    wait(2)
    click( )
    wait(2)
    click( )
    wait(2)
    click( )
    wait(3)

# This is your workflow with no changes
while exists( , 0):
    break # but ends if img gets visible
    popup("UNDER ATTACK!!!")
while not exists( , 0):
    click( )# This makes sure your in Space Base
    wait(2)
    refuelFleet() # This refuels your fleet
    startInstances() # This starts your Instances
    sortMail() #This will sort Instances mail and box

# concentrating on the workflow

now the only piece of code we have to deal with, to solve the workflow problem:

# This is your workflow
while exists( , 0):
    break # but ends if img gets visible
    popup("UNDER ATTACK!!!")
while not exists( , 0): # looking for image1
    # section1
    click( )# This makes sure your in Space Base
    wait(2)
    refuelFleet() # This refuels your fleet
    startInstances() # This starts your Instances
    sortMail() #This will sort Instances mail and box
    # section1 end

I guess once more:
- as long as image1 is not visible, perform section 1
- if image1 gets visible, do section2
- after section2 is done, start all over again

I try:

while True: # loops forever
    # label1
    if exists( , 0): # looking for image1
        #section 2
        pass # a no operation to keep indenting syntax
        # replace with whatever code you want
        # section2 end
    # section1
    click( )# This makes sure your in Space Base
    wait(2)
    refuelFleet() # This refuels your fleet
    startInstances() # This starts your Instances
    sortMail() #This will sort Instances mail and box
    # section1 end
    if some_condition: break # breakout
    # label2

# here script will continue if break is processed

comment:

1. script will start at label1 and process the statements till label2
2. then it will start again at label1
3. this will happen forever (while True:), except one of the finds in the clicks fail (script stops with FindFailed)

statement breakout:
some_condition has to be substituted by an expression, that evaluates to either True or False (like e.g. exists(image)). If True, it will end the loop, if False, break is not processed.

if True: break # will end the loop in any case
if False: break# will never end the loop

examples for conditions:
- some image is visible
- a certain time of day is reached
- a counter or timer has reached a threshold value
- many more ...

If you have only one break in the while loop, the condition might replace the True in while True:, since the while statement generally is:
while some_condition:
where some_condition is evaluated, whenever processing reaches the while statement.

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

I am back online in ten days.

Can you help with this problem?

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

To post a message you must log in.