Godot State Machines vs. State Charts
Asked Answered
O

5

2

Not exactly a tutorial but I recently wanted to check out the Godot State Charts extension and compare it to a simple state machine. So I made a simple comparison project. Maybe it's of use to anyone.

Source code: https://github.com/Toxe/godot-state-machines
Playable web version: https://toxe.itch.io/godot-state-machines-vs-state-charts

The blue guard and player use my own simple state machines and the red guard uses Godot State Charts. Move around with WASD or cursor keys.

(Please note that this is not intended to be a fancy stealth game with some sophisticated AI or any kind of polish. I just wanted to play around with a couple of things and the guards might be dumb but that's fine. If this were a real project I'd put code like the player detection or movement into their own components. But this test project is already way too big for its own good.)

Has anyone used Godot State Charts before? Seems pretty okay from what I have seen. Although unless I have multiple parallel states I'll stick with my own state machines because they are more readable to me, faster and I can configure and customize more in code.

My own state machine base classes, a helper GuardState to make things simpler for the BlueGuard (provide guard and player references and make working with animations simpler) and an example "follow_player" state.

I use simple nodes for each state with an attached individual state script.

(Godot State Charts brings its own class called State therefore I needed to rename mine to MyState and MyStateMachine but I think I'll live. 😆 )

The RedGuard uses Godot State Charts and has all of its code (guard and states) in one single script. Which I really don't like. I could of course come up with ways to split up that state/behavior code but to be honest I noticed that I prefer my own solution and wanted to finish this test instead of expanding on it.

I like having one Node and script for each state like I do with my own state machine for the BlueGuard but doing this for the RedGuard would only add more nodes and the need to click even more in the inspector. The best middle ground would probably be to add a "behavior" Node that has one script containing all the code related to the states.

A big plus though is that Godot State Charts has a really nice debugger that you can just drop into your project.

In conclusion

Godot State Charts seems neat but I'd stick with my own solution. Although to be fair I did not really play to its strength because I don't have parallel states and also I did not put much effort into refactoring the code needed for Godot State Charts. Still, I'd give it a thumbs up. 👍️

Owensby answered 23/10, 2023 at 12:41 Comment(0)
L
0

Excellent work! It would probably be interesting to compare it with other plugins:

Limner answered 23/10, 2023 at 18:13 Comment(0)
N
0

Limner

Orchestrator doesn't seem to provide state machines. Or does it?

Nahum answered 23/10, 2023 at 20:34 Comment(0)
L
0

Nahum Or does it?

Well, that's game logic. How do you take it? But maybe it's really unnecessary…

Limner answered 23/10, 2023 at 20:52 Comment(0)
Y
0

Although this looks useful for certain types of state management, I personally would never use anything like that. Aside my dislike for discrete state machines and formulaic design patterns in general, I find it's always easier to implement custom tailored state management than to hurdle over someone else's abstractions.

Yaron answered 23/10, 2023 at 21:14 Comment(0)
O
0

Limner Excellent work! It would probably be interesting to compare it with other plugins:

Thanks! I'll make a note and I might look into those.

Owensby answered 24/10, 2023 at 7:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.