How to Code a Simple State Machine (Unity Tutorial)

preview_player
Показать описание

In this video, I'm going to teach you how to code a simple State Machine in Unity.

#Unity3d #UnityTutorial, #GameDevelopment

Have you ever written a method with two paths of execution? It probably had an if statement based on a member variable, right? But then your requirements changed, and you added another condition to handle a new state. And I bet they change again, over and over, until your code became a mess of hard-to-read flow logic and conditional statements.

If that's you, then you should learn about State Machines. State Machines are an implementation of the State Pattern, a behavioral design pattern that enables an object to control its internal behavior at runtime. In this Unity tutorial, I'll walk you through the implementation of a simple State Machine that handles the logic of a turn-based battle system.

🎓 Related Courses & Videos

My Favorite Unity Assets 💯⤵️

* Disclosure: These are affiliate links, which means I'll receive a commission if you use them to make a purchase.
Рекомендации по теме
Комментарии
Автор

Wow, I've never seen such tutorial before. Explaining patterns in "question-answer" dialog is really easy to understand. Classes scheme is also really useful. That's what I've been looking for

MartyFoxy
Автор

That new style of pair programming / mentoring is really nice to understand what you're showing. Nice job, I hope you gain more viewers!

shaunyitisme
Автор

I started running into the problem of if-statement spaghetti as you mentioned and this tutorial was very helpful in transforming my existing code to the state machine pattern. Thanks a lot.

julianerickson
Автор

Wow never seen someone do the sketch-like tutorial without coming off as either cringey or condescending. This was really well done and actually proved that a tutorial like this can work to great effect. Great job!

ginocode
Автор

17:45 That sneaky 2f transformed to 0.2f

donixdev
Автор

It is amazing to me that the conversation at the beginning of the video is the exact same thought process I had before looking for a refresher video about State Machines. and for this exact situation and project type as well. I'm amazed the first video I opened fits so well with what I wanted.
Great video, I love how you add a bit of explanation about why and when to use this pattern. It is as important to know about the pattern as it is to know when to use it.

ZOMBIEBUSTR
Автор

I've seen them all, but you're one of my favorites to watch right now to learn Unity!

TackleProd
Автор

I've implemented this for my weapon system (still working on some issues).
It works great! It's actually been quite fun to work on the different states - it's so simple and keeps my code clean compared to the mess it was before! Thank you Charles :)

holmbergen
Автор

I spent 2 days writing then deleting state machine code for my game. Then I watched this video, and it resolved the issues I was having. Thank you.

SigmaChirality
Автор

I just used this tutorial to implement a FSM with the state pattern in a project I'm working, and I love it! I can now add more states as my battle system gets more complex (and I think it will). And it's so much easier to reason about the code that's already there. Thank you so much for this! Keep it up!

modiddymo
Автор

you have such a good relationship with yourself my dude, good tutorial

brianwilson
Автор

Thank you a lot!!! Your way of making it an inner dialogue rather than monologue is best way not to get frustrated when following the tutorial🤟

shkkate
Автор

this is so freakin cool, why did I not understand this sooner when I first watched the video

doge
Автор

Great video, just another comment to say that I enjoyed the question/answer format - it's very reassuring to have my noob questions answered like that!

thethirdkate
Автор

Wow, this pattern is amazing. It makes enemies yield when their currentHealth is less than twice their totalHealth :D
(silently fixed) typo aside, ... Amazing delivery on explaining the concept of a basic state-machine. I love your videos. Informative as well as entertaining :)
Keep it up!

nGAGEnline
Автор

If only learning from your peers was this smooth sailing

eter_inquirer
Автор

That was the best state machine explanation and very creative alternating state presentation. I get it. This sir, is worthy of the Infallible Code channel name. We're this reddit, I'd give you gold.

Kandyman
Автор

Thanks for this vid. I appreciate the human touch of the "skit" parts.

dweebosupremo
Автор

This was one of the best tutorials I have ever had the pleasure of watching in this website! thank you so much!!

ZoepX
Автор

One small improvement that could be made so that you did not have to implement an Attack and Heal method in the base State class would be to have the battle system generate AttackButtonPressed and HealButtonPressed events. When a BattleState is created it could choose to subscribe to the events that it cared about such as Attack and Heal and could make decisions based on that. This would scale better, decrease coupling, and improve readability / code intent.

JackKell