The State Pattern (C# and Unity) - Finite State Machine

preview_player
Показать описание
The state pattern is a programming pattern that is also known as a Finite State Machine or FSM is a pattern that can be very useful for systems with a small to a medium number of distinct states or behaviors. The pattern can help to clean up and make it very clear exactly WHAT code is running.

Timecodes
Intro 0:00
Project Description 0:50
Programming with IFs 1:10
State Pattern Simple 2:04
State Pattern Class-Based 5:03
Final Thoughts 8:52
Outtakes 9:52

#UnityTutorial #GameDevelopment #programmingpatterns #csharp #statepattern #finitiestatemachine #fsm
Рекомендации по теме
Комментарии
Автор

Is there a programming pattern you’d like to see me cover? Which one?

OneWheelStudio
Автор

Great video. I use the state pattern quite regularly. I generally add a constructor to each State and pass in the appropriate data that each state needs to work with. Also in my interface I add void OnEnter() and void OnExit() method(S), and in update simply test if the returned value from the current state is different. If so, run OnExit() on it, then switch the current state to the returned state then call OnEnter() on it. This allows a nice way to reset counters, trigger animations, play sound effects, etc when the state is entered and do any cleanup when it is exited.

MQNGameDev
Автор

very professional way to make a tutorial, hope every tutorial where like this

DonDisainer
Автор

What a great tutorial, you really set my up to succeed with the next 2 phases of programming my VR game.

betterlifeexe
Автор

Simplest and cleanest state pattern I've seen.

iDerp
Автор

perhaps the best introduction into the state-based AI. Thank you.

ZZaGGrrUzz
Автор

Very nice. I really hope this series continues. Thank you.

jameslavery
Автор

This perfectly illustrates the use of state pattern. However there is a catch. It does not completely remove the code smell. You are essentially delegating the if statements in your main class to the child classes to act upon the state change. However this violates the open-close principle. Incase a new state is added in the future all the child classes you created will get affected. Just wanted to highlight this so it would be useful for others who are starting with design patterns. Overall a good job!

bhanumallepeddi
Автор

I really enjoyed your video! Straightforward, to the point, with clear examples. Awesome!!

IreneTorrubia
Автор

I really appreciated this video! I could easily pickup and implement my own version! Thank you a lot! I will pickup that book as well and use these patterns in my games. Cheers!

madewithreallemons
Автор

If I'd known about the state and the observer patterns a couple of years earlier my life would've been so much easier =D
The great thing about a FSM is that even if your number of states gets a bit big, it's still perfectly readable/debug-able because each state is only doing one thing. I have about 50 states in the fighting game I'm working on. While that seems like a lot I've made them generic enough that I'm not going to need any more but if I do I can add them easily and the flow of the system is easy to understand =)

JohnnyThousand
Автор

Dont need check if the navAgent is null everyframe, you can to GetComponent in the Start or Awake or the NPCSearch, for thease reason I think the states must have OnEnter, OnStay and OnExit methods

gamedoelcaminodelvideojuego
Автор

I'm confused how State Pattern is any good. Every time you'll add/remove states, you'll have to edit each state that may need to transition to that state making the whole thing tightly coupled between them and that pretty much goes against OOP modularity expectations.

I think you should keep all transition business in a controller (FSM) who has access to many information that may or may not concern every states and let it decides which states to move next based on the information it gets, like the simple example, but make each states a ScriptableObject with an interface "Execute(), Validate(NPC_FSM _fsm)". Validation tells FSM if it is a valid state to move to based on gathered information.

This way you can easily parametrize each states and even have multiple "Attack" states, but with different weapons assigned (Sword, Axe, Bow). Making it central would let FSM switch from any to a specific state, "if attacked, state = attack", but also "if low health, state = run". Just looking at the FSM script you should be able to paint the graph without knowing what states do.

Best example of this is Animator Controller. Your animations does not know what comes next, controller will make that decision.

Another way would be to synapse (a transition object) between states (S1 -> T1 -> S2, S1 -> T2 -> S1). The base class would have static information every synapses can access to validate when state question it, a state would loop between all synapses assigned, each synapses would act like transistor gates, and quit on first true. But without a graph inspector like ShaderGraph or even AnimatorController this can get hard to visualize the network from code.

Kalahee
Автор

So I logged into another account just to like this video one more time. Thank you very much, this is awesome!

johnpepperwood
Автор

this video helped more than I can explain. Thankyou.

misfire
Автор

great video, the state pattern is very similar to the strategy one, seems like they are almost the same code structures but with different aplications

sonsai
Автор

Thank you so much for the book recommendation, it's definitely making the difference in my learning process. Also, it's kinda rare to see tutorials which focuses on good practices, and you do them good. You got a new sub!

chuckdude
Автор

why i dont find this channel before! great turorial

datcong
Автор

Great video, really clear explanation of this fundamental concept. One question however: do you suggest to code each fsm from scratch or to use existing ones? For example unity animator is itself an fsm, even if thought for animation. A lot of people use it as logic fsm, even if I think it is somehow limited because likely hard to debug and because it doesn't allow to distinguish between logic and animation states management.

However, great job. One sub more!

PunishCrow
Автор

Had to throw a like on this video. You are a very good teacher!

cb