derkork / godot-statecharts

A state charts extension for Godot 4
MIT License
761 stars 39 forks source link

Event sent on state enter is ignored on parallel state #143

Open derkork opened 3 weeks ago

derkork commented 3 weeks ago

Assume you have this setup:

ParallelState
  A
    on enter:  send_event("some_event")

  B ( compound )
    B1
       on "some_event" -> B2
    B2

If we start this, A is entered, which triggers the sending of some_event. But because B is not yet entered (in parallel states states are entered one by one), B is technically not yet active which is why B1 never gets the some_event event delivered so while we should end up with state B2 active, we end up with B1 active. We will need some two-phase state entering mechanism, where we first finalize all state entering and only fire the event handlers once the new state is propagated through the state chart.

derkork commented 3 weeks ago

As a workaround one can delay the sending of the event by one frame, e.g.

func _state_a_state_entered():
    $StateChart.send_event.call_deferred("some_event")
derkork commented 3 weeks ago

We will also need this same process for state exiting.