[C#] How do you handle State Machine (in code).

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Cristian

Watching some YT videos, watched to avoid the “Animator Hell”: https://www.youtube.com/watch?v=nBkiSJ5z-hE

Then the Finite State Machine videos start to appear. So I tried to copy this: https://www.youtube.com/watch?v=Ty4wZL7pDME

but I do not understand quite well.

This is my project: GitHub - chriztheanvill/Godot_CS_State_Machine: Testing_CS_State_Machine

Its on Godot, with C#

Does not have the Assets folder:

Assets/Images/Character/Individual Sprite/

The thing is:

In the Implementation I repeat the code many times, like:

First get the parent object (Player) in to “pl” where there are the Methods:

Gravity, Movement, Jump, Attack, etc…

Walk, has the “pl.Gravity, pl.Movement, pl.Jump, pl.Attack” Methods.

Jump has the “pl.Gravity, pl.Movement, pl.Jump” Methods.

It is normal to repeat the methods in each state?

Can you show us the part in your code? Maybe I can tell you how to improve things.

juppi | 2021-12-28 14:17

:bust_in_silhouette: Reply From: spaceyjase

Yes, that’s inherent to the pattern; although here, even more likely as there are many common things that overlap where as some complex state machine will have very different code. And that’s fine, the pattern isolates the code making it easy to maintain and develop but ultimately the choice is yours.

There’s some C# specific things that make the pattern simpler, such as first-class functions (also coming in godot). You could compare your state machine to other code, like this one I used in a game: GitHub - spaceyjase/sr-6: A short MVP platformer for Go Godot Jam 2.