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

Asked by dizarus

Hello, i was looking for a timer that i could use, i found the 2012 post https://answers.launchpad.net/sikuli/+question/211455 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:
#Timers
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))
    else:
        rest =eval(Settings.timer + str(1)) - time.time()
        if rest >0:
            return False
        else:
            return True

while runn:
    print "counting"
    if timer(1,2):
        print"timer1"
    elif exists(rare):
        timer(2,4)
    elif exists(brare):
        timer(3,6)
    else:
        pass

    while timer() and runn:
        click(tresure)
        print"waiting"

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

Language:
English Edit question
Status:
Answered
For:
SikuliX Edit question
Assignee:
No assignee Edit question
Last query:
Last reply:
Revision history for this message
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.

Revision history for this message
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))
    else:
        rest = eval("Settings.timer%d - time.time()"%(name))

but be aware:
Settings.timer1
Settings.timer2
Settings.timer3

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

which in turn is never used

Revision history for this message
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.