Code Class - Build your own State Machines!

Ғылым және технология

Heya Pals!
This video we discuss building State Machines for your games in Unity. This video follows on from the previous player movement video in the series! Subscribe and check back soon for Part 3.
Enjoy!
Tutorial Assets Download: uppon-hill.itch.io/indietales...
Previous Video - "2D Player Movement" : • Code Class - 2D Player...
Next Video - "Hierarchical State Machines": • Code Class - Hierarchi...
Chapters:
0:00 - Introduction
1:20 - Complex behaviours in my games
4:28 - The problem to solve
6:16 - What is a state machine?
7:41 - Importing Aseprite files for player animations
10:56 - Beginner - Enum State Machine
17:28 - Intermediate - Component State Machine
35:00 - Next time
36-14 - Outro
Music: Rifti Beats - Chocobo & Chill [Gamechops.com]
----
Twitch: / adamcyounis
Twitter: / adamcyounis
Tiktok: / adamcyounis
Discord: / discord
Become a Patron at / adamcyounis
Download assets and games from the stream at uppon-hill.itch.io/
Later, pals!

Пікірлер: 89

  • @iHeartGameDev
    @iHeartGameDev2 ай бұрын

    It's simple: I see a state machine video... I like and subscribe.

  • @batattitude
    @batattitude2 ай бұрын

    The best part about this video for me was the refactoring, too often you see state machine lessons jump into the component-based implementation without an explanation as to why it's being made that way. Going from a messy script, to a longer but cleaner enum-based system, to the compact and extensible component-based system is an ESSENTIAL part of the learning process because if you don't know why you're doing what you're doing, you haven't really learned. You've made my past mistake of copying code you can't read.

  • @cbuckington2933
    @cbuckington29332 ай бұрын

    One little addition I might throw in here is the concept of finite state transitions. Something along the lines of "You can do an attack while Idle but not while walking". As the developer it is crucial to really think about the behaviour of the thing you are trying to create and use a logic that represents this as closely as possible. Overriding states through user input is only one of the many ways one can achieve that. A similar logic would be something like keeping a list of states your state can transition to or even implementing this in the states themselves if they are very closely tied together, like an aerial attack that can always only happen out of an aerial state

  • @AdamCYounis

    @AdamCYounis

    2 ай бұрын

    This is definitely a topic we'll cover in the follow-up video. Transition pairs were actually one of the first things I added to insignia's state system and then never used because of the tree structure I'll be showing next.

  • @pornopirate
    @pornopirate2 ай бұрын

    I've been very interested in how you handle your state machine while watching your stream, so thank you for taking the time to explain it so in depth.

  • @thehomiesbrand
    @thehomiesbrand2 ай бұрын

    You're awesome, I love how thorough you are! You always cover all the bases and I know you take the time to organize your thoughts before presenting them to us. Thank you, for being an excellent teacher.

  • @DireSwift
    @DireSwift2 ай бұрын

    Excellent timing. I have just been researching different types and approaches for state machines and look forward to the next video!

  • @eason9
    @eason92 ай бұрын

    Thanks for these tutorials! I start making pixel art last year and I learned a lot from your channel!

  • @funteav3437
    @funteav34372 ай бұрын

    This state machine tutorial is fantastic! Your detailed explanation of state transitions and the logic behind it really clicked for me. The way you handle state machines is intriguing and makes coding feel less daunting. Looking forward to more in-depth tutorials. Thanks for shedding light on this topic! I have always wondered how to go about implementing this. Thank you Adam

  • @xairo8134
    @xairo81342 ай бұрын

    This is by far THE best state machine tutorial... I've already started applying some of this stuff and it's super useful

  • @hrkdreams6120
    @hrkdreams61202 ай бұрын

    Thank you so much for all game tutorials!

  • @Rhino182
    @Rhino1822 ай бұрын

    I love this series. I’m just starting making my own game and this has been super helpful

  • @Joytokey1231
    @Joytokey12312 ай бұрын

    Great video! Really happy to see more coding content from you, especially these more intermediate ones.

  • @hdscheepers
    @hdscheepers2 ай бұрын

    Excellent video, thank you! I’ve been doing my state-machines a bit differently with the base state not inheriting from MonoBehaviour, but I really like your approach. It addresses a lot of things that felt were a bit awkward with my own approach. I’m very much looking forward to the future videos, especially the hierarchical state-machines, something that would have been so useful for my current project.

  • @Parecletus
    @Parecletus2 ай бұрын

    I think you explained State Machines so well .This video made me think how different aprouches can be when coding . I'd love to watch a video from you about creating a new editor window or an animator. I love any content you share. Thanks a lot

  • @MOON9FLY
    @MOON9FLY2 ай бұрын

    About to start my C# game Dev Journey, Love your video , I needed that , Explained in clear way , thank you very much

  • @alexanderdenisov226
    @alexanderdenisov22629 күн бұрын

    This is the tutorial on controlling the movement, animations, and a State Machine pattern of a 2D character, that I have been looking for for so long. Thank you, Adam!

  • @ArcticHaven
    @ArcticHavenАй бұрын

    Thank you for these awesome tutorials, they have really helped me improve my knowledge and thought processes while developing my game.

  • @travisalstrand4237
    @travisalstrand42372 ай бұрын

    I was experiencing the Enum downfall and started watching videos on how to implement something like this. Pretty overwhelming for me initially, then I stumbled upon your video. I've been a long time subscriber but only knew about your pixel art related content. I'm super grateful for this and your explanations, thank you. Looking forward to the follow up!!

  • @franskeeh
    @franskeeh2 ай бұрын

    I was literally just looking on your page to see if there were any new videos! crazy coincidence

  • @VuTran-gw3ej
    @VuTran-gw3ej2 ай бұрын

    Amazing tutorials!

  • @user-fx4hn9iu6c
    @user-fx4hn9iu6c2 ай бұрын

    More State Machine tutorials :) especially on a boss will be appreciated Adam

  • @samuelcharlesbird
    @samuelcharlesbird2 ай бұрын

    Hey Adam, just wanted to say thank you so much for this video. I’d got to the point where I’d implemented running, jumping, sliding, crouching, attacking; and i was becoming more and more uncomfortable with the code. I started researching state machines and watched a bunch of videos, but nothing resonated with me. I was envisioning something concise, readable, re-usable and scaleable but I was struggling to implement it. This video perfectly encapsulates it. I wish i was able to define it so clearly like you. But thank you so much, once I’ve watched this a few more times I think I will be able to implement what i’ve dreamed of beautifully. I’m especially excited about the follow up hierarchical state machine. I’ve been building in Godot and for the most part prefer it to Unity, yet I do prefer the type system with C#. I hope I can type this sufficiently in gd script. Thanks also for the little nuggets, that mapper that makes sure the jump animation is timed perfectly: genius. I would never have thought of that. Amazing stuff. I’m so proud to be a patron: your content is sublime. Good day!

  • @deveshsingh0904
    @deveshsingh09042 ай бұрын

    Ohhh Man I am getting mad right now !!! Brought new Top down engine asset && wanted to have some sort of tweaking to add it to my new game but I can't able to get the way they had written the code .... it's some what readable but tweaking is a lot difficult spend over 5 full days to find such a great tut !!! Thanks Dude !!😇

  • @kris7695
    @kris7695Ай бұрын

    It's videos like this that make me feel like I can create my own AAA game. Then I load up the IDE and have no clue what I'm doing. XD Your videos are the things that give me hope

  • @iRubyJ
    @iRubyJ2 ай бұрын

    Saw your intro animation and subbed. Hope your content is good 😁

  • @chestythepuller
    @chestythepuller2 ай бұрын

    Already subscribed to you on Twitch... but I can't thank you enough for being part of my journey into tech/programming/and game development.

  • @AdamCYounis

    @AdamCYounis

    2 ай бұрын

    Thank you!

  • @CoderFromTurkey
    @CoderFromTurkey2 ай бұрын

    Thanks for the video!

  • @sitrusjo
    @sitrusjo2 ай бұрын

    Love this it's so helpful

  • @allenytw
    @allenytw2 ай бұрын

    tutorial is great, the video is actually not long enough!😄

  • @PeterMilko
    @PeterMilko2 ай бұрын

    I'm making a platformer so it was cool to see how you set up stuff. I'm not using state machines, but may need to in the future.

  • @Hasdf32gbve
    @Hasdf32gbve2 ай бұрын

    What an amazing video

  • @NoahNCopeland
    @NoahNCopeland2 ай бұрын

    hyped for the next state machine video! will you explore the enemy's AI machine in the future video?

  • @andylinkOFFICIAL
    @andylinkOFFICIAL2 ай бұрын

    wonderful!

  • @samuelalvarez7631
    @samuelalvarez76312 ай бұрын

    this video is goated

  • @grzesfu
    @grzesfu2 ай бұрын

    I went with a component state machine too because I felt it allows me to leverage existing unity functionality, so I just used the monobehaviour's Update / OnEnable / OnDisable, instead of custom functions and I enable/disable my components. It's still reassuring to see someone more experienced use the component based approach. Very nice format, with introducing the enum based version and walking through the refactoring. One thing I missed is at least a mention of how your behaviour tree visualisation is made. Is it an asset? Is it a custom editor window you made? Looking forward to the next instalment!

  • @theashbot4097
    @theashbot409719 күн бұрын

    EDIT: You change the code base to not need it starting at 32:11 I do not know if you did this just so it is easier for people to understand, but instead of checking "if (state.isComplete)" on update you could instead have a event on the sate that the state Machine Handler subscribes to, which would make it run faster.

  • @nangld
    @nangld16 күн бұрын

    Good explanation. But gets more involved in isometric games with several directions, especially if your system has to deal with both 2d sprites and the 3d models. May also want to break some animation mid play, while transitioning smoothly to the new sequence. Some animations could be conditional (i.e. a monster with low health will be stumbling on walk). And then higher level scripting for cutscenes could be reusing the pathfinder and setting the ai states manually, while also interacting with a physics system. Would love to see a survey of different approaches and their trade offs.

  • @Vesrayech
    @Vesrayech2 ай бұрын

    I really enjoyed this and can't wait for the next one. Typically what I like to do is create a Character class that essentially operates as a store for everything a living creature will need, and then you override that with the Player class or BasicEnemyAI. It seems like there's a lot of overhead setup with making new game objects and setting state to them and configuring props in those components and dragging their references onto the actual character, and then passing in those references to the individual states every time. Wouldn't it be easier to do something like the Character class where you can pass that into the state so it can hold a single loose reference for things like animators, rigidbodies, health components, etc?

  • @AdamCYounis

    @AdamCYounis

    2 ай бұрын

    That's right! I'll be showing a more advanced example next video doing exactly that.

  • @CupKyle
    @CupKyle2 ай бұрын

    Hey Adam, I hope everything is good. I was fascinated with the helper function used in the video. It got me intrigued that you used math to solve the animation problem. So, it got me thinking, what do you recommend for learning math for game development. Are there any resources that you recommend, or I should go back and study math from the beginning?

  • @AdamCYounis

    @AdamCYounis

    2 ай бұрын

    I would encourage you to be project oriented in your learning. If the project you're making needs specific math, you'll encounter it while trying to solve the associated challenges. Otherwise you'll just be learning for learning's sake (nothing wrong with that, either!)

  • @Anerisian
    @Anerisian2 ай бұрын

    👍

  • @Hersatz
    @Hersatz2 ай бұрын

    Note that if you have knowledge of UI toolkit, making a simple node based UI for the selected state machine would be a big plus performance wise for more complex entities. Using monobehaviour over pure class does have a slight computational drawback, and it pollutes the entity's hierarchy with, not only the components themselves, but also with unused transforms. Imagine having 50 states for a complex boss, all packed up in the prefab of that boss 😱. Also, although I guess it's going to be talked about in the next video, using a state machine controller can help decouple a lot of the boiler plate code for handling the switching of states. Otherwise, the state machine usually ends up being an ugly spaghetti code blob, switching from one state to the next with ifs and elses. Last thing: note that using Unity's "animator" state machine is perfectly fine for any state machine if your game can handle the, again, computational drawback from the parts of the system you don't use (like blended, as specified in the video). With a bit of work, you can make a pretty generic and solid StateMachineBehaviour.cs based system.

  • @stalkerassassin4549
    @stalkerassassin45492 ай бұрын

    Hi Adam, I really like your videos, they teach me a lot, but I'm having a hard time understanding the running animation for a 64 pixel hero, I know you already have a running tutorial, but the hero there is 32 pixels, ideally I would like to see a tutorial on creating Armin's run from Insignia if possible, or a running tutorial for a 64 pixel hero, thanks for your attention.

  • @cloudirubez07
    @cloudirubez072 ай бұрын

    Hey Adam, I’ve been watching your videos pretty much since I began as a pixel artist, and they’ve helped me tremendously, especially over the last few weeks since a lot of the topics you cover are issues I have with my own art lol. I’m not sure if you’ll see this comment, but would you ever consider doing a video of a “pixel art review”? Not of a viewer’s, but rather character sprites from other games, and how you’d personally improve or change them? I’ve seen a lot of games be shown on this channel as examples, and rather than obvious ones like Metal Slug or other well-known games with good pixel art that would be hard to say much about than what’s already known, I’d actually think it’d be really cool if you did more obscure or lesser known pixel art styles/sprite work, like the Megaman Zero or ZX series’ sprites, since they both use the same character base. The pixel art in those games are probably the least covered online since everyone mainly focuses on the Classic Megaman games or the X series, so it’d be really cool to see from you. Again, love your content and have a great day if you’re reading this!

  • @rosebudsuccubus2296
    @rosebudsuccubus22962 ай бұрын

    This also works in Gamemaker!

  • @CyberAngel67
    @CyberAngel672 ай бұрын

    The correct term at the time stamp 20:19 is expression bodied members

  • @ReneSteenNielsen
    @ReneSteenNielsen2 ай бұрын

    Hi Adam, 15:40 you say; this would be good for a controller controlled game. What if you do both? Do you leave this in or do custom for each behavior, ending up with two vast different movement systems?

  • @cbuckington2933
    @cbuckington29332 ай бұрын

    Great Video :) Ive got one little question regarding the aseprite importer. I always avoided it since it seems to be messing up my animations when I add or remove frames from the file. When I for example add 2 Frames to an animation tagged "Walk", every other animation behind that will be shifted by two frames in the Unity animation clip. The workflow itself seems nice but issues like these really bother me, is there any way to savely export the animationclips in a way that the old ones just get replaced. I need to use the exported Animator Controller and exported animation clips since its important that the animations also scale the characters slightly, making it impossible to use the pre defined read only ones :/

  • @AdamCYounis

    @AdamCYounis

    2 ай бұрын

    When I tested it before filming, I was able to add frames to a tag in Asperite after the initial import and had no issues seeing the Unity animations update accordingly. This is the first time trying it in earnest, since I've got my own systems that I've been using for a long time. It surprised me with how simple it is for basic animations like this, so I might use it in future game jams. Of course, there are limitations that don't make it very extensible. Eventually you typically need to build your own thing, but that's what these tutorials are for :)

  • @cbuckington2933

    @cbuckington2933

    2 ай бұрын

    @@AdamCYounis Thanks for the answer

  • @jhopo737
    @jhopo737Ай бұрын

    i want to know your opinion on the blendtree,isn´t more code clean just put one blend tree called "xmove" with idle and run in it and set a code like this: animation.SetFloat("xmove", Mathf.Abs(xInput)); with coding is more easy to control if something is bad but is a lot more complicated i supose,i am just in the start of coding so i dont know which one is better

  • @unitywithzaher1374
    @unitywithzaher1374Ай бұрын

    Interesting, i have made my own state machine system from scratch, i have made videos about how i did it, it's generic so you can use it for multi different NPCs and not designed for only one object

  • @UZPvNUCaaQdF
    @UZPvNUCaaQdF2 ай бұрын

    LOOK OUT! THAT FERRET'S GOT A KNIFE!

  • @Pxmsurvivor
    @Pxmsurvivor2 ай бұрын

    Ola adam, você pode criar um tutorial de animação do cycle correndo em topdown?

  • @zentheplayerrapper8504
    @zentheplayerrapper8504Ай бұрын

    😎👌🤔❤

  • @waterfish3886
    @waterfish38862 ай бұрын

    how do you import aesprites such that the gizmo is completely centered and not just attached to the bottom boundary of the sprite?

  • @lloyd011721
    @lloyd0117212 ай бұрын

    hey adam, do you know of a way to have a game do attack animations with different weapon models without copy and pasting the animation and changing every frame?

  • @BigIndianBindi-jy1cz
    @BigIndianBindi-jy1cz2 ай бұрын

    Hey Pal

  • @user-qd6wj2ri6w
    @user-qd6wj2ri6w2 ай бұрын

    If I want to add attack and attack animation in the first method how do I approach?

  • @swanik_yt
    @swanik_yt2 ай бұрын

    What colour pallet do you use plz give me the link 😢😢😢

  • @boohbehr
    @boohbehr2 ай бұрын

    4 the algorithm

  • @guiftavares8439
    @guiftavares84392 ай бұрын

    how can i make something like a attack or dash behavior? I can't get it. I made it in my own way, with spaghetti code, but i can't understand how to make the thing i created work within system in the video.

  • @St4rQu3st
    @St4rQu3st2 ай бұрын

    is there a reason you made each state a script that was attached to a game object and not just a static class that any object can reference or say a scriptable object system? Seems like more work making each state a sub game object.

  • @AdamCYounis

    @AdamCYounis

    2 ай бұрын

    They are components primarily to allow for instance data to be set in the inspector, at the game object level. A static class would not allow for this, and scriptable objects are designed as data objects, not really intended for runtime logic.

  • @St4rQu3st

    @St4rQu3st

    2 ай бұрын

    @@AdamCYounis I mainly meant to hold the data for what the state does and have a script that swaps between the states and takes the data from a different source than having say 200 sub game objects holding 200 scripts under the player in the hierarchy which seems less performant to me. So A manager script that references the Data for each state. Since states aren't changing the data on the fly then why would it need to be modified at runtime?

  • @captainawesome2226

    @captainawesome2226

    11 күн бұрын

    @@St4rQu3st Yep, that's the better way to do it.

  • @zaidhassan5114
    @zaidhassan51142 ай бұрын

    Where did you set isComplete =false in component state machine I write all the code as yours but do function and the if statement with isComplete in update did not work 😢

  • @AdamCYounis

    @AdamCYounis

    2 ай бұрын

    isComplete belongs to the State class, and I set it false in an Initialise() function from 31:55

  • @zaidhassan5114

    @zaidhassan5114

    2 ай бұрын

    Thank you

  • @DaydreamStudios_Official
    @DaydreamStudios_Official2 ай бұрын

    Who is Neil and why do we want to add him as a state?

  • @cyberblitz
    @cyberblitz2 ай бұрын

    Man, i still don't get your approach to state machines. I think where I struggle is how the code executes, particularly the do, enter, fixedDo etc take place. Been trying to understand this for months. Maybe it's just one of those things im never going to get.

  • @AdamCYounis

    @AdamCYounis

    2 ай бұрын

    24:38 - The update function of the "state machine" class (PlayerMovement for the sake of this tutorial) calls state.Do(), as well as SelectState() which handles Enter() and Exit().

  • @cyberblitz

    @cyberblitz

    2 ай бұрын

    @@AdamCYounis I have no idea what state Do, enter or exit actually do. I understand them conceptually; do, enter, exit something. I just really struggle with how they run logically; how they run line by line. The state object is, i believe, just a framework you can use over and over, and code within it can be trailered for things common across states etc. I just struggle how they are interpreted by Unity and order of code execution. This probably sounds complete jibbirish but I'm not used to creating objects in programming, and how to use them. im more of a functional programmer, and used to keeping things on one page.

  • @izzya8132

    @izzya8132

    2 ай бұрын

    @@cyberblitzTBH I'm not super familiar with Unity or with Adam's exact system, but maybe that'll help me explain this stuff to you in a more agnostic programming perspective. Basically, every frame, Unity calls Update() on all the active game objects Objects with a state machine will now call state.Do() and SelectState(), because that's what they were told to do in their Update() function. state.Do() is just a function, the same as functions in the paradigm you already know. It's going to do something. Maybe it tells the enemy AI to approach the player, as an example. Maybe it spits out "Hello, World." It doesn't matter. It is just running the specific lines of code written under "state.Do()" for the state that was assigned to this game object. SelectState() changes which State the game object is in, according to its current situation. This is just simple IF statements. If you're in the air, you're in the aerial state. If you're attacking, you're in an attack state, etc. It figures out which state the object should be in, and puts it in that state. Then, in future Update()'s (that are happening all the time as the game is running), the behavior will change according to the new state. On the line level, it's just something like this: Update() -> the line of codes under "Update()" run, calling the "state.Do()" function state.Do() -> the lines of code under state.Do() run, they come from the specific "state" we assigned to our character earlier. Update()-> now that state.Do() finished, Update() continues to its next line of code, which is calling SelectState() SelectState() -> the lines of code under "SelectState()" run.

  • @ezekielswiss5956
    @ezekielswiss59562 ай бұрын

    Awesome tutorial i REALLLY wish it was in godot though. I thought you switched to godot?

  • @AdamCYounis

    @AdamCYounis

    2 ай бұрын

    "switch" implies that you have to pick one or the other. My long term game is a Unity game and can't be ported to another engine. For smaller games I produce for jams, Godot is an engine I've begun to use, but it's far from a replacement for Unity at this stage.

  • @mela_pela
    @mela_pela2 ай бұрын

    You're using a tree to display a state machine that's so confusing. why not using state machine nomenclature?

  • @AdamCYounis

    @AdamCYounis

    2 ай бұрын

    Because my state machine data structures are hierarchical, they are literally trees.

  • @trvkv

    @trvkv

    24 күн бұрын

    @@AdamCYounis I think it's a valid point. You showed a tree structure at the beginning which was confusing, because state machine you shown later, in this particular video, is not hierarchical. It's state A to State B to State C to State A, basically.

  • @captainawesome2226

    @captainawesome2226

    11 күн бұрын

    @@AdamCYounis On that note, is your behavior tree actually connected to any code/logic or is it just a visual guide? Your implementation in Insignia is closer to a behavior tree than a state machine, technically speaking. LlamAcademy has a few videos on how those two differ from each other. So, is your behavior tree connected to any logic? (i.e. the one in the graph view. And did you code the graph view yourself?)

  • @AdamCYounis

    @AdamCYounis

    11 күн бұрын

    @@captainawesome2226 I coded the graph view myself. It's "connected" in that it visualises the current state, and I can force the character into a state by clicking it. But the graph is designed primarily to reflect the structure inherent in the code, it's a generated visualisation for debug purposes.

  • @user-se7ih2xn8u
    @user-se7ih2xn8u2 ай бұрын

    с этой стейт машиной один геморой, проще скрипты включать и выключать

  • @EpicGamerScout
    @EpicGamerScout2 ай бұрын

    Ew Unity

Келесі