Is there a way to create a player finite-state-machine with only one script?

Godot Version

Windows Godot 4.2

Question itself:

For a little while now, I’ve been trying to redo my character code for my game into a finite state machine using a method of various nodes and scripts (image for reference) but I’ve been getting stuck because it’s a bit tedious to do so, but I’m now wondering if there’s a way I can possibly dump the nodes and multiple scripts part and just do it all inside my singular player script?

I know a state machine is used to separate each movement from each other, so the option to only call one movement at a time is opened up, and that its useful for more organized coding, but couldn’t that all be done on one script? if it is, how would that be done?

Currently, my non-FSM player code is a script where each movement is in its own function (i.e., “defaultJump()” and “defaultMove()”) and all of them are called in _physics_process(delta). (Here’s an image of it for better reference)

Could I possibly keep my current code style perhaps, but implement a form of state machine inside that same code where only 1 of these functions is called at once?

Thanks in advance!

1 Like

You can do a state machine with no code at all!

Look at animation tree and it’s state machine node!

3 Likes

Interesting idea! I might try this another time, though I got my answer solved through a crosspost onto the godot subreddit, and I’m getting a functional one using enums, but this might be useful for a feature project, I might toy around with this during game jam projects, thanks!

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.