Time based if statements

Asked by Smizzy

I was wondering if it was possible to have a specific argument that is only true after a 30 second delay from its last time run
for example

if exists(image.png) and 'time since last run' > 30:
    #bunch of functions and stuff
else:
    pass

So in my dream script, this if is running in a loop but i can set delays on specific lines while not stopping it from doing the other lines.
I cant use wait(30) because then it would just stall the script for 30 seconds and would stop it from running the rest of the loop while waiting.

Question information

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

# the principal solution
start = time.time() # set the current time

while some_condition: # start loop
    # some code
    if exists(image) and (time.time() - start) > 30:
        start = time.time() # reset to current time for next lap
        # do domething
    else:
        pass
    # more code
# after loop

Revision history for this message
Smizzy (mad-dugas) said :
#2

Thanks RaiMan, that solved my question.

Revision history for this message
Smizzy (mad-dugas) said :
#3

Are "start" and "time." set names with existing presets or are they just generic names? Could I rename start to something else?

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

time.time()
is a function from the Python module time, that is already pre-imported in Sikuli scripting and returns the current time as a long integer (I think seconds counted since beginning of 1970)

So you might store as many "now" values as you like to variables named as you like.
Hence start can be renamed as you like.

Revision history for this message
Smizzy (mad-dugas) said :
#5

Thanks! Ive got this attractive looking setup now!

#outside the loop
delay1 = 10
timer1 = -delay1

#inside the loop
if (time.time() - time1) > delay1 :
        crafter(w1[0],w1[1],w1[2],w1[3],w1[4])
        time1 = time.time()

That way it runs the first time without delay but then doesn't check it again for 10 seconds. And the rest of the loop is running smoothly while this function is on cooldown :D
Thanks!!!!!