life script questions

i wrote this simple life script and i tried to implement some kind of “time for restore of health”, for example if i am beaten and i’m still alive but my health value is low, if there is no collision for about two seconds, i want to restore my health
i thought to use python’s time library but i can’t think a way to do this.
here the code:

import bge, time
from bge import logic as l

controller = l.getCurrentController()
own = controller.owner

scene= l.getCurrentScene()
#print (int(time.time()))

if own.sensors[‘Collision’].status == bge.logic.KX_SENSOR_JUST_ACTIVATED and own[“health”]>= 1:
–own[“health”] -= 10
#print (own[“life”])

if own[“health”] < 30:
#restore health until it reach 30 value if there is no collision for 2 seconds

if own[“health”] <= 0:

A easy way is use a timer property to know if you passed 2 seconds after the collision.

collision = ....    
if own["health"] &gt;= 1:
    if collision.positive:
        own["health"] -= 0.5
        own["timer"] = 0
        if own["timer"] &gt; 2 and own["health"] &lt; 100:
            own["health"] += 0.5

Here an example: health.blend (446 KB)

Note: you post your script using a quote, use code tags, also a more easy way to know if a sensor is activate, is use sensor.positive.

thanks a lot it work im kind of new in the comunity and i’m still learning how to post correctly.

ok that’s work correctly but, just for curiosity and learning, do you know how to do this with time library?

You can use time.time() on object property ‘lasthit’ on every collision. Then check time.time() - lasthit > 2 * 1000 and health < 30: health += regeneration

Very simple and neat method.

ok thank you