# ActionManager sequence break issue

**URL:** <https://forum.babylonjs.com/t/actionmanager-sequence-break-issue/55212>\
**Category:** Questions\
**Tags:** actionmanager\
**Created:** [December 5, 2024, 4:10pm UTC](https://forum.babylonjs.com/t/actionmanager-sequence-break-issue/55212 "2024-12-05T16:10:18Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Forsaken](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/forsaken/32/21296_2.png) [@Forsaken](https://forum.babylonjs.com/u/Forsaken)\
**Post date:** [December 5, 2024, 4:10pm UTC](https://forum.babylonjs.com/t/actionmanager-sequence-break-issue/55212/1 "2024-12-05T16:10:18Z")

</div>

I ran into the following issue using the Action Manager:

If two registered actions run, and 1 action deletes an action from the list. Then depending on the order of the actions, the actions registered past the one getting deleted, get skipped, regardless of them being deleted or not. I’m guessing because there is an internal array it’s iterating through, and the deletion is causing the index to get “offset”.

I’m not sure if this would be considered a bug, how would one go around this in a clean way?

Maybe a solution would be to keep a copy of the list of the actions before it goes through all the actions, and when we go over one we compare if it’s deleted or not. This way this issue would be avoided.

example:  
[https://www.babylonjs-playground.com/#8LFTCH#2534](https://www.babylonjs-playground.com/#8LFTCH#2534)

---

<div class="post-metadata">

**Author:** ![Tricotou](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/tricotou/32/5844_2.png) [@Tricotou](https://forum.babylonjs.com/u/Tricotou)\
**Post date:** [December 5, 2024, 5:28pm UTC](https://forum.babylonjs.com/t/actionmanager-sequence-break-issue/55212/2 "2024-12-05T17:28:43Z")

</div>

Indeed I agree that there is a strange behavior here.  
If you don’t mind, here is an edit of your PG with no external asset loading time :

> **[Babylon.js Playground](https://playground.babylonjs.com/#4U4QH9%23144)**
>
> Babylon.js playground is a live editor for Babylon.js WebGL and WebGPU 3D scenes

* * *

If you encapsulate the action unregister in a setTimeout with 1 millisecond, postponing this in the JS event loop, it works :

> **[Babylon.js Playground](https://playground.babylonjs.com/#4U4QH9%23146)**
>
> Babylon.js playground is a live editor for Babylon.js WebGL and WebGPU 3D scenes

But that’s not what I would call a “fix” 🤔

---

<div class="post-metadata">

**Author:** ![RaananW](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/raananw/32/28955_2.png) [@RaananW](https://forum.babylonjs.com/u/RaananW)\
**Post date:** [December 6, 2024, 11:14am UTC](https://forum.babylonjs.com/t/actionmanager-sequence-break-issue/55212/3 "2024-12-06T11:14:12Z")

</div>

The issue here is, as you also said, the array iteration. Removing an object from an array while iterating this array will result in exactly this behavior. One solution for that is to call remove in the next frame. For example:

> **[Babylon.js Playground](https://www.babylonjs-playground.com/#8LFTCH%232535)**
>
> Babylon.js playground is a live editor for Babylon.js WebGL and WebGPU 3D scenes

Due to back-compat (and the fact that I wouldn’t consider this a bug) we can’t change the behavior, but I will be happy to discuss it internally with the team and see if we want to change the unegister Action function to support this. CC @Deltakosh
