creating a timer in sikuli, need update from old code.

Asked by dizarus on 2019-12-09

Hello, i was looking for a timer that i could use, i found the 2012 post and the code raiman used on post#8 seemed like what i need but ia can't seem to get it to work.

here is my code:
common ="common.png"
rare ="rare.png"
brare ="brare.png"
tresure ="tresure.png"

Settings.timer =time.time()
start = time.time()
runn =True

def stop(event):
    global runn
    runn =False
Env.addHotkey(Key.F1,KeyModifier.ALT, stop)

def timer(name, elapse=0):
    if elapse >0:
        exec(Settings.timer+ str(1) + "=time.time()+" + str(elapse))
        rest =eval(Settings.timer + str(1)) - time.time()
        if rest >0:
            return False
            return True

while runn:
    print "counting"
    if timer(1,2):
    elif exists(rare):
    elif exists(brare):

    while timer() and runn:

print "Elapsed: %d seconds"%(int(time.time()-start))

The error it is giving me is:
error] TypeError ( unsupported operand type(s) for +: 'float' and 'str' )
[error] --- Traceback --- error source first
line: module ( function ) statement
18: main ( timer ) exec(Settings.timer+ str(1) + "=time.time()+" + str(elapse))
27: main ( <module> ) if timer(1,2):

really appreciate the help.

Question information

English Edit question
Sikuli Edit question
No assignee Edit question
Last query:
Last reply:
Manfred Hampl (m-hampl) said : #1

"Settings.timer =time.time()"

time.time() Returns a numeric value (seconds since January 1, 1970, 00:00:00 UTC ).
Consequently your expression
Settings.timer+ str(1) + "=time.time()+" + str(elapse)
does not make any sense, because it tries to concatenate a numeric value and text values, which is forbidden.

RaiMan (raimund-hocke) said : #2

exec need a string, that represents an executable statement/expression.

It looks like you want to execute something like this in your function:

Settings.timerX = time.time()+nnnn.nnn and
Settings.timerX - time.time()

where X is the value given by parameter name

So do it this way:

    if elapse >0:
        exec("Settings.timer%d = time.time() + %d"%(name. elapse))
        rest = eval("Settings.timer%d - time.time()"%(name))

but be aware:

must be set before first use like
Settings.timer =time.time()

which in turn is never used

dizarus (dizarus) said : #3

Thanks for the help, apologies as i am quite new and still having trouble getting it to work, i can get it to start up and click but it doesn't leave the loop and check the exists. Going to keep trying see if i can figure it out

Can you help with this problem?

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

To post a message you must log in.