Forums › 💬 NodeCanvas › ⚙️ Support › Events sent in OnCollision slip through between states
I’m sending some events during onCollision2D, “stagger” in this case whenever a hitbox hits a hurtbox. It seems like the event sometimes gets caught in limbo, between a states OnDisable and a new state’s OnEnable, when the listener is not active.This causes the FSM not to transition where it should.
<div>
<div> protected override void OnEnable()</div>
<div> {</div>
<div> router.onCustomEvent += OnCustomEvent;</div>
<div> //UnityEngine.Debug.Log($”adding listener for event {eventName.ToString()}”);</div>
<div> }</div>
<div> protected override void OnDisable()</div>
<div> {</div>
<div> router.onCustomEvent -= OnCustomEvent;</div>
<div> //UnityEngine.Debug.Log($”remove listener for event {eventName.ToString()}”);</div>
</div>
<div>
<div> }</div>
</div>
So the log looks like this;
[20:21:09] adding listener for event “stagger”
[20:21:10] remove listener for event “stagger”
[20:21:11] stagger
[20:21:11] adding listener for event “stagger”</div>
I could create a new type of event that caches events or something, but I think it should probably be fixed that events can slip through haha.
I tried all 3 update modes for the FSM; Update, FixedUpdate and LateUpdate, and the problem occurs in every setting.
I’m guessing the FSM doesn’t activate new states until the next frame, so if OnCollision occurs in the update loop after OnDisable is already called it will simply miss the event. Either the order should be fixed somewhere internally, or the CheckEvent might have to listen to events continously.
Edit: Sorry no clue how to format code here lol.
Hey,
No worries about the code formatting 🙂 Can you please share a screenshots of FSM (or even a reproduction FSM) just so that I can replicate it exactly the same and fix accordingly? Feel free to send files to support_AT_paradoxnotion.com if you don’t want to use an external image sharing service since attachements are disabled on the forum right now.
Thanks!
Thanks for the quick reply! The setup is quite simple;
It happens in about 1/10 of the times of the collision.
Do you have any suggestion on how to fix this? I thought I could just listen to events continously, but I realised it’s not so simple since the conditions really have no idea of the state that they are attached too, so it would be hard to verify if the event is legitmate when it’s received. And also, you can’t really start listening until the condition has been enabled at least once.
It feels a bit strange that a state’s condition are disabled on the one frame, but that the next state’s conditions are not enabled until a next frame, so the FSM is kind of in limbo in between, which is exactly where the event is triggered in this case.
Hello and sorry for the late reply. Happy new year!
This is a bit hard to debug exactly what the flow of the FSM is in this case. So, indeed the the transition conditions of a State are enabled only once the State is enabled (enter) and are disabled when the State is disabled (exit), and remain active for as long as the State is the active one only (running or finished regardless).
What confuses me in your example case, is: why is the OnDisable called (and therefore the event removed from listening)? Does another transition of the State (not the “stagger” event) take place first? If so, is the “stagger” event transition the first in the the list of transitions (transitions can be re-rodered in the State inspector panel and take place from top to bottom)?
Is it possible to send me a small reproduction project/scene to ‘support_AT_paradoxnotion.com’ so that I can see better what the flow of the FSM is please?
Thanks 🙂
did you try one of the special update nodes. Maybe fixed update and send or capture the event if collision happens during update.
