How to Make a Simple State Machine in Godot 3.1

Поділитися
Вставка
  • Опубліковано 22 лип 2024
  • Howdy! If you've ever tried adding cool features to your player character, but quickly found that the code became confusing and riddled with bugs, then this video is for you. We're going to be taking a look at my most tried and true method for creating state machines. State machines will limit the scope of your code based on the condition it is in. This allows us to create things such as a more advanced character controller, as well as start dabbling in the basics of enemy ai, which we will be covering very soon!
    For a better look at why state machines are so useful, check out this link that explains why we need state machines: gameprogrammingpatterns.com/st...
    Interested in the clip of Transmogrified that I showed? It's a puzzle platformer where you transform the enemies into puzzle elements that you need to progress. Check it out here: playtransmogrify.com
    Become a patron to get exclusive content and support the channel:
    / gameendeavor
    Join Our Community! We look forward to welcoming you.
    Discord: / discord
    Twitter: / gameendeavor
    Into: Splashing Around - The Green Orbs

КОМЕНТАРІ • 204

  • @GameEndeavor
    @GameEndeavor  5 років тому +92

    IMPORTANT: Ignore 6:19 where I add the states in _state_logic. They're supposed to be added to the _ready method. How embarassing.
    Howdy everyone! Are you excited to learn about state machines?! No? Well I can't say I blame you, however state machines are a crucial skill to learn for when you're developing your games. In the future, I will be referencing this video countless times, so go ahead and get familiar with it.

    • @JVJF7
      @JVJF7 5 років тому +1

      Are you in the Godot discord group? Someone mentioned this (State machine) when I asked about characteranimation, what a coincidence. 🥳 Very usefull

    • @GameEndeavor
      @GameEndeavor  5 років тому +5

      @@JVJF7 I am, but that was kidscancode you were talking to. Another creator on this platform (and one of my favorites). Just did a search and it seems y'all were talking about this yesterday. That is a fun coincidence. :) Well I hope this video helps you. It really is amazing for handling animations, especially with enemies and such.

    • @Comakino
      @Comakino 5 років тому +3

      This is cool but could you say a bit more about getting the machine to change between states and how to organise my nodes? Im not clear on how to make the machine understand commands like should.chase etc.

    • @GameEndeavor
      @GameEndeavor  3 роки тому

      @LCWY Thanks. :) I'm not making tutorials any more, sorry. I'm doing devlogs instead.

    • @salsichalivre5401
      @salsichalivre5401 2 роки тому

      why do you say virtual instead of abstract?

  • @Mrguest69
    @Mrguest69 4 роки тому +23

    English is not my native language and I understand not very well when listening. So thank you for the English subtitles.

    • @GameEndeavor
      @GameEndeavor  4 роки тому +8

      Any time, buddy. :) I'm glad they're useful to you.

  • @vidvad
    @vidvad 2 роки тому +2

    Very comprehensive video, it's one of the simplest state machine I've seen in Godot and it's well made / well explained (I've watched all parts of this series and I'm re-watching to follow along and implement it in my project). Good job, thank you very much!

  • @D21_Hardbody
    @D21_Hardbody 4 роки тому +6

    Thank you so much for this. Been watching state machine tutorials for like 3 hours now and not a single person has explained it as well as this.

    • @GameEndeavor
      @GameEndeavor  4 роки тому +2

      Any time. :) I'm glad you found it useful. I had issues grasping the concept as well, so I tried to explain it as best I could.

  • @ashlin112
    @ashlin112 5 років тому +4

    Loved this! I’ve been searching forever to find a good tutorial on state machines because everyone I asked said I needed to learn how to make one if I ever want to create a game. I searched UA-cam and Reddit and pretty much all of the tutorials and advice was too confusing to understand but this one helped a lot! Thanks!! :D

    • @GameEndeavor
      @GameEndeavor  5 років тому +2

      Glad I could help! State machines were difficult for me to grasp when I first learned about them. But once you get the concept, they're easy peasy. And yeah, they're pretty darn important when it comes to making a game. I would say it is one of the most used dev skills that I have. I literally use them every day. Thank you for watching, I appreciate it!

  • @Temzy
    @Temzy 4 роки тому +55

    Hero name: Game Endeavor
    Quirk: Quick and easy Godot tutorials

    • @GameEndeavor
      @GameEndeavor  4 роки тому +5

      Easy, that's a new one. :)

    • @spachurrenGuias
      @spachurrenGuias 4 роки тому +2

      @@GameEndeavor Hello, I have the same issue as when using the animationtree node.
      When "jump" animation only starts at the ground reaching... only on "jump" animation not jump, fall.
      Same code to trigger the gravity change and jump animation transition... still nothing until landing (not time issue as it seems independent of long it takes to land from the jump)
      What can it be that it is making if work this way? is it for not having a jump and fall separated and called?

  • @moonrose1929
    @moonrose1929 5 років тому +13

    Looking forward to learning how to use this in the next tutorial. You go very fast so it's hard to follow along sometimes as a beginner, but I love your style of teaching, and look forward to more tutorials as a platformer is exactly the kind of game i want to make, and you're covering everything i need to know as someone who knows nothing about coding.

    • @GameEndeavor
      @GameEndeavor  5 років тому +6

      Thanks. My videos are mostly meant for intermediate developers, but I'm trying to design the videos in such a way that they're useful to beginners as well. Thank you for watching. :)

  • @0.lennart
    @0.lennart 4 роки тому +1

    Just discovered your channel. I've never seen so helpful Godot Tutorials. Thanks man.

  • @jacobcowan3599
    @jacobcowan3599 3 роки тому +1

    I subbed after watching you do the 4-person jam on miziziz's channel because I liked how clear and straightforward your feedback was.
    Your guides are very much the same! I look forward to watching more ^~^

  • @PhilHowlett
    @PhilHowlett 5 років тому +6

    Thanks for this video - I just got my 'ah-hu' moment on how state machines work. Your video explains it so easy on how to implement one (from a Godot point of view). I'll definitely be incorporating this in my new project. You just got a new subscriber :)

    • @GameEndeavor
      @GameEndeavor  5 років тому +1

      Glad to know I explained it in an easy to understand way. :) State machines took me a while to understand once I first started using them, but with this method I've never had an issue. Thank you for watching and subscribing!

  • @Topyy
    @Topyy 4 роки тому +5

    I don't know why but this intro is so satisying. And the fact that you're so straightforward in your tutorials makes it such easy to watch and understand : getting the information as quick as possible that is really cool, I like your channel

    • @GameEndeavor
      @GameEndeavor  4 роки тому

      Thank you! I'm quite happy with my intros. And yeah, when I'm trying to learn something, I want the information as quickly and thoroughly as possible.

    • @Topyy
      @Topyy 4 роки тому

      Since I started learning Godot you couldn't imagine how many tutorials I've seen of people taking loads of time to introduce the subject and make every manipulation a really long process !
      Great job on doing this channel and I can't encourage you enough to keep going !
      If I may give you a piece of advice, from someone who made radio broadcasts, you could articulate a bit more : you could try to imitate how they talk on TV or on the radio.
      But don't worry, how you do it right now is really decent anyways :)

    • @GameEndeavor
      @GameEndeavor  4 роки тому +1

      @@Topyy Indeed, many of the channels have a tendency to do that for Godot, which was part of what inspired me to make my channel.
      Thank you for the advice. :) I'll take a look, I'm always trying to improve, which helps if I have something I can study, so thank you. :D

  • @sharathkumar2297
    @sharathkumar2297 4 роки тому +10

    pretty neat tutorial!!! "dont be that guy" cameoutta nowhere cracked me up!! thank you for this :D

    • @GameEndeavor
      @GameEndeavor  4 роки тому +1

      Glad you enjoyed it, thank you for watching. :D Yeah, I like trying to sneak a little humor in when I can. :)

  • @nicholascurran1734
    @nicholascurran1734 2 роки тому +1

    I really like the flow of your videos and instructions. I have been doing self lead game development without a coding background, so I'll be rewatching some of these to get my mind wrapped around the concepts fully. The explanations are great and visually represented very well, so thanks for sharing.

    • @GameEndeavor
      @GameEndeavor  2 роки тому +1

      Thank you for watching them. :) Visual elements help me learn such things and keep me focused, so I made an effort to use them when I could. :D

    • @nicholascurran1734
      @nicholascurran1734 2 роки тому

      Old_state vs previous_state?
      Also from concurrent state machine video:
      Var main_states = parent.state_machine.states
      Where is the state_machine pulling from? Class name? Script label?

  • @ChefMarius
    @ChefMarius 4 роки тому +1

    Best video on creating FSM I've seen yet, really well done!

    • @GameEndeavor
      @GameEndeavor  4 роки тому +2

      Thank you! State machines can be intimidating when first learning them so I wanted to do my best on this one. :)

  • @devdurros5033
    @devdurros5033 3 роки тому

    thank you for providing me insight to the many layers that is the Finite State Machine

  • @GravityI
    @GravityI 4 роки тому +2

    Great tutorial! Best implementation of a state machine I've found so far. Thanks!

    • @GameEndeavor
      @GameEndeavor  4 роки тому +1

      Thank you. :) I'm glad you found it useful.

  • @fmhqbattousai
    @fmhqbattousai 5 років тому +11

    "sleeping angel" -- HA! love it.

  • @stanley9252
    @stanley9252 5 років тому +23

    Good programming, no-nonsense, and working in Godot? Subscribed! Keep up the good work. I'm hoping to learn a lot from you.

    • @GameEndeavor
      @GameEndeavor  5 років тому +3

      If that's what you want then you found the right place. That there is the foundation of this channel. Welcome, and enjoy the videos. :)

  • @5minutemovies977
    @5minutemovies977 5 років тому +2

    This is gold ! This is basically like writing an AnimationTree for behaviors, how cool !

    • @GameEndeavor
      @GameEndeavor  5 років тому +1

      That's exactly what it is. :) With the AnimationTree, you're creating a state machine for animations.

  • @Fupicat
    @Fupicat 4 роки тому

    Now that I know a bit more about the engine I can really appreciate these straight-forward quick explanation videos. It's more important to get the thought process across rather than just know what to type!

    • @GameEndeavor
      @GameEndeavor  4 роки тому

      That's my goal. :) I have no intentions of holding hands and telling exactly what to type. You can't learn and improve by copy-pasting code. You have to experiment, thinking about, and even struggle with the code a little to become a better developer. That's my opinion at least and the method I used to teach myself to program.

    • @Fupicat
      @Fupicat 4 роки тому

      @@GameEndeavor tbh I do prefer the hand-holdy tutorials as my introduction to a new engine. These code overview videos are more useful to grasp a single concept, rather than learn the language, that's why I'm only now getting something from them.

    • @GameEndeavor
      @GameEndeavor  4 роки тому +1

      @@Fupicat I never intended this channel to be an introduction to the engine. :) I would only recommend my channel to those who have gotten a grasp of the engine already and want to improve their skills further.

    • @Fupicat
      @Fupicat 4 роки тому

      @@GameEndeavor Ye that's what I like about it

  • @OhhGeeGee
    @OhhGeeGee 5 років тому +2

    Just learning Godot and found your channel just now. That has to mean something because it seems to be a perfect combination.
    Looking forward to steal, I mean borrow, ideas and tips from you.
    "..but don't be that guy." :D

    • @GameEndeavor
      @GameEndeavor  5 років тому +1

      It's not stealing if I freely offer them. :) Hope you're liking the engine so far, I think it's absolutely fantastic. I'll look forward to seeing you around.

  • @Lightwuz
    @Lightwuz 5 років тому +2

    This video couldn't come in a better time, thanks pal
    looking forward to seeing more of those huh :D

    • @GameEndeavor
      @GameEndeavor  5 років тому

      It's funny, you're the 3rd person to say something to that effect. Seems state machines are all the rage lately. :) I really didn't expect this video to do well at all. I had the mindset that most people wouldn't care, but it's something I need to get out of the way because I'm going to be using them in the future a lot.
      Thank you for watching! I look forward to hearing more from you. :D

  • @TheSteveTheDragon
    @TheSteveTheDragon 5 років тому +3

    Thank you! I was hoping someone would cover this!

    • @GameEndeavor
      @GameEndeavor  5 років тому +2

      I can imagine. I'm surprised more people haven't covered state machines.

  • @242feder
    @242feder 3 роки тому +3

    Where do we can find the implementation of the gds for each state and how we call them? Thanks!

  • @johndoe-is2fw
    @johndoe-is2fw 4 роки тому +2

    oh yes, every second content i love your tutorials!

  • @tanmaynarayantikam225
    @tanmaynarayantikam225 5 років тому +6

    Sir, really nice content... your videos are awesome they are time saving and exactly what we want... really nice work.... ☺

    • @GameEndeavor
      @GameEndeavor  5 років тому +3

      Thank you! I'm glad you like them. That's one of my main goals with this channel. Saving my viewers time while being informative and teaching practical developer skills.

  • @theroadgame7437
    @theroadgame7437 4 роки тому

    when i try to use the load state in the new note it says that it's not declared, does someone know why?

  • @puffinthefish
    @puffinthefish 5 місяців тому

    i know this is really old, but how do i actually.. connect my functions to each state? do i just put the code into the state_logic function? do i put it into a new function named state_[whatever thing it does] or just use the existing one?

  • @Sinchu9
    @Sinchu9 2 роки тому

    I'm back with more state machine related questions
    If I have multiple scenes I want to share a copy of a state machine setup with some states and transition conditions in it, should I attach that specific machine set up to a node and have the scenes have a initialised copy of that scene or is there a better way of doing it?

  • @SamiAhmed-ri3gj
    @SamiAhmed-ri3gj 3 роки тому

    You can communicate from the FSM to the player controller with the onready var parent = get_parent() in the statemachine. But how do you communicate from the player controller to the FSM? I tried get_child() and get_children() but cant seem to figure it out.

  • @nathanlindstrom4393
    @nathanlindstrom4393 3 роки тому +2

    All the code makes perfect sense, but how does the node inheritance thing work and why do we need to do it?

  • @batchallbro
    @batchallbro 2 роки тому

    when do you go into making this enemy? I really want to try it in my game to test states

  • @batchallbro
    @batchallbro 2 роки тому

    I don't understand this,I wrote all the code and such but where do I attach them to? It feels like I missed Something in the video

  • @vicpm8088
    @vicpm8088 4 роки тому +1

    Thank you for your good work in explaining everything, thank you very much for taking your time to make these videos, they are helping me a lot ... Greetings! I have already subscribed

    • @GameEndeavor
      @GameEndeavor  4 роки тому +1

      Thank you for watching. :) I'm glad you've found them helpful.

  • @iFiSiKz
    @iFiSiKz Рік тому

    What is should_chase()? Is there a gihthub where you have all this code?

  • @annoyed567
    @annoyed567 3 роки тому

    i seem to be getting an error i dont understand following this script, i downloaded the mannyquinny asset from godot main site and it uses a complex state machine, to understand it i decided to write my own state machine to garnish some more understanding by following your video. in the set_state function im getting an error
    its the line _exit_state(previous_state, new_state)
    with the error " many arguements for the "_exit_state() call expected at most 1"
    func set_state(new_state):
    previous_state = state
    state = new_state
    if previous_state != null:
    _exit_state(previous_state)
    if new_state != null:
    _enter_state(new_state)
    the only way the code doesnt bring up the error is by removing new state from the _exit_state line and previous_state from the _enter_state line, but i have a feeling it breaks the statemanchine. not actually got round to implementing the state machine but how do i get rid of this error

  • @sininenblue9161
    @sininenblue9161 3 роки тому

    would it be possible to get some sort of github link for the project since I would like to see it

  • @josuereis6367
    @josuereis6367 3 роки тому

    For a long time finite state machines did not enter my head (not as deeply as I wished). But, after watching another tutorial, I understood that what it does is simply to control the processing flow, in which the state machine is always running and the states (classes) are switched to process together with it, so that if I need to that more than one state is executed "at the same time", all I have to do is direct the flow to the first state and then to the other. Useful in fighting games in which I need to jump and hit at the same time.

    • @GameEndeavor
      @GameEndeavor  3 роки тому +2

      That is a bit more complex topic known as concurrent state machines, where you have multiple state machines executing simultaniously and interacting with each other. Typically state machines can only have one state active at a time, that's the point of state machines. So you can't jump and fall at the same time, you can only do one of these, and the state machine manages how these states flow from one to another, and limits the logic that is performed during these states. For example if you don't want to jump while falling, then you simple don't process the jump logic while in the fall state.
      I actually have a video on concurrent state machines though, if you're interested in that. I give the example of chewing gum and walking at the same time: ua-cam.com/video/BQwNiU5v9as/v-deo.html
      It can be a little tricky though, so I would make sure you're quite comfortable with normal state machines before getting overwhelmed with concurrent state machines. :) Good luck!

    • @josuereis6367
      @josuereis6367 3 роки тому +1

      @@GameEndeavor Oh, very thanks, buddy!

    • @josuereis6367
      @josuereis6367 3 роки тому

      @@GameEndeavor I'm doing a version of Street Fighter II, and, to execute the kick while I jump, I simply release the processing of the corresponding node (kick) while keeping the main node (jump) running together (the processing would come out of the state machine, would execute the jump node, would return to the state machine and then would execute the kick node). It would be a way to dispense with the need for a second state machine.
      ua-cam.com/video/9N9NdiGwfrI/v-deo.html&ab_channel=JosueReis

  • @wolcamophone4783
    @wolcamophone4783 Рік тому +2

    I'm working in G4.0 and the line "var state = null setget set_state" seems to return an error. Is there a way to update this for more modern syntax? I've made it up to the wall jumping tutorial and now my character will only ever face away from the wall once and never again when wall jumping.

    • @petbij
      @petbij Рік тому +7

      Change to var state = null : set = set_state

  • @GeekzAnonymous
    @GeekzAnonymous 4 роки тому +1

    In this video you mention going over some enemy AI in a future video. Did you ever end up doing that? I would love to see it.

  • @maxrobertstudios5160
    @maxrobertstudios5160 5 років тому

    Very nice sir! Subscribed 🙂

  • @moozdoomz7573
    @moozdoomz7573 4 роки тому +1

    God tier tutorial. Earned my sub!

  • @neilnattrass5838
    @neilnattrass5838 4 роки тому

    Hey sorry I'm real new at this. Could this state machine be used for player movement as well?
    (I'm dealing with the issue of my animation only flickering the first frame cause my current movement system plays the animation for idle or run by delta so its interrupting constantly)

    • @GameEndeavor
      @GameEndeavor  4 роки тому

      Howdy, nothing to be sorry about. Indeed it does. :) I even have a video on how to convert a player controller into a state machine here: ua-cam.com/video/j_pM3CiQwts/v-deo.html
      It should fix your problem.

  • @kabelsimpson4749
    @kabelsimpson4749 4 роки тому

    Anyone know why Godot would be returning a "setter function not defined" error for var state = null setget set_state? I haven't quite been able to wrap my head around the setget keyword.

    • @GameEndeavor
      @GameEndeavor  4 роки тому

      That happens when you don't have a function for the setget to call. Make sure that the method `func set_state(new_state):` is spelled exactly as you have it declared after setget.
      As for trying to help you get your head around it: setget is used to define a setter and getter for a variable. In this case we're telling it that `set_state` is a setter function to be called whenever an outside script tries to set our state variable. Instead of setting the state, it will call the method that we want it to use. You can read more about it here: docs.godotengine.org/en/3.1/getting_started/scripting/gdscript/gdscript_basics.html#setters-getters

  • @vaishnav_mallya
    @vaishnav_mallya 4 роки тому +1

    2:05 lol
    Your methods of teaching is great.

  • @minusmensch2040
    @minusmensch2040 4 роки тому

    thanks for the video!

  • @HeatGameDev
    @HeatGameDev 5 років тому

    Really good video!
    I've been stuck with a question. how does the statemachine know in which state the parent is currently in. does the angel script have a exported var with the state in it?

    • @GameEndeavor
      @GameEndeavor  5 років тому

      That is stored in the state machine. It's the state variable.

    • @HeatGameDev
      @HeatGameDev 5 років тому

      @@GameEndeavor Oh ! thanks. now I understand. thanks for the reply

  • @joelchrono
    @joelchrono 4 роки тому

    I have seen some tutorials using animation trees for state machines, but I am a beginner and I dont understand, should I use this? or should I use animation trees?

    • @GameEndeavor
      @GameEndeavor  4 роки тому

      AnimationTrees state machines are meant for animations. This state machine is more geared towards logical state machines and can manage animations. You can use either, it's mostly a matter of preference. From what I've seen of AnimationTrees however, I feel like this kind of State Machine is easier to manage. But that's my preference.

    • @joelchrono
      @joelchrono 4 роки тому

      @@GameEndeavor Thanks for replying so quickly! I will try to understand this state machine

  • @thisisgod2639
    @thisisgod2639 5 років тому

    Efficient and precise tutorial

  • @dumpsterDeity
    @dumpsterDeity 3 роки тому +3

    Thank you for the tutorial! I'm just starting to learn Godot, and your videos help me so much.
    Can I ask a question? Why do you spell some of the functions with an underscore in front of them (like "_get_transition()") and some of them without the underscore (like "set_state()")?
    Is there some sort of rule to this?

    • @GameEndeavor
      @GameEndeavor  3 роки тому +1

      Glad to be of help. :) It's a convention thing. Methods with underscores in front mean that the method is private and should only be called within the script. It's a way of knowing how you can interface with a class outside of the class.
      So no where else in the engine should `_get_transition()` be called other than the state machine. It has one purpose that only it should be using. But `set_state()` is fine because something can change the state externally. For example if the player takes damage then it might call the set_state method to set a stunned state.

    • @GameEndeavor
      @GameEndeavor  3 роки тому +2

      Just a heads up. I will break this convention when calling methods from the state machine to its parent. This is because I see the state machine as an extention of the parent, but I keep the methods in the parent for organizational purposes. For example the player might have the method `_apply_movement()` that I normally would only want to be called within the player script. But adding an external state machine, I would want it to use that method, but if someone were looking at the list of methods I wouldn't want them to think they should call that method outside of the player's logic. So in this instance I keep the underscore in front of the method, even though I access it externally from the state machine, otherwise all methods would be seen as publicly accessible.

    • @dumpsterDeity
      @dumpsterDeity 3 роки тому +1

      @@GameEndeavor Thank you so much for the detailed reply! I was wondering if I should put those underscores in as well, and now I've got the reason why I probably should.

  • @trongdo9059
    @trongdo9059 4 роки тому

    Thanks for the great tutorial, but i have a question. If i want to make more enemies, each one of them have some same states like idle, walk but some have chase state or jump state. How would i do about this without duplicating the idle state in another scripts. Sorry but i'm really new to this game dev thing. And sorry about my english.

    • @GameEndeavor
      @GameEndeavor  4 роки тому +1

      Howdy. :) Yeah, this can be a bit tricky. Especially with this state machine since it's meant to be a simple one for beginners to quickly grasp the concept of. For what you want, it might be worth it to to go for a more modular approach to state machines.
      That said, I often find that my states vary quite differently from enemy to enemy. Like, what they do in their idle state is often different, how they transition from the idle state. What states they transition to from the idle state. There's a fair bit you'll have to design for to prevent duplicate code with state machines, as it'll need to take several things into account. Just a heads up. :)
      Your english is fantastic, so no worries. :)

    • @trongdo9059
      @trongdo9059 4 роки тому

      Game Endeavor Thank you. I think i know what to do next :) Keeping your good work, i really love your tutorial

  • @ResistantLaw
    @ResistantLaw 3 роки тому

    Would it make sense to put this at the top of the set_state function: "if new_state != state:" ? Otherwise, enter/exit state would keep getting called right?

    • @GameEndeavor
      @GameEndeavor  3 роки тому

      It depends on how you want the state machine to function. First off, enter exit would not keep getting called. I assume you're referring to us calling set_state() in _physics_process()? If so then we only call it if _get_transition() does not return null. Which it only does when we want the state to change. Otherwise the method does not get called.
      But the important thing is that adding `if new_state != state:` prevents you from entering the state from the state you're already in. For example, say you wanted to implement a double jump. You want to be able to double jump from the jump state. You could just enter the jump state again, and reapply the new jump velocity. This may or may not be necessary depending on your jump animation, but hopefully you get the idea. This could apply to other things.

    • @ResistantLaw
      @ResistantLaw 3 роки тому

      @@GameEndeavor Thanks for the reply! Yes, you're exactly right, I was thinking the state would keep getting set in physics process. But I forgot that it depends on there being a transition. After reading your comment I remembered that I didn't really understand the _get_transition() part originally. But I've been messing around with it more for the past few days and I think it makes sense now.
      I didn't know about state machines before this but the essence of a state machine is pretty straight forward (in my opinion). It just might take me a bit to set it up properly for my character.
      By the way, would it be appropriate to use this same template for game states? I've done this in the current project I'm working on, but I was wondering if there is any reason this would not be appropriate.

    • @GameEndeavor
      @GameEndeavor  3 роки тому +1

      State machines are funny that they're super simple. But for some reason they're a little tricky to wrap your head around at first. Which is why I tried making this video as straight forward as possible.
      You can use a state machines for game states. :) That's why they're called game states. You can use a state machine for anything that can benefit from state logic. State machines are even used often outside of game development.

  • @lauti6500
    @lauti6500 3 роки тому

    hey dude, nice vid, following along and learning! just one problem, it gives me the error "the setter function isn't defined", any idea why?

    • @GameEndeavor
      @GameEndeavor  3 роки тому +1

      The function defined as the setter doesn't exist. Check your spelling.

    • @lauti6500
      @lauti6500 3 роки тому

      @@GameEndeavor ...yeah i just wrote it again and it worked, what the hell did i write lmao ?? thanks a lot !!!!!!

  • @neilwalker4277
    @neilwalker4277 3 роки тому

    Hello, this might be just me and my janky code, but in the set_state method I found I needed to wrap a "if new_state!=state:"
    statement around the setting of previous state to stop repeated state changes to the same state updating previous_state. For me, this is because I have a 'lock' state that overrides all other states.

    • @GameEndeavor
      @GameEndeavor  3 роки тому

      I don't do that because sometimes I want to set my state again from the same state so that the enter state logic plays. Attack queue'ing for example. At the end of my attack state, I check to see if an attack is queued, if so then I reset the attack state so that it attacks again.

    • @neilwalker4277
      @neilwalker4277 3 роки тому

      @@GameEndeavor not doing it is probably the proper way every time, just I gave up on brain power. It'll probably come back and bite me 😉
      Anyway, my code is much lovelier now and I prefer this way over the gdquest or simple enum FSM approach.

  • @herbhbut
    @herbhbut 4 роки тому +2

    Hello , I have a question about how you perform matches to your state in the "SleepingAngelFSM.gd" script. ie: at 7:12 your code shows
    match state:
    states.sleep

    Doesnt that match against the value in the dictionary and not the key? states.sleep = 0 , so this won't match with "sleep" ever.
    Maybe i'm confused about FSM and misread your code, but in my implementation this is what happens. When adding new states instead of 'states[state_name] = states.size()' i did 'states[state_name] = state_name' and now it works. However this creates a dictionary with identical key-values which seems pointless.

    • @GameEndeavor
      @GameEndeavor  4 роки тому

      Howdy. :) Correct, `state` will never match "sleep" as a string, because I'm not setting state to "sleep". I'm setting `states.sleep` to 0, and when I set the state to sleep, I'm essentially saying `state = states.sleep` ie `state = 0`, so state will match with states.sleep because they're both 0. You could even say:
      match state:
      0:
      And this would also work, if you're using my code. The dictionary's keys are for us, for readability purposes. The value is for the engine to see if the state's value matches with the value in our dictionary. The value makes no difference to us, it just needs to be unique to our dictionary. You could even say `states[state_name] = randi()` if you check that the value is unique. As for why yours didn't work when you used `states.size()`, no idea. It works fine for me, and I use this method in practically every game I work on. I would double check the code to make sure you have everything just right.

    • @herbhbut
      @herbhbut 4 роки тому

      ​@@GameEndeavor okay I think I know where I went wrong.. Let say you wanted to change the state outside of both state machine scripts. You would do set_state(0) and not set_state("sleep") ?

    • @GameEndeavor
      @GameEndeavor  4 роки тому

      @@herbhbut Technically yes. Though for readability purposes, and should that number ever change, I would instead say `state_machine.set_state(state_machine.states.sleep)` state_machine being whatever reference you have to the state machine.

    • @herbhbut
      @herbhbut 4 роки тому

      @@GameEndeavor awesome, thanks for all your help! One last thing. I need to also get the state name (key) on some occasions. I used the get_state method and returned the key based off the value of current state using a series of match statements to determine the key. ie:
      match state:
      states.Sleep:
      state_key = "sleep"
      This seems not an optimal way to do this, do you know of an easy way to get a key from a dictionary? been scouring the web for information on godot dictionaries and not finding much info beside the docs

    • @GameEndeavor
      @GameEndeavor  4 роки тому

      @@herbhbut Yeah, I meant to include this in the video but forgot to write it into the script. Since we're using iterative indexes starting from 0 for our key values, we can use those to access the string values of our states. So `states.keys()[state]`. keys() returns an array of the keys in our dictionary (the strings), and we use `state` to pick which one we want.

  • @JoeBob-rc3cp
    @JoeBob-rc3cp 4 роки тому

    4:18 I could be completely wrong, but should 'previous state' on 05 be 'old state' as it appears in the rest of the machine (same with 'new_state' and 'state")? Sorry, I just can't figure out why you'd want different variables for that.

    • @GameEndeavor
      @GameEndeavor  4 роки тому

      It's been a while so I can't say for sure, but I believe I did it for readability. Specifically for state / new_state. If we used the class properties, then we would drop them as parameters so you'd have the method `func _exit_state():` and if in it you were to use the `state` variable, then it could get confusing as to whether you meant the current state or the previous state. So most likely I did that for readability.

    • @JoeBob-rc3cp
      @JoeBob-rc3cp 4 роки тому

      @@GameEndeavor ah, cheers. Nice tutorial btw, it most mostly helpful (im doing something a little different so it wasn't directly applicable, but it helped with a basic understanding). Thanks.

  • @DraftAniel12
    @DraftAniel12 3 роки тому

    I'm kinda lost.
    How can I connect the State Machine to the node?

    • @GameEndeavor
      @GameEndeavor  3 роки тому

      You add the state machine script to a child of the node. For example I'll instance a Node, name it something like PlayerFSM, and give that node the script for the player state machine.

    • @DraftAniel12
      @DraftAniel12 3 роки тому

      ​@@GameEndeavor Oh I get it now, thank you

  • @TheBuffED
    @TheBuffED 5 років тому

    This was great!

    • @GameEndeavor
      @GameEndeavor  5 років тому +1

      Thank you! I'm glad people seem to enjoy it. I honestly didn't expect it to do so well. :D

  • @illusioon1569
    @illusioon1569 2 роки тому

    Very cool

  • @lv99redchocobo37
    @lv99redchocobo37 3 дні тому

    could you remake this using godot 4 and utilizing nodes?

  • @5minutemovies977
    @5minutemovies977 4 роки тому +1

    I've watched this video and the player state machine one countless times, and I can't have it working properly. So I'll keep watching. Godot does not even give me an error, which is horrrible, it means everything is correct for it.

    • @GameEndeavor
      @GameEndeavor  4 роки тому +1

      What's it doing / not doing. I might can help debug.

    • @5minutemovies977
      @5minutemovies977 4 роки тому

      @@GameEndeavor Oh it's nice of you to answer.
      I've tried to print the state and other variables at different times, in different functions to see what was working and what was not.
      Basically, what I understood is that it won't transition from state to state.
      The call_deferred() didn't work either.
      My project is a top-down perspective game, so I can't just copy/paste blindly what you do in your video, but I have faith that I'll eventually find what I messed up.

  • @oleksybabansky
    @oleksybabansky 4 роки тому

    Wow, that was very quick. I didn't understand it yet. I see only states, states, states. Where is the actual code that controls the player? I didn't understand anything yet.

    • @GameEndeavor
      @GameEndeavor  4 роки тому

      This video isn't meant to teach you who to control a player. It demonstrates how to implement a simple state machine to control things (not limited to a player). ie you can use a state machine to control the flow of logic for AI or other complex behaviors. I have a video on implementing a player controller here: ua-cam.com/video/7JCRpY3biZg/v-deo.html and another video on converting that player controller to a state machine here: ua-cam.com/video/j_pM3CiQwts/v-deo.html

  • @khalfy_kun
    @khalfy_kun 3 роки тому

    I like your funny words magic man.

  • @witsz
    @witsz 5 років тому

    moon walk sold me to subscribe to you hahahaha

  • @juanmena3648
    @juanmena3648 5 років тому

    Oh my god, thanks for this.
    How did you learned how to do this?
    Did you used the official docummentation?

    • @GameEndeavor
      @GameEndeavor  5 років тому

      No, I designed this method myself. :)

  • @dustinmorse8497
    @dustinmorse8497 5 років тому

    Do you post your code anywhere?

  • @jwerlley
    @jwerlley 5 років тому

    very nice

  • @Foxuniverso
    @Foxuniverso 3 роки тому

    For some reason it says my setget has an error
    "The setter function isn't defined" anyone have a clue of what it could be? I wrote everything as in the vid, i even double checked so im not sure what it could be 🤔
    (Unless if i missed something)

    • @devnirwal6888
      @devnirwal6888 3 роки тому

      define a function with the name you used after "setget" and pass it

    • @Foxuniverso
      @Foxuniverso 3 роки тому

      @@devnirwal6888 yeah i figured it was that so i erased it, thing is i followed another tutorial on making the movement and i needed the state machine so i looked this one up, i figured it out though, and on the same script! thanks anyway for the help!

    • @devnirwal6888
      @devnirwal6888 3 роки тому

      @@Foxuniverso np... btw is this state machine working good? I am still searching for a state machine ;-; (idk hows one better that another)

    • @Foxuniverso
      @Foxuniverso 3 роки тому

      @@devnirwal6888 im not 100% sure my self lol xD i think its a case by case basis, they're all basically the same just some can be more complex than others so you can use any SM it really just depends on how complicated you want it to be, i decided on using one i found thats made by HeartBeast, it worked fine, i also used reddit a lot, also trial and error!
      so maybe give that a shot, like UA-cam HeartBeast State Machine, and it could work for you if the one in the vid didnt

    • @devnirwal6888
      @devnirwal6888 3 роки тому

      @@Foxuniverso Oo yes HeartBeast, I saw his video. If i am guessing correctly he uses enum and match statement to make state machine. I tried it but... all the states in one big script... Ooof I think i will try one with different node for each state like in this video or gdquest one ( I'm getting a headache.. need to sleep probably :V )

  • @msabyss_3998
    @msabyss_3998 2 роки тому

    Followed this tutorial, ended up with a script, not sure how to use it in my project, can you please make a video on how to use it, Thank you

    • @GameEndeavor
      @GameEndeavor  2 роки тому +1

      Literally the focus of my tutorials was how to use the state machine. :) Check out my other videos, like the one about wall jumping or crouching / crawling.

    • @GameEndeavor
      @GameEndeavor  2 роки тому +1

      The purpose of this video was for me to point to when I used it in my other tutorials. It's not meant to demonstrate it. The othe videos do that.

    • @msabyss_3998
      @msabyss_3998 2 роки тому

      ​@@GameEndeavor I found the video, Thank you man ! this helped a lot

    • @GameEndeavor
      @GameEndeavor  2 роки тому

      @@msabyss_3998 Any time. :) Glad it was useful to you.

  • @johnblazevirtudazo6112
    @johnblazevirtudazo6112 3 роки тому +4

    It has been a year, and still waiting for the ai :(

    • @GameEndeavor
      @GameEndeavor  3 роки тому +6

      Aww, sorry enryuu. :) I never got around to it. But I don't do tutorials anymore. :( If it means anything I do feel bad about it.

  • @grzegorz8670
    @grzegorz8670 5 років тому +1

    oh cool you back

    • @GameEndeavor
      @GameEndeavor  5 років тому

      Indeed. I came back a few weeks ago. I missed making videos, so I'm glad to be back.

  • @collinvisser7108
    @collinvisser7108 3 роки тому

    Is this code on Github ?

  • @Ceisriel
    @Ceisriel Рік тому

    I honestly didn't understand anything but if you say it's better, ill try cause for now my enemy ai lag.

  • @lacasadeacero
    @lacasadeacero 3 роки тому

    A bear followed by an angel. ¿How did you guessed me? Haha

  • @lucaslevin3395
    @lucaslevin3395 5 років тому

    Great video! You mention nested inheritence for states - how would you go about this? For example, if I wanted to only be able to shoot once while in the air, I'd have an 'air' state and a 'air_shot' state that inherits from air, but not quite sure how to do this in GDScript.

    • @GameEndeavor
      @GameEndeavor  5 років тому +1

      Well I meant nested inheritence for the state machines. For example if you had a guard patrol state machine and wanted to inherit from it to make a guard patrol but with a flashlight. However, if I were to do state inheritance then I would probably have scripts for each individual state. Recall in the video I said that the state variable can be whatever you want to use. It could also be a node/script/resource with its own methods that you call. You could inherit from that and override the methods that are called.
      That's when you start getting into a more complex modular state machine though. I would like to cover a modular state machine in the future. I just have to finish designing it first. :D

    • @rafaelgpontes
      @rafaelgpontes 5 років тому

      @@GameEndeavor Please, do this modular state machine!! I was wondering how I could implement complex parallel independent behaviors like the example this guy mentioned (shoot while falling, or aim into a certain direction while running or jumping etc). This seems really important for more realistic actions. Maybe I should make independent state machines for different parts of the game entity (like one state machine for the arms and another for the rest of the body)?

    • @GameEndeavor
      @GameEndeavor  5 років тому +1

      @@rafaelgpontes That's exactly what you do. :) It's called concurrent state machines. You have one state machine for your character, ie idle, walking, jump, and another for actions ie shooting, melee attacking, etc. I will probably do a video on concurrent state machines and modular state machines soon. Kinda forgot I wanted to do them, but you've motivated me to push them up on my list of topics.

    • @rafaelgpontes
      @rafaelgpontes 5 років тому

      @@GameEndeavor yes! That would be awesome! I think I have an idea of how I could tackle this problem, but it's always nice to have a second opinion! Your tutorials are really helpful and insightful.

  • @JSena-ff8we
    @JSena-ff8we 3 роки тому

    Are you still gonna make tutorials? I love your devlogs but I miss your tutorials a lot.

  • @proatgaimz
    @proatgaimz 4 роки тому +1

    Instead of adding the states in the script at 6:30, why not just add a node as a child of Statemachine node, for each state and attach a script for each corresponding state node.
    For example in the node tree it would look like this:
    - SleepingAngelFSM
    - Sleep
    - Chase
    - Attack
    I would really like to know what your thoughts on this are!
    Thanks for the great tutorial!

    • @GameEndeavor
      @GameEndeavor  4 роки тому +2

      Personally I'm not a fan of this method. I find it to be a bit cluttered and I prefer to have all of my state machine logic in one easily readable place. The only reason I can imagine to separate them like that is if you have a really complex state machine, and I've never needed to write a state machine so complex that I felt warranted such a measure. With the exception of strategy games, enemy ai is usually extremely basic. Player ai is more advanced but only slightly so.
      Having said that, even if I were to take that type of approach then I wouldn't do it with nodes. Nodes have a different function than a state would. A state just needs to execute snippets of code. It doesn't need processes, ready, access to the SceneTree etc. Its role is more suited to a Reference object. So I would probably create state references and have the state machine call its methods when necessary.

    • @proatgaimz
      @proatgaimz 4 роки тому

      @@GameEndeavor
      Instead of adding the states via code, you can sort of automate it , by just looking at the children of the FSM, these child nodes however does not process any code and is only there for a visual hint for the programmer/ designer what states exists we would still use the add_state method to add the states.
      just in the base State machine code you can get all the children of the node , iterate over each one and call add_state(child.name) and if you're worried that there's performance overhead i guess you could even set_processing(false) on each child while iterating. its basically a visual dictionary for the states.

    • @GameEndeavor
      @GameEndeavor  4 роки тому +1

      @@proatgaimz Yeah, you could do that. There's a list of the states at the top of the script though, which is visual enough for me. I'm not a visual programmer at all. :) In fact visuals usually confuse me when programming which is why I'm not a fan of this approach.
      GDQuest recently sent me some ledge grab code to look at which uses the style that you mention, and I found it exceptionally confusing to navigate and find the code that I needed to see.
      If you're just using the nodes as a list and they don't have a script on them (or they have a script but nothing in the process functions) then it's not overhead in that regard. The process functions are opt in as I understand it. They only process those functions if you have them. If you'll look at `get_groups()` of a node, you'll find that they get added to groups when they use those methods. I'd wager Godot uses that to determine who to use those functions for.

  • @aheahetaroman
    @aheahetaroman 4 роки тому

    Does the state machine editor exist in 2d?

    • @GameEndeavor
      @GameEndeavor  4 роки тому

      What do you mean, as in a visual graph?

    • @aheahetaroman
      @aheahetaroman 4 роки тому

      @@GameEndeavor
      yes,it mean visual state machine.
      this.
      docs.godotengine.org/en/3.1/tutorials/animation/animation_tree.html
      docs.godotengine.org/en/3.1/_images/animtree11.gif
      Is this for 3d?

    • @GameEndeavor
      @GameEndeavor  4 роки тому

      No, this is a code based state machine. It doesn't have a graph builder, but can be used for both 2D and 3D, or even without any graphics. It's just a means to organize logic.

    • @aheahetaroman
      @aheahetaroman 4 роки тому

      @@GameEndeavor
      Thank you
      I understand

  • @flaviokonti5522
    @flaviokonti5522 4 роки тому

    Awesoem tutorial thanks. I would just liked if you talked a bit slower its hard to understand you when you talk this fast. I tried slowing down but that sounds weird lol

  • @Skulll9000
    @Skulll9000 2 роки тому

    "error(4,1): The method "add_state" isn't declared in the current class."
    edit: Nvm, it's working now. I don't know if it was solved by changing the "extends StateMachine" to something else like "extends StateMachineGe", or simply by saving one of the two scripts.

  • @danieldrew2356
    @danieldrew2356 4 роки тому

    This is a very good tutorial. Do you think it matters that the state machine will be 'calling upwards' to its parent?

    • @GameEndeavor
      @GameEndeavor  4 роки тому

      Nah, so long as it has state machine functionality, it doesn't matter how you have it setup so long as it's converted properly.

    • @danieldrew2356
      @danieldrew2356 4 роки тому

      @@GameEndeavor Yeah it seems all other implementations call up to the parent too. I have actually been using ur design for ages now and its super elegant. Was just curious tbh. Anyway, thanks a bunch!!

    • @GameEndeavor
      @GameEndeavor  4 роки тому

      @@danieldrew2356 Any time. :)

  • @kimbaptempura4073
    @kimbaptempura4073 2 роки тому +1

    Great video until 6:12 , where I'm like WTF did he just do !?

  • @joelchrono
    @joelchrono 3 роки тому +2

    Hopefully my game doesnt break when trying to implement this

  • @jaatavamokkapala6673
    @jaatavamokkapala6673 3 роки тому

    good video
    thanks
    portal 2 is good btw

  • @gamemakerjay8840
    @gamemakerjay8840 5 років тому

    wow, this is great! thanks, my coding life just gets so much easier!

    • @GameEndeavor
      @GameEndeavor  5 років тому +1

      For sure. I use this practically every day. Coding would be so hectic without a good state machine.

  • @rareraven9951
    @rareraven9951 4 роки тому

    Like writing your spells in a spellbook, so you don't have to memorize em.
    or something...

  • @maxllb2334
    @maxllb2334 3 роки тому

    Hey bro, can you tech us how to make a game like EVO of SNES?

  • @nopens
    @nopens 3 роки тому

    Very fast, very hard to understand, i read a ouple of articles about state machines before going here and after watching it it seems i kno even less know that just right after reading articles. Very confusing example.

  • @samuelallen6494
    @samuelallen6494 4 роки тому

    why would you have moon walking?

  • @mr.sentryanddispenser
    @mr.sentryanddispenser 6 місяців тому

    Whats the point of a state machine,I dont get it

    • @GameEndeavor
      @GameEndeavor  6 місяців тому +1

      It's mostly an organizational tool. Say for example you were to implement a platformer character that can crouch and jump. If they crouch, then jump, then release crouch, then you'd need to make sure they don't exit the crouch state to a standing state in the middle of the air. This gets exponentially more complex the more things you add. A state machine organizes this and makes it much easier to manage. I'll link an article that explains it better, including code examples to show how it can get complex:
      gameprogrammingpatterns.com/state.html

    • @mr.sentryanddispenser
      @mr.sentryanddispenser 6 місяців тому

      @@GameEndeavor Oh, that's a good explanation, thank you!

    • @GameEndeavor
      @GameEndeavor  6 місяців тому

      @@mr.sentryanddispenser Any time. :)

  • @kyonas6047
    @kyonas6047 2 роки тому

    at 6:12 u did something idk what it is with the editing +speed i couldn't understand for the past 927383 minutes lol its oki if u stoped doing totorials they harder to make than making games themselves

    • @GameEndeavor
      @GameEndeavor  2 роки тому

      I attached a script to the node that inherits the StateMachine script, then copied over the virtual methods. Making tutorials is a lot easier than making games.

  • @tyraniusdelten8207
    @tyraniusdelten8207 3 роки тому

    😳

  • @Lukifah
    @Lukifah 2 роки тому

    this tutorial is incomplete as you don't show the enemy script

    • @GameEndeavor
      @GameEndeavor  2 роки тому +1

      This is not a tutorial for enemy state machines.

  • @OmegaF77
    @OmegaF77 2 роки тому

    Didn't realize that I needed Mike Tyson to teach me Godot state machines.

  • @lexolotlgod
    @lexolotlgod 13 днів тому

    6:11 - 6:20 makes no sense to me, what is it you're doing? You have a new node tree, did you inherit the state machine node you made? How do you get a second script for it? The FSM one on top of the one that comes in. You blew through in 9 seconds what is like... several minutes of work, let alone understanding....
    Edit: Got it.

  • @notoriousadrian4369
    @notoriousadrian4369 3 роки тому

    this is definitely not for beginners :'0 didnt understand anything