Get your code looking nice, clean and maintainable, by applying this simple pattern for your state management in Unity. Inspired by: gameprogrammingpatterns.com/s... www.firemind-academy.com/
КОМЕНТАРІ: 488
@Michael_H_Nielsen3 роки тому
I think the information is good but please speak faster. I sat the speed to 1.5 :-)
@Zicore472 роки тому
State machines are great, but they don't reduce spaghetti code. In all your examples you could invert the ifs and return early to reduce nesting. Also keep in mind that virtual methods can add a performance overhead. Stick to KISS and composition over inheritance. Still a good tutorial for learning the concept.
@davedoublee-indiegamedev86332 роки тому
It:s not a bad approach but it can devolve into an inheritance hell with 30 classes for something.
@Gortart2 роки тому
You generally don't want a seperate class with just 1 method to override because you can just use delegate. And rather than returning a new instance of the state, the states should return a state flag that the caller should use to look up what to do next. Enum with a dictionary to look up would work well.
@aleksandarpantic87323 роки тому
Put it at x1.25 speed and still slow...
@ZeroSleap3 роки тому
I believe the CharacterState would be better as an Interface,as there's no real reason for a base class implementation of handleInput.
@polarisinglol2 роки тому
Awesome video! I recently got into Unity due to an assignment in my University which will be graded and am really loving it so far. Even though I already knew most of the stuff in the video I have to say it was a pleasure to listen and watch and I think you definitely helped a lot of people who are just getting started with programming languages and coding in general :)
@7oca7hos72 роки тому
Well, idk. I think using oop for this problem is a bit of an overkill. You just could have handled all the conditions inside the attack() function, using boolean operators and the return keyword to avoid nesting. The user presses the attack button, then the function is triggered and it checks the conditions for doing the attack. In this way you have all the conditions in one place for that specific context, making the update function much cleaner. I think also that the code will be easily understandable for other people, and for you too when you'll read it after a while
@seb68613 роки тому
I still don't consider my self a game dev until i could make at least one game and not just little project that i do to learn different stuff.
@mintesnotmelese65523 роки тому
Men Please I want a bunch of Tutorials on State machine pattern programming, You're now officially my fav UKpostsr. keep doing this stuff a lot I love it! :)
@spectrecular97212 роки тому
IDK, I think this boils down to whether you want your code to "look pretty", or you want a more performant/optimized game.
@TGameDev2 роки тому
Imo you could also save the headache by implementing functions and guard clauses. So for instance that nested nightmare would look more like this:
@kreed14153 роки тому
State machines rule, however I would suggest you look into a more efficient solution that has less boilerplate code. If you have to create a brand new class for every state it gets incredibly tedious as your game grows. Consider making a StateMachine class that inherits from monobehavior that has a dictionary of <string, Action> which represents state then simply run the method for each state. it also makes declaring each state very simple and concise
@LordBordNoob2 роки тому
Much like others have said, using enums and a dictionary with delegate functions is a much better solution to this. Or simply having functions that take care of each behaviour.
@andythedishwasher11172 роки тому
This was extremely understandable and helpful. State management seems to be one of the trickier initial hurdles in Unity, so I think your subject matter was on target as well.
@2simmy22 роки тому
I would recommend keeping your state as an enum instead of different classes then have a switch that shoots off to their own methods. Doing so will leave access to common data accessed by multiple states. This will also allow access for multiple states at once by using a flag enum if it's necessary (but i would recommend avoiding it)
@thatoneuser86002 роки тому
12:28
@magnusm43 роки тому
I've seen Table Flip Games's tutorial and the great Unity finite state machine tutorial.
@garebeargaming92813 роки тому
Spent the past week having a headache and a half trying to fully implement a state machine. And damn you just alleviated my headache.
@NikolaZagoracРік тому
I'm about 20% done with my game mechanics, and have around 20 classes already... It seems like the more I unwind my code, the more complex it gets, but the more complex it is, the easier it is to work with.