function time() not reliable ?

Well this is strange,

When my warrior is hit, I want him to be immune for a couple seconds, so I save the timestamp of the last hit.

But the recent timetstamp is smaller than the previous one :eek:
(time( ) returns the number of seconds elapsed since 1970. It is an ever increasing number, or should be)

After a lot of mind boggling and not working, I Google about the function and find this :

Return the time in seconds since the epoch as a floating point number. Note that even though the time is always returned as a floating point number, not all systems provide time with a better precision than 1 second. While this function normally returns non-decreasing values, it can return a lower value than a previous call if the system clock has been set back between the two calls.

So what am I supposed to do now ? How can this be ?

Can you show a simplified version of your code?

Don’t store time.time() in an object property. An object property can’t store a big enough number to represent the time correctly!


  • Subtract a massive number from the time. This will make it work for a couple years
  • Store the number somewhere else (class, module, globaldict etc.)
If Collision.positive:
    agent = Collision.hitObject
    if agent['ImpactTime'] ==0:
        agent['ImpactTime'] = (60*5) 
        agent['Health'] -= own['Damage'] 

have logic in the agent

if ImpactTime >0 - - - - - - - - and-----------ImpactTime add -1

(sensor object checks to see if ImpactTime property in hitObject is zero,)
(ready to be hit)

If ImpactTime is zero, apply damage and set ImpactTime to the number of desired Frames.

Each agent needs python or logic to reduce the property ImpactTime to zero,
(agent resets self)

One could also have logic to place the player in a stun state,

My hit sensor actually looks for a int ‘block’ in the hitObject as well,

(block is zero the first frame of blocking, I use this data for intercepting block throws =D)

In my own project, I spawn generic sensor objects, and tell them who owns them, and what property they are looking for on spawn.

count based on framerate and delta time

rate = 1.0
delta = bge.logic.getFrameTime()

own["Timer"] += rate*delta

EDIT: this is a guess, not tested

Thank you people,

BluePrintRandom, your solution is good, in fact I use it extensively when real time does not matter
(for example to shake the camera a little, or for a diamond to shine somewhat, etc).

I wanted something with time. But you are right, I will revert to a frame count solution, it is much simpler.

You can also use blender’s time function, which I believe does the same as Daedalus_MDW’s code snippet. See: