r/unity_tutorials • • Aug 30 '21

Learn the fundamentals of the State Pattern and Finite State Machines to keep our code effecient, organized and expandable as our games increase in complexity! With a guided project walkthrough and accessible examples, let's take our programming skills to the next level! (Full tutorial in comments)

77 Upvotes

13 comments sorted by

4

u/GoldHeartNicky Aug 30 '21

Learning to utilize the State Pattern with Finite State Machines can to make our lives so much easier and keep our game's code looking clean and running well!

Happy and excited to share a tutorial that may help you understand them!I hope these video essays/tutorials help you on your gamedev journeys!

LINK: youtu.be/Vt8aZDPzRjI

Cheers!🍻-Nicky

2

u/-eXnihilo Aug 30 '21

Thanks, Nicky. Have just been banging my head on state machines lately. Going to use your approach.

1

u/GoldHeartNicky Aug 30 '21

Awesome! Yeah, it took me a while to understand them too. A lot of research and experimentation was compiled into this video! I hope it helps!

2

u/WhyYaGottaBeADick Aug 30 '21

Really nice video! I work with Unity professionally and I can confirm we use this pattern quite a lot. In fact, your implementation is extremely similar to how we use state machines.

2

u/GoldHeartNicky Aug 30 '21

Oh rad! Thank you for watching! It makes me feel a lot more confident about the content hearing that you all use a similar implementation. Was a bit nervous because this is my first “programming-centric” tutorial. The rest have been about animation and movement in Unity.

2

u/[deleted] Aug 30 '21

I just watched this a few hours ago, and I immediately sat down and rewrote a bunch of my existing scripts to use this instead. Significantly better organized than the mess I had going on. Really appreciate you making it. As always, really impressive production quality, especially for a "smaller" channel. Keep at it, you're doing awesome!

1

u/GoldHeartNicky Aug 30 '21

That's awesome! Love to hear that the video was able to help! And thank you for the kind words.

2

u/baz4tw Aug 30 '21

Seriously this was great, the other FSM examples lurking around the unity community don’t make it easy to provide the context. This solves that, thanks!

1

u/GoldHeartNicky Aug 30 '21

Right on! Happy to help make things clearer!

1

u/Bonejob Aug 30 '21 edited Aug 30 '21

This is probably the best explanation I have seen of the State Pattern. It is easy to follow and well explained. Great Job.

edit: The only thing I would mention is that using all the different files for the scripts is redundant. It is easier to include the code all in one script with the multiple classes included in the single script file.

2

u/Future_Viking Sep 02 '21

Personally i think it is generally best practice to have one class per file:

  1. Better readability (potentially less on screen), think chapters.

  2. Easier to find and solve errors (indicates the file namn directly)

  3. if you are part of a team of developers and use a source control tool that uses pessimistic approach (exclusive locks) - your developers will have a hard time while working on the same file.

But yes it does come down to personal preference... there is no right or wrong.

Anyways here is an old quote from Scott McConnel

"Put one class in one file. A file isn't just a bucket that holds some code. If your language allows it, a file should hold a collection of routines that supports one and only one purpose. A file reinforces the idea that a collection of routines are in the same class."

1

u/Bonejob Sep 02 '21

I agree it is a personal preference. I have found that portability can be a problem when using multiple interconnected class files. This is fine if your class set is going to be compiled into a DLL for use, but in Unity where things tend to be left as loose scripts, it can lead to issues.

For example, I use a finite state machine for our movement and control scheme code. We currently have 31 specific states in our movement. If I had that spread across 32 files and wanted to move it to a different game it could cause issues. Although it could be mitigated by good folder management. The problem being is developers are lazy by nature...

I also will never use pessimistic code control ever again. My first taste of that was Microsoft SourceSafe, then CVS, and finally Subversion. I had the team switch to Git (We currently use Gitlab with LFS for Unity) and will never go back. Some people swear that exclusive locks are the best but in decent-sized teams I found it to be a nightmare. We have two developers working on environment coding on the same sets of classes with locking they would be forever having to bounce the classes back and forth as changes are made.

1

u/E-2-butene Jan 09 '22

Thanks so much for this fantastic tutorial. I’ve been looking at how to build in a state machine to my own character controller, and this was an excellent breakdown on how to do that!