Unreal Engine 4 Tutorial - AI - Part 1 The Behaviour Tree

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hi everyone and welcome to a brand new series in my ue4 tutorials this is all about a iron creating a ia to do also some manner of things so in this first episode we're going to set up AI and explain how it works and hopefully get something moving around on its own so we're gonna build up on our a I over each week as we release these videos hopefully get into something quite advanced by end of it so to begin with we're going to create a simple NPC that will be wandering around this whole entire map so this is going to be a little demo talking about how we use behavior trees the blackboard I our controllers and also nav meshes to create this effect so in my content barrels on I'm gonna make a new folder and I'm going to call it AI and I make another folder in there in PC so this would be my friendly NPC and this guy is going to have a new blueprint class and it'll be a character talk so I'm going to walk around so character so go NPC and we can open this up so one be able to see what I'm doing here so we're gonna click on the mesh component on the left hand side and choose a skeletal component a skeletal mesh from the list I'm using a third person template therefore I only have the mannequin and I'm going to choose the animation class of the third person and MBP going to compile and we're done here closest so we need several things here we first we'll need a AI controller so click on add new blueprint class and you'll see player control in a list here but not a i/o controller what controller is is basically like the soul and brains of persistable actors and pawns okay so if a character wants to be moved it needs a controller to take to move now the player controller is one that has been by a human player and I controller is when you leave the computer to move to October porn around so it's not in this list here but if you click on this little expansion down bottom here you can type in IR controller and see you there click on it and choose select and you will name this NPC underscore AI say our controller is one component ok that's just going to be the thing that controls the movement of this NPC but in order for us to make it a bit more clever and a bit more intelligent we're going to use a behavior tree so I'm not going to explain it just yet we'll get to that when we have a look it so what we do is click on add new and see artificial intelligence you'll see behavior tree choose that and go NPC go BTW behavior tree and we're also going to need the other option which was in there and that's a blackboard NPC blackboard BB so you should have these four things of the mesh the IR controller the behavior tree and the blackboard which work in tandem to make decisions be made so let's link Willy's things up so the NPC needs to be told to use the i/o controller so click on the NPC self topic component on the left hand side click on that on the right hand side you'll see IRI controller class and you can choose our from the drop-down to use the NPC AI the one you just made click compile and close that guy down then on the NPC AI we're gonna go to this event graph and on begin play we are going to have a run behavior tree node and in here we need to choose from the drop-down out behavior tree and then we actually compile and that's it that's that then so there I our controller could tie to the NPC mesh and heavy tree is tied to the AI controller open up their behavior tree and you should automatically have the blackboard asset assigned to it so it says MP CBB if you have marble we just choose which one you're using from the list so this is the behavior tree the way the behavior tree works is very different from anything else you've seen so far if you watching my videos anyway so you see here we've got a root here if details panel on the right hand side and some blackboard information down here blackboards empty stuff there for the bomb here is empty - and barratry just has the root node available to us so they can't get rid of the root node it must have a root okay you can't get rid of it the blackboard we can access if I click on the blackboard on top right so click on there you can see it's all empty nothing here at all go back to be how your tree so a behavior tree the way it works is it's a decision-making tree so if we come off the bottom here yes arrow come out and we can drag that off and choose a selector or sequence I'm going to choose a sequence and from there I'm going to choose a so not sequence this get rid of that let's put a selector in first ok selector and I'm putting another selector in just to demonstrate how these work I'll say oh all going wrong set aside forma Slater their sequence there we go and in another sequence coming off that and there we go because the root can only have one output coming out the selector can have multiple because it will only choose one or the other okay so won't do both at same time it won't do one or the other root can only do one thing and that's one down the line a sequence will play whatever is it attached to it sequentially so keep going but down then down the branches and till it fouls when it fails it goes back up okay and you can see these little numbers here 0 1 & 2 these refer to the ordering and priority so 0 is top priority so it goes to their first always then you always go down to the left hand side so 1 and then it goes to 2 so if I want to rearrange these I want this one happened in the second place or do is move this to the left hand side and you can see that one is now still here okay so I switched so it's important to note trees always go down the left-hand side first and foremost so on our sequence we're going to come out of here and we're gonna do tasks so the tasks we're going to do are gonna vary between built-in ones and our own custom ones but before we can do any of that let's save what we've got here make sure you're going to save the blackboard to and close us down because our NPS is not in the world and we also need either navmesh for it to guide itself around so drag UMPC into world like so and then now we want a nav mesh so the nav mesh you can search for in the modes a place mode here nav and you'll see nav mesh bounds of volume plug this into the world and you get a volume cube appear and you want this cube to encompass the whole level that you want the empathy to walk around so just scale it out and up and if you hit the P key on your keyboard the green areas will show what is the nav mesh that the AI can use okay so you wait to make sure green is everywhere okay now there are settings through change on the nav mesh that you can do to make it more less accurate to the walls here so it doesn't get big gaps but we're not gonna do this in this episode we'll do it down a line in a future episode so now we've got the empathy and enough mesh into the world we're gonna go back to our NPC behavior tree so I'm gonna get rid of this second sequence for now we'll come back to that later on in a future episode so this first sequence here we're just gonna make the embassy wander about so here we need a new task the first task we're going to set itself is to find a location to move to so if you drag off of here you'll see tasks is open for you now the game engine comes with some already but you'll see there's known here none here to say where to move - you got ones that say move - but nowhere to say - actually where to move the actual character - so we're gonna make our own custom tar for this so at the top you'll see new tasks click on this and you get a new template now it looks very similar because you've got a blueprint in middle and you've got some details on the right-hand side and on the left hand side you've got your variables macros functions or so on and so forth so here a task is the instruction you want to give the AI so this task is going to handle one thing only and that is getting the AI to find a new location to move to so when creating a task there are two things we must have for the task to work the first one is the execute event and we're going to use the event execute ai and this is basically a starting block so as soon as the tree goes down and hits it this will trigger and to end it you need to finish execute and it's important that you have this because if it doesn't ever have this you'll never get out the task okay so you want to make sure you've got these two always so the first thing we need to do gets to work is we need to tell the character in the app NPC so to find a location based on that navmesh so what I'm going to do is right click and type in navigable radius and you'll see get random point in navigable radius and this node find all those points inside that nav data we have now we all go in put anything Internet data because by default we use the one that's there but if you want any particular ones you can store that here and get that put into this so the origin for this is going to be the character's current location so if I say controlled pawn and get the actor location this is going to get the location of the ìiî possessed pawn ok the character so the origin will be this character the radius is how far away do you want the character to look for a point so if this is quite far let's do 1,500 and this is going to spit out a random low so the random location is all well and good we've got it excellent but how do we get it to the other tasks that we needed to get to well this is when the blackboard comes in so the blackboard that allows you to communicate between tasks so it's very very useful very very powerful so the way this works is if we go back to our purgatory click on the blackboard you can see here we're gonna add a key to it new key click on this and we're gonna choose a vector and I'm gonna name it target location a vector is being of course a location X Y Z location if you are clicking on this and then click on here and it isn't working like again nothing come up that usually means you haven't saved it so make sure you save and close your blackboard first and reopen it okay and then it should work so your target location you can see here on the round side has some basic details here we're gonna leave all of it as is just make sure you've got the name of it to watch you what it's been click Save go better behavior tree and you can see now that target locations available on the blackboard down the bottom here now what can do then is tell this task to look for that blackboard key so I'm gonna quite a new variable here and I'm gonna call it vector and make it a type of a blackboard key selector so I sort of choose a random not choose a random key but I'll choose tell it that this is going to refer to a key on the blackboard okay the name doesn't matter you're not going to it does have to match or anything that's not how it works you don't need that names of matching but what you can do now if you drag the vector out and choose get we can now drag from here and topping a get so I set so I set blackboard value adds vector and the value one asset is this random location hook all this up to the finish execute and the receive execute and this is all good to go except for two things we need to do first thing we need to do is tell the finish execute that we were successful to tick the little tick box to say this is a success the next thing we need to do is tone this vector variable this key that is storing this value is public we need it public because we need other tasks to be able to access this blackboard key selector click compile and go back to your behavior tree we can now add that task to it now when you create a new task you'll find a just crates it and distort it as this sort of messy name here I always before I add it to the area tree is rename it so I'm going to close this and rename it it's just one of the big circle and I'm going to call it find random location go back into your behavior tree and then from your sequence we're going to type in find random location now because we made that variable public you can see that pebble here vector dot colon is to target location now if you've got multiple keys in your blackboard you can change what blackboard value vector is pointing to but going to the right-hand side to details and change the vector to whatever key you want we've only got once and if what only ones going to show that's found a random location next one unit eight two is to move to a location so we go move and you'll see move two as a already built-in task so this moves through tasks basically tells the actor to move to a certain location and don't move on until it's reached its target location so you can really see here it's already tying itself moved to the target location again if you've got multiple vectors in your blackboard go to the right side you'll see backward key and you can choose which key you want to use and once it's moved to that location I'm going to take to wait for bit wait now take how long I'm going here and go you have to wait for two seconds if we move to another one click Save and enclose this if I push play now you see the NPC will move to a random location pause move pause you see just randomize is where it goes and it's because the navmesh is what it is it'll figure out cleverly how to get round things and go up things as well today it goes just randomly not really affected by us not doing anything at all it's just running around okay which is fine but it's pretty dumb doesn't do anything so in the next episode what we can do is make it be able to see the player and react to the player so make it look towards and run towards the player too if you've liked this episode and want to see that that very next episodes right now head over to patreon subscribe and support me just like these people have and you can see it right now along with other videos too and thank you so much everyone who have supported me and I'll see you next time bye bye [Music]
Info
Channel: Ryan Laley
Views: 375,723
Rating: undefined out of 5
Keywords: Unreal Engine 4, Unreal, UE4, Tutorial, Guide, How to, AI, Artificial Intelligence, NPC, Enemy, Behaviour Tree, AI Controller, Blackboard, Explained, Broken Down, Task, Random, Navigate, Navmesh, Nav
Id: zNJEvAGiw7w
Channel Id: undefined
Length: 16min 42sec (1002 seconds)
Published: Thu Jan 17 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.