How to delay scene change?

I’m working on a little game (my first semi-complete one, which I was hoping to make a thread in the WIP/complete forum about as soon as this evening… ), and have run in a little problem. I have set up the logic bricks for two events - losing and winning. Each of those would start a new scene (basically, just to show “you win”/“you lose” text, and let player choose whether to play again or quit game) via Set Scene actuator, however, I vant the scene to change a short while after the win/lose conditions are fulfilled. Say, with 5 second delay or so.
What I tried:
First of all, I figured that win/lose conditions should add an object with a Timer property. Then, set up a sensor to change the scene as soon as the timer gets to 5. For some reason this crashed blender. I even tried to “fake” a timer - by giving the ‘timer’ object an integer property which is initially 0, and is constantly increased by 1 with an Always sensor set to pulse delay of 20 logic ticks. Same result - blender crashes.
My other attempt was using a python function:

import time
time.sleep(5)

(that is not the whole script, of course, I’m leaving the rest of it out, which is just typical getCurrentController/getActuator/AddActiveActuator stuff)
This did not crash blender, however, it sort of “froze the time” in the game scene. I thought sleep() was supposed to suspend the execution of the script itself for the indicated duration - apparently it pauses game as well. :confused: After this, scene changed, but still, the “time freeze” effect is not what I was looking for.
So, yeah, as often as I’ve had to say this: help, anyone? :o

time.sleep() will cause the BGE to hang, in the same way that this will:


while 1:
        print "This will freeze the game"

You need to keep track of time, without getting the entire game stuck in a loop.

You can do this with a timer property, or, you can time it yourself with time.time().
Here is a rough example of what to do.


# myCondition is true when you want to start the timer.
import GameLogic as gl
import time

if not hasattr(gl, "myStartTime"):
        gl.myStartTime = time.time()

if not myCondition():
        gl.myStartTime = time.time()

diff = time.time() - gl.myStartTime

if diff >= 5.0:
        # Change Scene Here