derkork / godot-statecharts

A state charts extension for Godot 4
MIT License
734 stars 35 forks source link

Problem with automatic transitions #71

Closed eldyer closed 6 months ago

eldyer commented 7 months ago

So I'm trying to use automatic transitions in combination with guards, but something isn't working as expected.

Here's a demo project with a few changes to the ninja_frog project: test.zip

If you look at the state chart debugger, the frog seems to always stay in the Move state.

derkork commented 7 months ago

It would seem I misinterpreted the spec :

Automatic transitions are transitions that are triggered as soon as a state is entered, sometimes causing the state that was just entered to exit.

and later

Such a guarded automatic transition is checked immediately after the state is entered.

So in the plugin these transitions will only ever be evaluated when a state is entered. However further down the text we have

... with this automatic transition in play for as long as the state is active. Every time the statechart handles an event, the guard condition for these automatic transitions are checked. If the guard condition ever succeeds, then the transition happens.

And this part is currently not implemented which explains why our example isn't working as expected. So there would need to be an implementation that checks all transitions in play:

If a machine is in a state with a guarded automatic transition, then that guard is checked as often as possible. Being event driven, the guards are effectively only checked whenever an event has been processed.

In our case it would probably make sense to check for these transitions:

This could incur a slight overhead and I would probably need to build up some internal caching to make such calls efficient (e.g. not having to traverse the whole state chart for transitions in play on every event/expression property update), but I think it would be a very useful thing to have. Sorry, that you had to write two tickets to get it through and thanks for taking the time! I'll cook something up.

eldyer commented 7 months ago

Excellent! :+1: And yes, checking only for expression property updates totally makes sense.

derkork commented 6 months ago

This was a lot harder to get right than I thought. There are a million edge cases. I'm cautiously optimistic that I got all of them handled properly while not killing existing projects and not tanking the performance, but we'll see.

eldyer commented 6 months ago

Great work! :+1: