derkork / godot-statecharts

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

Feature: Monitored transitions #55

Closed Deslucir closed 9 months ago

Deslucir commented 9 months ago

Hey everybody!

I tried to implement something with godot-statecharts and it did not work how I imagined it. So I debugged everything and read through the documentation a little bit.

In the code I found that transitions are only evaluated on entering the state or a relevant event was send. I also found in the documentation statecharts.dev/guard the following paragraph:

Guard functions are often called while processing an external event so this must obviously be possible. However, the statechart should be allowed to check any guard at any time, i.e. also while not processing any event. This is to allow the author of the statechart to be free to use things like delayed events or delayed transitions.

I interpret it that a guard should be able to be checked an any time. So I implemented it as an optional feature that the user can choose if the transition will be checked every frame. I found a small problem with my implementation and fixed it here https://github.com/derkork/godot-statecharts/commit/a603e7d4e1b2cdc7b74ae5d7ade6bc56b1ba0ffe but that does not feel good in the current state how I implemented it. The result worked how I imagined it, so I created a small demo to show the new functionality in action.

I hope this fits into the vision of this project! Feel free to edit it as you please or suggest stuff that I should change, rename or delete!

derkork commented 9 months ago

@Deslucir, thanks a lot for taking the time to send a PR! I am not sure if I understand the feature correctly though. If the goal is to have transitions that are checked per frame, you can already have this. Let your transition listen to a "tick" event and then just send a "tick" event every frame to your state chart, e.g.

func _process(delta):
   state_chart.send_event("tick")

Could you also give me some more information about the use case behind this feature? In general I feel polling for a condition is something one wants to avoid if possible, so I wonder if this could actually be better done with some signal rather than checking for a condition every frame.

derkork commented 9 months ago

I'm closing this as it has been dormant for a while.