Move an object with bge script

I must do a program where there is an object that moves forward and backward.
I have created a script which takes from a file.txt a numbers sequence.
If it reads:
0 : go forward
1: go backward
2: stop

Can I write a script where I give a start position, an end position and a time for moving the object from start position to end position in (for example) 3 seconds? If it is possible, how can I do it?

My script is:

import bge

def main():

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

filename = open(‘filedati.txt’,‘r’)

line = filename.readline()

for word in line.split():
spostamenti = [int(e) for e in line.split()]

for i in spostamenti:
if spostamenti[i] == 0:

…script to move forward from [0,0,0] to [0,2,0] in 3 seconds…

elif spostamenti[i] == 1:

…script to move backward from [0,2,0] to [0,0,0] in 3 seconds…

elif spostamenti[i] == 2:

…script to stand still in the position in which the object is located…


You would have to fetch the number of commands and divide that by time. So if you have 10 commands and 10 seconds a command would need to execute every second.

You will need to monitor the current frame and FPS and measure time passing internally to determine when commands need to be run.

    avg_fps = logic.getAverageFrameRate()
    if avg_fps < 90:
        # When the BGE first starts up the average fps can be quite high.
        # Only begin processing after it has dropped down from one million.

        current_tic = current_tic + 1
        if avg_fps != 0:
            if current_frame == 10:
                # Time to render command for frame #10.
            if current_frame == 20:
                # Time to issue command for frame #20.
                if current_tic > (goal_fps/avg_fps):
                    current_tic = 0
                    print("frame #%f, Goal FPS:%f, Average FPS:%f" % (current_frame, goal_fps, avg_fps))
                    current_frame = current_frame + 1

To complicate things, time is based upon the average FPS the BGE can achieve on any one specific hardware set. So the results may differ when run on a different machine.

For an exact 3 second animation, just keyframe it.

Thank you very much for your help. But I can not figure out where I have to enter your piece of code in mine. Can you help me? Sorry but I am a beginner with the Python programming :frowning: