# Why animation group master frame doesn't change for 1 step after starting it when lockstep is active?

**URL:** <https://forum.babylonjs.com/t/why-animation-group-master-frame-doesnt-change-for-1-step-after-starting-it-when-lockstep-is-active/50861>\
**Category:** Questions\
**Tags:** animation, animations, animationgroups\
**Created:** [May 27, 2024, 1:59pm UTC](https://forum.babylonjs.com/t/why-animation-group-master-frame-doesnt-change-for-1-step-after-starting-it-when-lockstep-is-active/50861 "2024-05-27T13:59:52Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![AmirHo3ein](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/amirho3ein/32/32020_2.png) [@AmirHo3ein](https://forum.babylonjs.com/u/AmirHo3ein)\
**Post date:** [May 27, 2024, 1:59pm UTC](https://forum.babylonjs.com/t/why-animation-group-master-frame-doesnt-change-for-1-step-after-starting-it-when-lockstep-is-active/50861/1 "2024-05-27T13:59:52Z")

</div>

Hi. I’m having a problem understanding why this is happening. I don’t know if it is a bug or not. I know it’s not a big deal but I’m currently implementing a rollback networking model for a fighting game and in this model, every frame (step) counts. this is an [example PG](https://playground.babylonjs.com/#Z6SWJU#1020).  
if you open the console you can see that the master frame of the currently playing animation group which is “Samba” doesn’t change after step 0. Is there a workaround for this so that after step 0 the master frame changes to 1?

---

<div class="post-metadata">

**Author:** ![sebavan](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/sebavan/32/57_2.png) [@sebavan](https://forum.babylonjs.com/u/sebavan)\
**Post date:** [May 27, 2024, 4:39pm UTC](https://forum.babylonjs.com/t/why-animation-group-master-frame-doesnt-change-for-1-step-after-starting-it-when-lockstep-is-active/50861/2 "2024-05-27T16:39:50Z")

</div>

Assigned @Evgeni\_Popov on May 27, 2024, 4:39pm UTC



---

<div class="post-metadata">

**Author:** ![Evgeni\_Popov](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/evgeni_popov/32/4432_2.png) [@Evgeni\_Popov](https://forum.babylonjs.com/u/Evgeni_Popov)\
**Post date:** [May 27, 2024, 6:50pm UTC](https://forum.babylonjs.com/t/why-animation-group-master-frame-doesnt-change-for-1-step-after-starting-it-when-lockstep-is-active/50861/3 "2024-05-27T18:50:12Z")

</div>

`onBeforeStepObservable` is run right at the start of a step, before the animations have been processed. You should use `onAfterAnimationsObservable` if you want to add code that runs after animations have been processed:

> <https://github.com/BabylonJS/Babylon.js/blob/master/packages/dev/core/src/scene.ts#L4473-L4491>

---

<div class="post-metadata">

**Author:** ![Evgeni\_Popov](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/evgeni_popov/32/4432_2.png) [@Evgeni\_Popov](https://forum.babylonjs.com/u/Evgeni_Popov)\
**Post date:** [May 27, 2024, 6:50pm UTC](https://forum.babylonjs.com/t/why-animation-group-master-frame-doesnt-change-for-1-step-after-starting-it-when-lockstep-is-active/50861/4 "2024-05-27T18:50:15Z")

</div>

Unassigned @Evgeni\_Popov on May 27, 2024, 6:50pm UTC



---

<div class="post-metadata">

**Author:** ![AmirHo3ein](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/amirho3ein/32/32020_2.png) [@AmirHo3ein](https://forum.babylonjs.com/u/AmirHo3ein)\
**Post date:** [May 28, 2024, 7:37am UTC](https://forum.babylonjs.com/t/why-animation-group-master-frame-doesnt-change-for-1-step-after-starting-it-when-lockstep-is-active/50861/5 "2024-05-28T07:37:19Z")

</div>

@Evgeni_Popov if you check [the PG](https://playground.babylonjs.com/#Z6SWJU#1020) you can see that there are 2 logs on each step. one `onBeforeStep` and one `onAfterStep` and the `onAfterStep` observers get notified even after `onAnimationsEnd` observers.

 ![image](https://us1.discourse-cdn.com/flex024/uploads/babylonjs/original/3X/b/3/b33193052247baa2bb0ded965fd0840b2afd4a17.jpeg)

here is a screenshot and as you can see the master frame of the animation group after step 0 is still 0. I also test this on different steps. when you stop an `AnimationGroup` and then start it in `onBeforeStep` on another step, the master frame doesn’t change for 1 step

---

<div class="post-metadata">

**Author:** ![Evgeni\_Popov](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/evgeni_popov/32/4432_2.png) [@Evgeni\_Popov](https://forum.babylonjs.com/u/Evgeni_Popov)\
**Post date:** [May 28, 2024, 8:28am UTC](https://forum.babylonjs.com/t/why-animation-group-master-frame-doesnt-change-for-1-step-after-starting-it-when-lockstep-is-active/50861/6 "2024-05-28T08:28:00Z")

</div>

You should not delete and recreate the engine inside the scene, as the Playground is still using the engine it created and not the new one. You should provide a `createEngine` function instead:

> **[Babylon.js Playground](https://playground.babylonjs.com/#Z6SWJU%231023)**
>
> Babylon.js playground is a live editor for Babylon.js WebGL 3D scenes

With this change, the animation plays at the right speed and I get:

![image](https://us1.discourse-cdn.com/flex024/uploads/babylonjs/original/3X/a/7/a7739d472708023b502f3ab9ecdbb9dad6970186.png)

There are a few 0 at the start because the animation code returns early if there are still some pending operations in the scene, like loading of textures. You can get rid of them if you wait for the scene to be ready:

> **[Babylon.js Playground](https://playground.babylonjs.com/#Z6SWJU%231024)**
>
> Babylon.js playground is a live editor for Babylon.js WebGL 3D scenes

With this change, I get:

![image](https://us1.discourse-cdn.com/flex024/uploads/babylonjs/original/3X/2/4/24f02632525ebb5bf788d9037ab8a5a4fe81cc11.png)

Which is the expected output:

- the first time the animation code runs, the delta time is 0 (because some internal `lastTime` variable is set to the current time, meaning `currentTime-lastTime==0`) so you get `masterFrame = 0` in `onAfterStep`
- the second time, delta time is \> 0, but you get 0 in `onBeforeStep` because this observable is triggered before the animation code runs. However, `onAfterStep` is called after the animation code, so you get the right value for `masterFrame`

Does this make sense?

---

<div class="post-metadata">

**Author:** ![AmirHo3ein](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/amirho3ein/32/32020_2.png) [@AmirHo3ein](https://forum.babylonjs.com/u/AmirHo3ein)\
**Post date:** [May 28, 2024, 9:09am UTC](https://forum.babylonjs.com/t/why-animation-group-master-frame-doesnt-change-for-1-step-after-starting-it-when-lockstep-is-active/50861/7 "2024-05-28T09:09:47Z")

</div>

@Evgeni_Popov Yeah, that makes sense. now I want to know is there a workaround for this? because as I said I’m implementing a rollback networking model for a fighting game and this would cause a desync problem. Would it be possible to provide a solution so that when I start the “AnimationGroup” in the “onBeforeStep” observer, the main frame of the “AnimationGroup” would increase in the “onAfterStep” observer of the same step?

---

<div class="post-metadata">

**Author:** ![Evgeni\_Popov](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/evgeni_popov/32/4432_2.png) [@Evgeni\_Popov](https://forum.babylonjs.com/u/Evgeni_Popov)\
**Post date:** [May 28, 2024, 4:58pm UTC](https://forum.babylonjs.com/t/why-animation-group-master-frame-doesnt-change-for-1-step-after-starting-it-when-lockstep-is-active/50861/8 "2024-05-28T16:58:30Z")

</div>

What you can try to do is to set the `Animatable._localDelayOffset` property to 0 after you started the animation group (lines 69-71):

> **[Babylon.js Playground](https://playground.babylonjs.com/#Z6SWJU%231025)**
>
> Babylon.js playground is a live editor for Babylon.js WebGL 3D scenes
