‫Hello and welcome.
‫In this lecture, we're going to be getting our A.I. to be patrolling back and forth between our locations.
‫So will go to my original start location, then I'll go back to his start location, wait a little bit,
‫and then go again.
‫We're going to be achieving this via behavior trees.
‫So let's dive in and see how it's done.
‫So now we haven't made much use of our behavior tree.
‫Let's fix that.
‫We're going to create a behavior that allows us to go from the place where we're starting to the player's
‫location and then go back to our original starting location.
‫Now, the way we're going to do this is using the following.
‫So there is a distinction in behavior trees between things called tasks and things called composite
‫nodes, or in this case, the example of the composite node is a sequence.
‫So let's understand sequences and tasks as they kind of sound.
‫A task is something you do.
‫A sequence is a sequence of such tasks and it will run all of the tasks in order.
‫So if we right click and expand out the tasks here, you can see an example of what could be used as
‫a task.
‫For example, we can have move two, which moves to a location that's moved directly to as well.
‫I'm not entirely sure of the distinction we're going to be using.
‫Move two.
‫You can get it to play an animation such as the scratch your nose animation.
‫You can get them to play a sound like, Hey you, that sort of stuff.
‫You can get it to run another animal behavior if you want to.
‫There's also a weight here which is very useful for adding in some pauses into your behavior.
‫Now what's interesting about these tasks is that they can run for multiple frames, for multiple ticks.
‫So Move two will be running the entire time that you are moving to a location and it'll only finish
‫when you get there.
‫So if we did a move too and we had, we can stick it in a sequence here by just dragging down from the
‫sequence node.
‫Then what you're going to get is, first of all, it's going to try and move to the destination.
‫And then we could add in a second task node which tells it to wait, and then we can add in a third
‫task task node.
‫Notice I'm pulling down from the sequence here and releasing, which allows me to add in a task quite
‫quickly underneath that sequence node.
‫And then I can tell it.
‫I want to move to another location and then I can tell it.
‫I want to wait again for a certain amount of time.
‫Now this is the basics of that moving to the player and then moving back to where we started from.
‫And you can see how the sequence node is working here.
‫Now there's a route node as well.
‫The route node can only have one child node, so don't try and add more things under that.
‫So we've got one sequence node is the route here and that's basically saying that I'm going to run all
‫of these tasks in order until one of them fails and then once we're finished, we can start the process
‫again.
‫So the other thing to note here is that if you click on any one of these nodes, the details over here
‫give us some impact over how that node works.
‫For example, our move too currently takes a blackboard key in the blackboard section of its details
‫pane of self active, but we can change that over to the player location.
‫So that's basically saying this move to Node will now move to our player location.
‫Then we're going to wait 5 seconds by default, but we can change that again to say we might want to
‫wait 2 seconds and you might want to have a random deviation of 1/2 so that basically it will be waiting
‫somewhere between 1/2 and 3 seconds.
‫Then we want to move to a different location, and that's a location that we don't currently have,
‫which is our start location.
‫So we can go ahead and add that.
‫Let's go over to our blackboard, add a new key, add a vector and have that as our start location.
‫And then we can go back over to the behavior tree and see that our move to a node, if you go to the
‫details pane now has the start location as an option and then we might say we want to wait 5 seconds
‫and then the process will start again because we'll have successfully completed our sequence node.
‫So this is how it works.
‫You step down into the sequence node, the sequence node then says, okay, well where am I at?
‫I want to start at the beginning.
‫And you can see this number.
‫Things here are telling us the execution order, we start at zero, which is the sequence node one is
‫the move to then two is a weight three it's moved to four, etc. So we step through doing each task
‫until completion or failure.
‫If we fail, then the sequence node will fail as a whole.
‫None of these are likely to fail, however, so we'll go through the whole thing and then the sequence
‫node will complete and the route will start it again.
‫So we're going to go through in a loop, going to the player location weighting, going back to the
‫start location, waiting for a little bit longer.
‫So that's the power of behavior trees and you can start to nest these deeper.
‫You can have choices in here, you can have decorators, all the stuff that we're going to see in upcoming
‫lectures.
‫But this is the basis of it.
‫A series of instructions that your AI can run and they can take more than one frame to complete.
‫Now what we want to do is actually set this start location to be something meaningful.
‫Now that should be reasonably straightforward.
‫The only thing I probably want to tell you how to do is how do we get hold of the -- that we're in
‫control of?
‫So just.
‫A reminder that if we go over to the controller h here, then we can have a look for the get -- and
‫the get -- method for both player controllers and air controllers allows us to get hold of the --
‫that is being controlled.
‫So you can use this to get the -- being controlled and get its location at begin play in order to
‫set our start location.
‫So here's the challenge to you.
‫Set a start location, get the --'s location as we've just talked a little bit about, set the correct
‫blackboard key, use the player position that we've already used as a template for that.
‫Set up the behavior tree the way I have in the lecture and test it out.
‫See if that behavior gets the desired behavior of the player or the enemy moving to the player's --
‫and then back to its original starting location.
‫Pause the video and have a go.
‫Okay.
‫Welcome back.
‫So hopefully that worked out for you.
‫Let's give this a shot.
‫So I'm going to copy the line for our player location.
‫And instead of setting the player location, we go over to our blackboard, have a look at our start
‫location.
‫We're going to copy that key value in here.
‫And then instead of getting the location of the player --, we're going to do a get -- and then
‫get actor location, which is going to get the -- that we're currently controlling and get its location
‫in the world so we can go ahead and go back to the editor and compile this.
‫And once that's done, hit play and have a look at our --.
‫He comes over, moves towards us.
‫It waits 3 seconds or 2 seconds or 1/2, then goes back to his original start location.
‫Then he waits 5 seconds, and then he comes back over to that original location of the player --.
‫So he's not updating that every frame.
‫That's okay.
‫That's how we designed it at the moment.
‫But you can see the behavior going on.
‫He's patrolling back and forth.
‫So let's have a look at the all of this in action.
‫Have a look at our behavior tree.
‫If we go over to our behavior tree and select the I in question, you can see where he's at.
‫He's moving to he's waiting so many seconds.
‫Then he's moving to his start location.
‫He's waiting again so we can undock this and see both of these things happening at the same time in
‫our game.
‫You can see he's moving to that location.
‫He's waiting now he is back to moving it to his start location.
‫So this is really handy for us to be able to debug, especially when these behaviors get a little bit
‫more complicated.
‫And it also gives you a sense of how unreal is calculating the behavior tree and how that execution
‫flow is working within our behavior tree.
‫Great stuff.
‫In the next lecture, we're going to make these behaviors even more complicated and add some element
‫of decision to whether we choose to follow a player or whether we choose to stay put where we are.
‫I'll see you in that lecture.