how to wait in run-time

so i want to create a waiting method in my run-time of game for specifics. example, a 10 second period between when you can open door and close door…

i try to use sleep by this code:

import time


but this causes bge to lag terribly to a point where it basically crash…

so then I think, i will write method that takes in time, then loops for as long as time is multiplied by 60 (for 60 logic ticks per second)


j = 0
    for x in range(seconds * 60):
j += 1

the code above does loop through the correct amount of times, as proven by the debug print statements in the code (the statements involving variable j). however, it does not seem to actually wait that long, as i can open and close door instantly regardless of loop method. my thinking was… call to method would keep code “trapped” inside it and teh loop for all of loop time… but clearly my understanding is wrong. can somebody tell me correct way to use?

Exactly that. I suggest you read the first couple of guides you can find in my signature. With that you should be able to discover why you get these effects and with a little thinking you will discover a (simple) solution.

yes monster, i read many of your great guides!

specifically now i re-read guide “Gameloop” “Coding in python” and “import external modules” but i am still not seeing this simple solution :(. it think maybe I not even understand your guides it seems!

i was thinking also to try and use delay sensor, but doesn’t delay only work for using the script initially, wheresa what i need is more of internally a way to use delay sensor in middle script.

Blender waits for all your scripts to finish.
If your scripts don’t end then the next frame won’t be drawn.

You would have to probe every frame the time.

time.time() returns the current time.
Store the starting time.time() in a property.
own[“start”] = time.time()
Then check if time.time() - own[“start”] >= 5 every frame.

You look in the script editor, there is a button named templates, look at the gamelogic_simple example.
It shows how to use logic bricks in python.

The answer is: the BGE never waits.

As it uses an event system you need to measure a delay … surprisingly there is an delay sensor ;).

Delay sensor + state switch
I guess your next question is when to start the delay?

The delay sensor starts measuring the duration on activation. This is typically on object creation. But it also happens when switching the state.

state 0: sensing the event when to start the duration -> set state 1
state 1: delay sensor (with LEVEL) -> do whatever you want to do after the delay

Alternative (timer property):
Use a timer property.
When starting the delay set the property to - delay (in seconds). It will start counting the seconds from that value.
Sense with a property sensor you can sense when the timer is above zero (interval mode !!!)


yes i try using timer property, to reset timer to 0… then when timer == value it would have to start, butt i will try it again…

also, why does it happen that sleep() crashes BGE? is it because it goes against the functionality of constantly running script with no breaks like you guys just explaint o me.

also, thank you guys for helping me understand. evenyday i leanr more in blender,

sleep() should not crash at all. It as the name says send the current process to sleep(). As this is the process with the game loop would send the complete game to sleep() including screen update.

As this is exactly what you requested (getting a wait), I think what you meant is:
How to sense an event after a delay after another event (while the the game keeps running)

Be aware evaluating a timer property on a specific value will not work as it is very unlikely that you can determine the precise number the timer will become. It is better to check a range.

okay thanks monster, i try with timers again and hopefully can get it to work

You can use this setup for easy results:

 #note that timers will start counting from the start of the game
if own['time'] > 2 (seconds): 
#reset the timer to make sure there is a gap or as you say "wait"
 own['time'] = 0 
 #rest of your code in here that you want to happen after a 2 second or whatever gap.

I give you this code, but you really should understand why the method you tried failed before just implementing this concept.

Instead of waiting (aka polling), make it event based. If the user tries to open the door, that’s when you check the time, versus checking the time every frame and setting some flag. On the initial contact, you mark the initial time, and when the user checks, you just compare the current time against the initial time + delay.