EndAnimationObs Death loop

I don’t know exactly when it come but i have update with the last version.
But now If i use a onAnimationLoopObservable.addOnce and inside the callback i call the stop on this animation it create a death loop.
Is like the once unsubscribe at the end of the callback now.
Why previously it was at the start of the call, and the observeable can recall itfself propagated by the inner stop.

chatGPT your assistant in all questions. I hope the explanations and examples are working fine.

GPT:
Starting from Babylon.js version 5.0, the behavior of the addOnce method for observables has been changed to ensure that the observable is unsubscribed at the start of the callback instead of at the end. This is to prevent the observable from being called again in case the callback function happens to add a new callback to the same observable.

This change may cause issues in some scenarios, like the one you described, where the stop() method is called inside the callback function of the observable. Because the observable is unsubscribed at the start of the callback, calling stop() will no longer prevent the observable from being called again, leading to an infinite loop.

To work around this issue, you can use the add method instead of addOnce to register your callback function with the observable, and then manually unsubscribe from the observable inside the callback function using the remove method.

Here’s an example of how you can modify your code to use the add method and manually unsubscribe from the observable:

csharpCopy code

const animatable = scene.beginAnimation(mesh, 0, 100, true);
scene.onAnimationLoopObservable.add(() => {
    animatable.stop();
    scene.onAnimationLoopObservable.remove(callback);
});

@Evgeni_Popov could it be a regression from the latest changes on Observable ?

@CodingCrusader we really need to review those answers before posting them :slight_smile: half of it is always great but the other half is scary :slight_smile:

1 Like

@Perpleex could you try with the latest version as we fixed something about it recently ?

I was on 5.43.2 and i have upgraded to 5.53.0 where the problem append for me.
He can be between the two.

Do you mean the problem still happen in 5.53.0?

Yes the problem still happen in 5.53.0

Would you be able to setup a repro in the Playground? It would be easier for us to look for the problem.

Thanks!

Yup we definitely need a repro as this seems to be ok https://playground.babylonjs.com/#0CQSZB#1

Actually, I think the repro would be:

But it still works.

I was using bone groupAnim i can repro the bug like this :

i have commented the stop it’s full freeze after the 10 second anim.
i have use the onAnimationEndObservable called when the play end.

Work with onAnimationGroupEndObservable it’s something specific with onAnimationEndObservable.

Thanks for the repro! Here’s the fix: