Loop for which lasting 10 seconds

Hi,
I would like to create a loop for or while which lasting for example 10 seconds, as the examples below:

For i in 1 to 10 seconds
Make my actions

While t<10 seconds
Make my actions
t = t + 1

Does anyone have any idea how I can do it in Python?
Thank you for your help

save the time at the start and subtract it from the current time. Keep the operation running as long as the rest is below the time you wanna spendon the operation. You should design that operation to have short iterations, so the time is checked often enough. Or use threads.

import bpy
from time import clock
from random import uniform

verts = bpy.context.object.data.vertices
start = clock()

# infinite loop over vertices
def get_vert_gen(verts):
    while True:
        for v in verts:
            yield v

# get an iterator from generator function
get_vert = get_vert_gen(verts)

while clock() - start &lt; 10:
    # pull a vert and do something
    get_vert.__next__().co.z += uniform(-0.01, 0.01)

Do you know a script with bge module?
I really would like to avoid bpy module for my game.

Sorry but I don’t use bpy module but only bge module.
I managed to make my loop which lasts 5 seconds but I have another problem now: my object doesn’t move (action in the loop)

See my code:

import bge
import time

cont = bge.logic.getCurrentController()
obj= cont.owner

scene = bge.logic.getCurrentScene()
texte = scene.objects.get(“Americain football ball”)
print ("object: ", texte)

INITIAL POSITION

initial_position = obj.position
save_initial_position = initial_position.copy()
print ("saved position: ", save_initial_position)

INITIAL SCALE

initial_scale = obj.scaling
save_initial_scale = initial_scale.copy()
print ("saved scale: ", save_initial_scale)

TIME BOUCLE

start = time.time()
print (start)

while 1 :
end = time.time()
if end < start + 5 :
obj.worldPosition = [2, 2, 2] #CHANGING POSITION
obj.worldScale = [1.5, 1.5, 1.5] #CHANGING SCALE

else :
    print (end)
    obj.worldPosition = save_initial_position
    print ("final position :", obj.worldPosition)
    obj.worldScale = save_initial_scale
    print ("final scale :", obj.worldScale)
    break 

Thanks for your help

Your problem is that your loop is blocking the main game loop. You will find that your entire game will freeze for 5 seconds and then you will see your object in the final position.

To fix this problem you have to move your object a small distance each frame between ‘start’ and ‘end’ time. You need to store the ‘startTime’, ‘endTime’, ‘startPosition’, and ‘endPosition’. With that data you can tell where the object should be at any given frame.



# Note this code has not been tested.  Treat it as pseudo code that might (and probably does) contain syntax errors.
# Assumes 'startTime' and 'endTime' are floating-point time values.
# Assumes 'startPosition' and 'endPosition' are Vector objects.

import bge

# See if the animation has ended
currentTime = time.time()
endTime = bge.logic.globalDict["endTime"]
if endTime is None or currentTime &gt; endTime:
  # The animation has ended, don't update the location anymore.
  return

# See if the animation has started
startTime = bge.logic.globalDict["startTime"]
if startTime is None or currentTime &lt; startTime:
  return

# The animation is running, get the animation parameters.

startPosition = bge.logic.globalDict["startPosition"]
endPosition = bge.logic.globalDict["endPosition"]

# Do a linear interpolation (aka 'average') between the two locations.
percentComplete = currentTime / (endTime - startTime)
obj.worldPosition = (1.0 - percentCompelet) * startPosition + percentComplete * endPosition

# You can do a similar computation for the scale by storing the start+end scale values.


You would hook that script up to an “Always” sensor. It will get called for each frame in your game and will update the location to the correct spot based on the current time that the frame was rendered.

To start the animation off you might tie an Keyboard sensor to a script like this:



import bge

bge.logic.globalDict["startTime"] = time.time()
bge.logic.globalDict["endTime"] = bge.logic.globalDict["startTime"] + 5.0

bge.logic.globalDict["startPosition"] = obj.worldPosition
bge.logic.globalDict["endPosition"] = Vector( (your,final,location) )


Thank you for the idea.
But my problem is I don’t know the initial position of my object (it has 5 opportunities to position - random). And during this 5 seconds, I want it goes to (2,2,2) position. So I can’t move my object a small distance each frame.
What do you think?

When you create your object you can find its current location at ‘obj.worldPosition’.

So you spawn your object, read its “.worldPosition” to get your starting point, then interpolate to (2,2,2).

This is my game
https://drive.google.com/folderview?id=0B0uRx1cKHvaGekhkYXNFWjJDRmc&usp=sharing

My only problem is the following:
I can not move it from the box to the camera in one time! I want that the object leave the box (depending on the z-axis), and then comes to the camera (y-axis and z), stay 5 seconds front of the camera, and go back in the opposite direction in the box)

Someone can help me?

ps: I attached the python script only on the subject “Tennis”

You might try posting this in the “Game Engine Support & Discussion” forum. There are plenty of Python questions posted there and you will get more traffic.

I didn’t check your script, but the answer remains the same, you just have more state that you have to keep track of.

The code I supplied will move an object from one location to another over a time range. Now you need to do a series of movements, move the object out of your box, move it to the camera, hold still, move back, lower into the box.

That is just a series of moving from one point to the next.