While loops and the BGE

Hi,

I’m having some difficulty getting the desired result from a while loop in the game engine. What is happening is that the while loop fully iterates before the game engine continues. Is there a way to release the game engine to continue during the calculation of the while loop?

Heres the code:

import bpy, bge, time, math

cont = bge.logic.getCurrentController()
act = cont.actuators[“motionX”]
own = cont.owner
sens = cont.sensors[0]
xLoc = 21.298
crMaxAccX = 0.5 #m/s^2
crMaxVelX = 0.3 #m/s
crVelX = 0.0
crLoc = bpy.data.objects[str(own)].location.x
distToLoc = xLoc - crLoc

turns on the controller

cont.activate(act)

timePrev = time.time()
#print (timeStart)

def craneAccel(crVelX, crMaxVelX, crMaxAccX, timePrev):

    crVelX += (crMaxAccX * (time.time() - timePrev))
    print("time:",time.time()-timePrev) 
    return crVelX 

while (crVelX < crMaxVelX) and (distToLoc > (math.pow(crVelX,2) / (2 * crMaxAccX))):

crVelX = craneAccel(crVelX, crMaxVelX, crMaxAccX, timePrev) 
act.dLoc = (crVelX,0.0,0.0) 
distToLoc = xLoc - crLoc 
timePrev = time.time()
time.sleep(.1)

Not the right spot, but anyway.

Don’t use while loops. Or more specifically, dont use time.sleep(1).

You’ll have to do this another way.