Oscillating (forwards - backwards) movement in BGE/Python: How to?

I tried a few things, but nothing works so far.
I just need a cube to travel along a path, and at the end it should just reverse the movement and travel back to the origin. A “U”-shaped path does not solve it, I need the cube to not rotate, but to keep the same orientation all the time.

I tried this:

def reverse_playback(scene):

if scene.frame_current == 100:
#bpy.ops.screen.animation_play()#this just stops
bpy.ops.screen.animation_play(reverse=True)

bpy.app.handlers.frame_change_pre.append(reverse_playback)

But it just stops the animation.
And having both animation_play() activated crahes Blender?..

Many thanks for help!

Why not continue the animation from start - end - start? if it doesn’t change i think it should be fine. That way you can just repeat the animation. Animate the cube from the start point, to end, then back to start. Your orientation wouldn’t be changed either.

OK, its working when I define a “Follow Path” constraint.

if own['Flip']==False:
    if own['Float']<Max:
       
        own['Float']+=AnimRate
    else:
        own['Float']=Max
        own['Flip'] = True
else:
    if own['Float']>Min:
         own['Float']-=AnimRate
    else:
         own['Float']= Min
         own['Flip'] = False

this will oscillate a value
just use it with a actuator in property mode, or replace the min and max in play action with own[‘Float’] and set the play speed to zero.

You should not use the “bpy” module in bge.

That said BPR answer is probably good enough for you, but actually there is a more complete answer for various kind of triangular oscilations (for sinusoidal just use the sinus or cosinus): http://stackoverflow.com/questions/1073606/is-there-a-one-line-function-that-generates-a-triangle-wave

That page is for C but porting that to Python is trivial (those are one line of code).

you can use the action key interpolation mode like bounce and all sorts of things to use actions to produce waveforms etc.

Yep but using actions for this kind of thing seems like a waste in my opinion. Unless you don’t know Python at all, it’s not easier to make the same efect with an action. (For me would at least be more time consuming)