derkork / godot-statecharts

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

Tips on many Movement based states for a Player scene? #56

Closed Svaught598 closed 9 months ago

Svaught598 commented 9 months ago

Hi! First off, I just wanted to say that I love this plugin for Godot! It's been incredibly easy to work with so far, and everything feels very intuitive.

I've just started getting into Godot, and having come from a more conventional software developer background, am having trouble wrapping my head around the "right" way to accomplish composition using state charts for states that require a decent amount of logic to be ran on process/enter/exit/etc.

In some examples that I've seen, it seems that the main way to interact with StateChart states is to have other nodes listen to signals being emitted by each state and processed in a main script. For a player scene, with lots of complex movement states, this method means that I either have to have 1000+ LOC in a single file, or create bespoke nodes/scripts for handling each one (e.g. a Running state would need a Running.gd script that handles the enter/exit/processing of that state. Or more generally, a PlayerMovement.gd script that contains all movement stuff).

I'm leaning towards creating scripts that just broadly organize the logic in the player scene, but with lots of cross-cutting concerns like parallel states that also affect movement, I was curious if there was a better way to accomplish this with StateCharts?

Apologies if this is a silly question, just looking for any tips if you have any!

derkork commented 9 months ago

Well without knowing more about the details it's really hard to give a decent recommendation that doesn't amount to "it depends" or descends into hand-waving. The platformer demo has about < 100 LOC for the whole player movement which has some extras like double-jump and coyote jump. This is an order of magnitude away from the 1000 you suggest. Could you give me some more insights about where all this complexity comes from? I feel I can give a much better recommendation when knowing the problem at hand rather than speaking in general terms.

Svaught598 commented 9 months ago

Thanks for the super quick response!

I ended up spending the last week or so really digging into the demos to better understand the usage and now I have a player character script with < 200 LOC compared to an initial ~1200. These were my takeaways:

So all in all, I think my problems were due to not modelling my state well enough in State Charts. But I'm interested to know if these solutions make sense or if there are alternatives that fit a little better?

Thanks again!

derkork commented 9 months ago

I'm happy that you were able to come to a nice solution. Your mentioned points seem sensible to me. As for the first point, you can actually have multiple transitions listen to the same event in different parallel sub-trees. This way you wouldn't need to send two events back-to-back, e.g.

If you now send the "switch" event both A and B will change their substate.

derkork commented 9 months ago

The last point could actually be addressed with events on transitions: https://github.com/derkork/godot-statecharts/issues/58 . If you get notified when a transition is taken you don't need to have this transient StateTimeout state because you get an information when the timeout transition is taken.

Svaught598 commented 9 months ago

That would be awesome! I'm finding that I have a lot of transient states used specifically for signaling a transition and this would help a lot!