Python time delay within script???

hey,

Is there some way of having a time delay within a script somehow? What I mean is, when the user clicks something, how do I call a function like a couple of seconds later? And have control over the time it takes to call the function.

I tried

import time

time.sleep(1)

but this stops blender to stop, not just a delay within the script. I’m sure there is a relatively easy way to do this, just cant find anything on it.

cheers

Never stop a python controller. It stops the whole game.

Better use

  • a delay sensor or
  • a timer property with property sensor

Or, if you’re doing this inside of a function and don’t want to wait until the timer’s up to execute the script, use a variable and increment it every frame.


obj = cont.owner

if not 'init' in obj:
     obj['init'] = 1
     obj['timer'] = 0.0

obj['timer'] += 1.0 / logic.getLogicTicRate()

if obj['timer'] > 10: # 10 seconds
     #Do something

2 Likes

also there a specific function in the sensors , is -> frequency (default=0)

if something:
cont.sensors[“Always”].frequency=10

but work only for this sensors , not for all, I think

All sensors have the attribute frequency

It sets up the delay between two consecutive pulses without a sensor’s state change. As this delay can be skipped on state change it is not really a good option for time measure.

Marco is right: the always sensor does not change state. So you could use it. Be aware the sensor will not stop to send pulses ;).

Good contribution SolarLune, thanks. It worked for me.

use a timer property, it’s accurate.

if i get 75 frames a second or you get 40 frames a second, it’s not the same time.(at least not when i tested it) but the timer property is.