Beginner's Guide To Behavior Trees In Unreal Engine! | Step-By-Step With Theory And Demonstration

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello there my name is Jackson and today we're going to be creating a simple Behavior tree AI this is going to be a beginner's tutorial so straight away um I'll just show you how to make a project I guess we'll keep this really simple um open your epic games launcher click launch of your engine whichever engine you've got installed so that's up in the top right I'm in 4.27 today click games new third person uh next starter content enabled and then all the other settings there are fine and then I'm going to call this tutorials 2023 and I'm going to turn this into a tutorial series I think where we cover a lot of the basic stuff in Unreal Engine and actually explain it properly because in a lot of my previous videos I've sort of just blitzed through things and I never really explained a whole lot of what was going on so that's what we're going to do a little bit in this series we're going to slow it right down so in my content browser I'm going to right click and create a new folder and just call this tutorials because this is where I'm going to put all the tutorial content um now we're going to be creating a simple AI from scratch today AI meaning artificial intelligence which can move around randomly and we're going to use Behavior trees to do that because Behavior trees are a much more powerful way to do AI then just inside of a blueprint event graph and that'll become obvious why as we go on so to create a new character we're going to go new blueprint class and type character and just call this BP underscore my simple character I like to put my in front of things um to differentiate between something else that might be in the editor that I didn't make so I know everything with my in front is mine now straight away we can drag him in and you can't see anything there because it doesn't actually have a body so let's just give him a body um this is what he looks like right now so if we go add component and go static mesh and just call that SM and then just add a cube our dude can be a cube today okay um that's rad okay so that's our AI he's there okay that's his body he exists he's in a in a world right um but at the moment he's just a body and he doesn't have a the way I like to think of AI is like AI is sort of the brain that you give to a character this is just another character here now if I hit play and jump in my this character has been possessed by my controller my player controller um but this dude over here this hasn't been possessed by anything he's got no brain he can't do [ __ ] um and likewise if I just hit simulate like this we've got two characters the one that we just created and this other character that started in the scene and they're both bodies but neither of them have got any brains and they don't actually know how to do anything um so that's the first sort of concept I guess is this idea of possession it's like you can have a character that exists in your game but it's just a body unless you give him a brain so the next thing that we need to do if we're going to be creating what we call AI which is artificial intelligence or ship moving around like by itself as if it had its own free will and like whatever um we need to go into our content browser new blueprint class we need to make a brain for the AI so if you go to all classes down here and then type in AR controller you type in an AR controller you select AR controller and I'm just going to call it AIC underscore um what we'll call it simple brain because it's a simple brain for an AI so down in the content browser we have the body and we have the brain now we need to tell the body to use the brain so if we go into the simple character um click on the top there my simple character and then over on the right uh AR controller class it has been given a brain AR controller but that's just the default one we're going to tell it to use our brain that we've given it okay um and auto possess AI placed in world or spawned I always like that to be all spawned so whenever you spawn one of these in the game um sometimes your AI doesn't do anything because it doesn't actually have an AI controller so I always like to have this set there okay so now he has a brain now we have a body we have a brain now we need what would you call it like neural networks and like subroutines and um things that it can do like if you think about a person and the behaviors that a person engages in one way to conceptualize their behavior is it's sort of like a script that's running inside their brain which is controlling their body that's one way to think about it or you can think of it as like a a script which is executing itself through a person's soul which is a player controller which is then animating their body that's another way to think about it it's the same with AI the AI has a brain now it has a body which can move given inputs from the brain but the brain doesn't have any information to actually execute you need to feed information into the brain and invent decision making and routines and movements and actions to tell the brain how to use the body so that's the next thing that we need to do and the way that we do that you can do it in the event graph but um anything in this event graph is not reusable across different characters it'll be restricted to this event graph or anything that we create a child from this from this event graph so if you want to be able to use different behaviors for different brains in different bodies you can abstract all of that out into what's called a behavior tree and in Behavior tree tasks so that's what we're going to do now and this is really really simple um there's so many complicated tutorials on this [ __ ] and it's like everybody sort of misses the point um Behavior trees are literally just it's it's a mechanism for decision making and they don't have to be complicated at all um so if you've got artificial intelligence and then type in Behavior tree and just call this BT and now let's give it a specific name knowing that this is not tied to a character so I'm not going to call this my simple character BT underscore my simple character because the simple character is a body and I'm not going to call it BT underscore simple brain because it's not a brain what this is is this is set of instructions in a particular pattern of behavior which is then fed through a brain and then executed in a character so if you think about it in terms of a behavior what kind of behavior is this today we're going to call it random roaming okay hopefully I'm not going too fast if this is the first time you've watched something like this um a lot of what I'm saying will be too quick you won't be able to digest it which is okay um so what I recommend to do if you're watching these videos and you're new to Unreal Engine and you're new to game development and a lot of the concepts that I'm talking about here is to watch these videos from start to finish first without trying to build anything that I've been making maybe even watch them twice and then once you feel like you understand everything that I'm saying then what you do is you go back to the start you watch it you see what I'm doing you pause it you recreate what I'm doing you start it again pause it create what I'm doing and Skip back and forwards as you need that's how I recommend that you do it if you're a beginner to this stuff if you're Advanced and I've got plenty more advanced tutorials on my channel where I just Blitz through [ __ ] like a [ __ ] psychopath a soccer path on 24 Red Bulls and some LSD but today we're gonna just today we're gonna be just chilling and going slow and vibing all right there's this sort of hangout in-depth learning time um so we're gonna have a random roaming Behavior now if we open that up there's not a whole lot in here um because well it's not meant to be because it's like basically a blank piece of information we're going to have a selector node which selects stuff we're gonna have a white node which Waits really simple stuff so far so basically if I run this now I mean this is already Behavior tree technically like literally um so why don't we just run that we can literally run this now and I can show you what's running so to get this guy to run a behavior tree we could inside of the simple character go begin play run Behavior tree blah blah blah but then you'd have to add this for every single character like if you wanted to have a separate character you know which could be um BP my second simple character and say he was like a freaking triangle or something like if he was completely different um you'd have to tell this to run a behavior tree as well and then it's like you're copying code from character to character and you don't need to do that whenever you find yourself copying lots of code there's probably a better way to do what you're doing because it's a waste of time so you don't want to be using the same code in different places you want to be using or copying the same code to different places I mean you want to be using code in different places but all drawing from the one place and so the way that I like to do that I mean there's a million ways that you can do this but the way that I do it is I create a blueprint class I create an actor component and call this AC underscore um run Behavior or something like that an actor component is literally what it says it is it's a component which can be added to an actor so if I go into my BP simple character and go AC uh what did I call it simple or run Behavior or something AC run behavior and then just add that in um we can get this to do all the scripting just by itself so inside of AC run Behavior on begin play what we're going to do is we're going to get the owner of this component which is my symbol character um we're going to get AI controller okay and then we're going to say run Behavior tree and now we can promote this to a variable by dragging off his ear and just call this um BT I guess and we'll make that we'll take the eye on that so that's a public variable and that can be edited and then basically any actor that has this component will just run a behavior tree straight away off the bat so if I put in as the default for the BT um our random roaming Behavior this is done we can close that component and now this guy here at begin play is going to run that behavior tree now the other guy we can get him to run it as well and we don't need to copy code all we need to do let's add in this and they're both going to run the same behavior true um I prefer to do it this way rather than copy and code in case you want to add more complex logic to which behavior tree they should be running um so now if I let's delete our second guy but if I hit play now it looks exactly the same as before but if I open up the behavior tree you can see um there is an option up here somewhere you can select the AI from the drop down AIC simple brain C underscore zero this is just saying it's running this Behavior tree is running on this brain okay and this brain is inside of this guy here if you want to think about it like that um so he's successfully running a behavior tree already um cool right like really really simple um so now if you wanted to get him to do something more complicated what we do is we use these things up here which is called a task um so maybe what we can do we can use a sequence node and the reason that I'm adding this over to the left is because this is how the decision making Works in Behavior trees So This Is A New Concept that I'm about to teach right now execution starts at the root and you can think of it like water which chooses which way it wants to flow down and this whole the whole system of behavior trees is designed to choose which behavior should be running given a set of circumstances or input it's like if you think of the way that your brain runs Its Behavior true it's like if you're about to cross a road and you use your perception and see that there's a car coming and you don't want to die you will block off the behavior that says cross the road and you'll probably just wait until the cars the car's done crossing the road and then you'll uh the car's done passing and then you'll cross the road so it's the same thing with behavior trees um you can have different Gates on these so for example if I had a a Blackboard on here this checks a condition to see oh well is there a car coming if there is then this whole branch is going to fail we're not going to cross the road instead we're going to do this Branch over here and the way that this works the way that the behavior tree selects which node to run is it always checks from left to right so if we go down to the selector node the first one that it'll try to select is on the left here and then if this fails it'll go to the next one check that next one check that next one and it'll keep going until it succeeds so what you always want is a weight over here on the right because if everything else fails then you just end up waiting for a second or five seconds or whatever okay simple enough so let's um let's see if we can build our first Behavior which could literally just be um oops I just clicked new task which I didn't really mean to but hey let's just roll with that um straight away what I like to do is go Ctrl shift s to save it because it had a little asterisk and then I go Ctrl B and it browses to where that task was created so I'm going to call this BTT hitting F2 to rename it BTT um get random location near actor this is a task that a brain can run Okay so you want to keep that in mind when you're naming things so let's just before I add code to anything let's look at how this might look so this note up here was the select node which selects the first node it can starting from the left and then moving progressively to the right a sequence node runs all of its nodes in sequence and you can hover over these if you want to read them so if we think about what we want to happen the first thing that we want to do is if we're going to be building a random roaming Behavior which is what we're going to be doing by the way spoiler alert I don't know if I mentioned that the first thing that we'll do is we'll get a random location near actor and that act is going to be ourself because we're just going to be roaming around our immediate location like sort of simple stuff um we'll do more complicated ones in future videos perhaps get a random location move to location which already comes inside of the editor that's a default one and then maybe what we'll do is we'll wait there for two seconds plus or minus one second okay and then once that's finished um this whole thing is then succeeded this whole sequence has been succeeded which means this selector note is then succeeded which means it's done the Root's done and then the root will fire again and it'll do the same thing again If This Were to fail um the select node would then cut that branch and it would go to the next thing on the right which is the weight and after it waits for one second that task succeeds this succeeds the route succeeds and then the root fires again and tries again so it'll try from the left and then go again to the right so that's how Behavior trees work now this is not going to roam at the moment because it doesn't actually have any logic inside of these inside of this task which is getting a random location near actor so if we double click that we can open that up now first thing I type in Rec X AI event receive execute AI which just means this task has been fired slash activated slash whatever um the controlled Pawn is our character which is possessed by the AI controller that's this and then the owner controller is the AI controller simple brain okay if we want to get a random location near the control Pawn we get a reference to the control pawn and we can say get location get active location and then we could get random point get random reachable point in radius say there's already a node that does it so the origin will be where we are and the radius can be a public variable so we'll drag that off and promote and just call that radius literally nice and simple and then make that public by clicking the I so we can edit it now once we've got that we can finish execute so the thing about these tasks is that you need to actually mark them as a success otherwise um you need to mark them as a success inside of the task itself otherwise the behavior tree has no way to tell if this actually succeeded or if it failed um because sometimes for example if you can't find a location you might want this to fail because you don't want to try and then continue in the sequence to move to a location that it couldn't find right so if this fails um the whole sequence will fail and then the selectable default to the next task on the right so that's something to keep in mind when you're doing these tasks is you need to remember to finish execute after you've done your script and then have different conditions for Success versus fail because then that can control how the behavior tree Works its magic um so in our case as soon as you get the location um I mean it's it's we're just going to assume that it's always going to succeed um because it probably will I think it will so then what you do is you just go finish six finish execute success now the values here but we don't actually we can't do anything with it from inside of this it's like okay well we've got the random location but like how do you get that from here inside of this task into this move to node to tell the brain or the the software on the brain to move to that actual location and the way that you do that is with a thing called a Blackboard so let me create a Blackboard and then I'll explain what that does so if you right click in your content browser and create a Blackboard and then call this BB underscore um this could be called basic Blackboard BB for Blackboard basic Blackboard and what this is is well if you think about what a Blackboard is what do you do with a Blackboard it's like you write [ __ ] on it and then it like stays on there in chalk or whatever um and I suppose you can rub stuff out and edit it and like whatever it's basically a storage unit for information um so a Blackboard when it comes for AI is it's a thing that is always paired to a behavior tree which allows the behavior tree to write information onto a Blackboard like you will with a piece of chalk and then use that information and remember that information to change the way that it makes decisions so in order to tell this AI where to move to we need to record the move to location from this task onto a Blackboard and then draw that information from the Blackboard in this move to node that's how this works so but before we can even do that we need to pair the behavior tree to the Blackboard um which I don't remember how to do it so let me see if I can figure that out if we go to our BT random roaming um you can see there's a if you just click on the event graph left click on the event graph you get AI Behavior tree Blackboard asset and then you click that now it's got a Blackboard okay cool and you can see it just appeared up there because this root is reading this Blackboard data so now if we go over to the Blackboard you can toggle between the two just like this so you can see here's the behavior tree here's where it's storing its information which means I can close this Blackboard asset now and I won't need to open that Blackboard asset again because you can just open it from here so if we open up our Blackboard we need to add a thing called a key which is a different variable type where you can store information that can then be used inside of the behavior tree so if we click Blackboard UK we need a vector because we're storing a location so let's just call this move to okay it's a vector and it's just called move two and the cell Factor will just that's a default one and that'll always just be a reference to whichever actor owns the Blackboard okay so we have a move to Vector on the Blackboard so how do we write to the Blackboard well let's go into our get random location and off of here the random location we're going to say set Blackboard value as vector and then just put that in the execution chain now which Blackboard value are we setting well we're going to create that we're going to make that a variable um and it defaults to the name key but what I like to do is I call it bbki underscore um sorry bbko I'll explain in a moment uh move to location or maybe I'll just call it location output and look the reason I call it location output is because the name of this task is get random location near active so if you read the name location output automatically you're going to make the connection okay well that's the location that's outputted from this thing okay now bbko means Blackboard key output bbki means Blackboard key input I like to differentiate between those two because sometimes uh let me just hit this eyeball here so it's editable sometimes if you have a bunch of different keys you can view all those defaults by clicking on your task up here you might have like five different keys and if your naming isn't clear you won't be sure which ones are meant to be variables that you're putting in and which ones are meant to be variables that you're getting out and then using in another part of the graph um so I use this convention bbko for out bbkife in and then just a descriptive name afterwards so now if you look at this event receive execute we get a random location near our radius which we're just going to set as a default to 500. 500 is a good no let's go big on 1000. let's move real far 1500. and then we're updating our Blackboard value as a vector right we're feeding it a vector and our Blackboard key is the location output and then we're marking it as a success okay so this is done we can close that down now but now what we need to do is we need to set this up so the radius is fine because we're going to be moving in a radius of 1500 but we can make that bigger we can make that smaller it's completely editable depending on what type of behavior you want it to run now the location output this is where it's right into the Blackboard because I've named it bbko if this BBK orbit was covered and it just said location output or even just like location or something you wouldn't be clear on what this did and you wouldn't be sure if it was something you were meant to set or something you wanted to get um but anyway so bbko location output we're going to Output that to the move to location on our Blackboard and then we're going to use in our move to node down here in Blackboard key I'll move to location so in sequence what's happening from left to right we're getting a random location here ourselves we're moving to that location and then we're waiting for that amount of time this all marks as a success the selector marks as a success the root continues and then it goes again so it'll go back through back to the sequence get a new location move to wait get a new location move to wait and it'll just repeat and repeat and repeat and repeat and repeat okay simple Blackboard Behavior tree um shall we give it a run and see if it actually works or if I'm full of [ __ ] hit simulate um do we have another mesh in here it doesn't look like we have a nav mesh is that something that I forgot to do okay so before AI can move in here um you need to go Place actors and then type in nav mesh bounce volume drop that in I'm going to center it to zero zero zero just down here in the bottom right and then uh I'm gonna make that a little bit higher so you can see it and then spread that across the whole thing and you just press the P key to toggle the nav on and off so you can see it but now what's going on here so okay you can see that he doesn't have navigation around his body because the cube is blocking the navigation so if you go to the static mesh and then scroll down to Collision presets set that to character mesh because it is it's a character mesh even though it's a cube um and now that'll stop blocking the navigation so you can see he's got green under him again so now I'll press P again to hide that and hit play we got our boy moving around now or girl or whatever the hell he is it is this horrible thing that we've created we've given a cube sentience it's just gonna roam around in this cube world for the rest of its existence you don't know what those things get up to in here when you turn the computer off you don't want to know trust me it's no good but to be honest maybe he's happy here I mean he's surrounded by cubes like there's cubes on the stairs there's cubes there's walls everything's made out of out of edges you know and this guy has how many edges eight edges 16 edges I don't know but he's got some edges um and you can see over here on the right now so this is the cool thing about Behavior trees is you can literally visualize I can't I can't move that while it's running but I was going to drag that across so you could see it a little better but on the left you can see moving around and on the right you can see what he's thinking that's literally what he's thinking the behavior tree is like the thought process of an AI so you can see he's running the sequence note he's waiting he just got his location instantly he moved to it now he's waiting now he's moving now he's waiting right so simple so that's how you think of it this thing on the right is his brain this is his this is his thought process how he makes his decisions and then you can see what that looks like on the left of the screen I'm over here um so I think I might leave that there this is a basic introduction to behavior trees I do have more advanced Behavior tree content on my channel um I have an AI Made Easy series which I go into quite advanced stuff to be honest um this is a very beginner tutorial to just get you started with behavior trees and see that they're not something to be afraid of like they're they're really really simple um and they're incredibly powerful like once you get the hang of what you're doing um and you start to build up a lot of these the tasks if you've designed these properly you can migrate these from Project to project to project and basically build up your own library of different AI behaviors which you can reuse to every AI that you ever create from that point onwards it's like once you design a task um you know you just copy it to a new project and you can reuse it so you end up being able to do really good complex Behavior tricks and let me just show you one more thing um just for the sake of like demonstrating the power of this this is our random roaming Behavior tree um let's create a new Behavior true so AI BT BT underscore move to random point near self that's basically the behavior that he's doing right this whole thing he's basically moving to a random point in yourself and then he's waiting so if I wanted I could cut that open up that new Behavior tree that I just created copy that in boom just like that because that's that's what it's doing now in here I can say run Behavior and then in Behavior I said um move to random point in yourself just like that and then if that fails then we wait so this is how you collapse your behavior tree graphs so basically you could have a big ass Behavior tree of itself inside of here which could be a very specific type of like combat for example or an invasive Behavior or a searching behavior and then you can have like 10 different ones of these and then have like a meta decision making process between which behavior to use um so just just so you know that you can collapse these and just make the most complicated [ __ ] inside just a single mode like that and then have a monstrosity Behavior tree selecting between different behaviors of their own so this guy is really the limit right with the kind of [ __ ] that you can do with this so just to demonstrate that running you can see he's just running that one behavior and it'll always succeed because I mean there's no point where he's ever gonna fail it um when he might fail it is if you introduced a condition where he would only Roam if the player was X distance away or whatever but you can look into my um AI makes Made Easy series if you want to learn a little bit more about that sort of stuff but um let me know if this is helpful like slowing this down like this um and if you would like more tutorials like this because if you do uh I will but you need to let me know because otherwise I don't know if anybody's watching them I feel like a lot of the the so-called views on YouTube are literally just like Bots and like not even real people um and so it's hard for me as a career to sometimes to unders to to know or to gauge if there's actually interest in something or if I'm just putting sinking all this time into videos that just go out into the [ __ ] void and don't even reach anybody they're just reaching fake computers so um yeah if you would like to see more beginner AI tutorials like this or beginner tutorials of Unreal Engine at all um let me know in the comments right now all right I hope that was helpful and if you have any questions drop them below as well alright bye-bye until next time
Info
Channel: Slightly Esoteric Game Development
Views: 21,766
Rating: undefined out of 5
Keywords: ue4, unreal engine, game development, indie games, game dev, epic games, artificial intelligence, ue5, philosophy, metaverse, technology, artist, creative, spirituality, tutorial
Id: cV5jeISRVqM
Channel Id: undefined
Length: 33min 26sec (2006 seconds)
Published: Sun Dec 11 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.