WEBVTT

00:06.560 --> 00:13.550
In this lecture, we're going to continue writing some code that will allow our agents to execute a

00:13.550 --> 00:14.060
plan.

00:14.060 --> 00:17.350
And then we're going to put a simple plan onto our agent.

00:17.360 --> 00:22.820
And before you start, if you want exactly the same project file that I've just started with here,

00:22.820 --> 00:26.490
it's attached to this lecture as a starter project.

00:26.510 --> 00:31.640
Now, to do this, we're going to go into our G agent script.

00:31.640 --> 00:32.600
So open that up.

00:32.600 --> 00:40.640
Now, if you remember from earlier on, there was a late update and we didn't actually fill this out.

00:40.640 --> 00:43.610
So now is the time to add the code.

00:43.610 --> 00:49.520
Okay, so the first thing we're going to do in here is add in the planner.

00:49.790 --> 00:53.210
So I'm just going to come down a little bit because I want to come back and put other things above this

00:53.210 --> 00:54.140
a little later on.

00:54.140 --> 01:05.390
First of all, we're going to say if the planner is equal to null or our action cue, which gets returned

01:05.390 --> 01:13.850
from the planner equals null, that means that the agent currently has no plans to work on.

01:13.850 --> 01:15.890
So what are we going to do in here?

01:15.890 --> 01:18.200
Well, we're going to create a new plan.

01:18.200 --> 01:19.820
So we create a new planner.

01:19.820 --> 01:23.660
First of all, so planner equals new G planner.

01:25.340 --> 01:26.270
Like that.

01:26.600 --> 01:32.550
And then we're going to sort through all of the goals that we have now.

01:32.570 --> 01:41.780
Do you remember when we created our agent and we allowed it to have multiple sub goals?

01:42.200 --> 01:49.100
So if we go back up the top here, a goal is not just a single goal, but there's multiple goals.

01:49.100 --> 01:57.320
And then we also have the name of the goal and we're storing with it a integer value, which is going

01:57.320 --> 02:01.960
to be how important this particular goal is in order to satisfy.

02:01.970 --> 02:09.350
So we're going to sort through all of our goals and put them in order from most important to least important,

02:09.350 --> 02:13.880
so that we can then loop through them one at a time until we find one that we can do.

02:14.030 --> 02:23.150
So I'm going to do a var sorted goals, which is going to equal our sorted dictionary, which will be

02:23.150 --> 02:25.670
using a link command.

02:25.670 --> 02:35.900
So it's going to be from entry in goals, order by entry value descending.

02:37.600 --> 02:39.940
Select entry.

02:40.600 --> 02:40.960
Okay.

02:41.110 --> 02:43.030
And that's going to take our goals.

02:43.030 --> 02:43.750
All right.

02:44.020 --> 02:48.100
And order it and stick it into sorted goals.

02:48.260 --> 02:48.520
Okay.

02:48.520 --> 02:52.180
Now, once we've got that, we're actually going to loop through each one at a time.

02:52.180 --> 02:57.940
So for each, we want the key value pair of sub goal.

02:59.140 --> 03:00.820
And int.

03:00.820 --> 03:05.740
So this is sub goal in sorted goals.

03:07.570 --> 03:10.390
Then in here we will say that the action cue.

03:11.570 --> 03:14.570
Equals the planner dot plan.

03:14.570 --> 03:16.340
So this is where we're calling plan.

03:16.340 --> 03:22.940
So we're taking the most important goal that we know about and trying to create a plan for it.

03:22.940 --> 03:29.060
So plan a plan needs the set of actions that we've got the goal.

03:29.060 --> 03:34.370
So we've got that's the sub goal, key goals.

03:35.780 --> 03:38.930
So that's all of those sub goals that make up that goal.

03:38.930 --> 03:46.490
And then we need to pass through the state, not the world states, but this is the beliefs of the agent

03:46.490 --> 03:48.950
and we actually haven't put those in yet.

03:48.950 --> 03:51.950
So at this time we're just going to pass through null.

03:52.820 --> 03:59.240
Okay, so we're not using the belief system, it's just going to use those world goals we've got.

03:59.240 --> 04:01.730
So then we go if action.

04:01.730 --> 04:08.930
Q After we've done this is not equal to null, then we must have a plan that we can follow.

04:09.260 --> 04:16.910
And in that case, we're going to set the current goal that we're working on to equal the s g, the

04:16.910 --> 04:20.030
sub goal, key value.

04:20.120 --> 04:26.660
And then we're going to break out because we don't need to keep looking through those goals for another

04:26.660 --> 04:28.730
plan because we've already got one.

04:28.730 --> 04:32.000
So that is basically our plan.

04:32.000 --> 04:36.260
And now remember late update, it's a loop, it's going to loop and loop and loop.

04:36.260 --> 04:36.540
Okay.

04:36.620 --> 04:44.270
So when it loops the first time and we don't have a plan or an execute, then we will get a planner.

04:44.420 --> 04:51.380
Now if we do have a action, then just above this, I want to test for it first.

04:51.380 --> 04:56.150
So if we're in the middle of performing an action, then I'm going to return out of this late update

04:56.150 --> 04:58.940
before we get down into the planning stage.

04:59.060 --> 05:10.430
So up here we'll go if current action does not equal null and current action dot running.

05:10.610 --> 05:15.560
So there's a bool that we can set to say that we're actually working on our action.

05:15.560 --> 05:22.610
Now, if we are working on our action, what we're going to do is put if current action.

05:23.550 --> 05:24.990
Dot agent.

05:25.760 --> 05:30.890
Dot has path and current.

05:31.600 --> 05:32.350
Action.

05:32.710 --> 05:38.970
Agent remaining distance is less than one f.

05:38.980 --> 05:44.470
Now, this, as you will recognize, is nav mesh code.

05:44.650 --> 05:50.590
And so we need to have this in here in order to move the agent towards the goal.

05:50.860 --> 05:59.890
Now, as soon as the agent has a goal and it has actually reached that goal, and we're saying in this

05:59.890 --> 06:08.440
case, like less than a distance of one, then we're going to run the end of our action, which is to

06:08.650 --> 06:11.650
run basically the completion of it.

06:11.800 --> 06:12.070
Okay.

06:12.070 --> 06:16.930
Now remember that we put in each action a duration.

06:17.200 --> 06:23.410
So when we've got to this point, we've reached our goal location, we then need to wait for the duration

06:23.410 --> 06:23.680
of it.

06:23.680 --> 06:29.650
So let's say that you've rocked up to the registry desk and you're registering, so you've arrived there

06:29.650 --> 06:36.430
physically, and then it's going to take 10 seconds to actually complete the task that the agent is

06:36.430 --> 06:36.990
doing there.

06:37.000 --> 06:38.860
This is the code that will do that.

06:38.860 --> 06:48.790
So we're going to say, if not invoked and I'll explain this in a moment and if not invoked, then we're

06:48.790 --> 06:55.900
going to invoke an external method called complete.

06:57.270 --> 06:58.230
Action.

06:58.960 --> 07:06.790
And that's going to be sent through the current action short duration.

07:07.840 --> 07:13.270
Well, actually, it's going to be invoked in that 10 seconds later on and then invoke.

07:14.760 --> 07:17.430
Equals true.

07:17.700 --> 07:22.860
Now when we start invoked is going to be false and I'm just going to put it up here.

07:23.190 --> 07:27.450
So bool invoked and equals false.

07:27.450 --> 07:31.150
So we know that the first time we come in here, this will be false.

07:31.170 --> 07:40.580
It'll allow us to go in here, invoke this complete action method in however many seconds, and then

07:40.580 --> 07:42.030
it sets invoked to true.

07:42.030 --> 07:46.530
So we're never going to come back into here again at this point.

07:46.530 --> 07:48.270
We can't it won't let us.

07:48.270 --> 07:56.280
But the complete action method that we're going to right just up here is going to set invoked back to

07:56.280 --> 07:57.120
false for us.

07:57.120 --> 08:02.130
So we want void, complete action.

08:03.510 --> 08:11.690
And then inside of this method, current action running equals false.

08:11.700 --> 08:18.060
So this is where we finally say we're finished running our current action and it will force our planning

08:18.060 --> 08:20.130
system to go on plan again.

08:20.430 --> 08:26.340
And so we want current action post perform.

08:27.630 --> 08:34.260
Which is the method that we run right at the end and then we'll set invoked back to false.

08:35.830 --> 08:36.370
Okay.

08:36.370 --> 08:44.350
So that's what happens when we finished a current action and then after how many duration it is we're

08:44.350 --> 08:48.580
going to have running set to false, which means that this will then fail.

08:49.180 --> 08:55.090
Excuse me for one moment while I bought into my own video just to say that I've missed a return statement

08:55.090 --> 08:58.000
that you'll need to put inside of that if statement.

08:58.630 --> 09:05.920
Now, at that point, you've either run out of actions or you still have more actions in your queue.

09:05.920 --> 09:08.890
So if that's the case, we now need to consider that.

09:08.890 --> 09:18.550
So underneath our if statement for our planner, we're going to consider if action Q does not equal

09:18.550 --> 09:18.970
null.

09:18.970 --> 09:30.460
So we actually have an action queue and the action queue dot count equals zero, which means that we

09:30.460 --> 09:35.380
have an empty action queue and we've actually run out of things to do.

09:35.380 --> 09:39.610
Then we can go if current goal remove.

09:39.610 --> 09:46.750
So if the goal that we've just been working on is one of those removable ones, then we can say goals,

09:47.230 --> 09:57.370
remove our current goal from there and then we're going to after we've done that set planner to equal

09:57.370 --> 10:03.370
null, which is going to trigger getting another plan and the next time this loop goes through.

10:03.670 --> 10:12.820
Now finally, there's one more if statement condition we need to consider in here that might occur and

10:12.820 --> 10:16.600
that is if action queue.

10:17.800 --> 10:19.230
Does not equal null.

10:20.240 --> 10:22.880
And the action.

10:23.090 --> 10:26.660
Cue dot count is greater than zero.

10:26.660 --> 10:32.980
It means we still got actions left sitting in our queue that came back from the planner.

10:32.990 --> 10:35.450
In that case, we can start running them.

10:35.450 --> 10:41.290
So we're now going to get current action and we're going to go equals action.

10:41.300 --> 10:42.080
Q.

10:42.920 --> 10:43.870
Dot D.

10:43.880 --> 10:50.390
Q And this will take the action that's sitting at the top of our queue off the queue and put it into

10:50.390 --> 10:51.500
current action.

10:51.530 --> 10:57.530
Now we then perform the pre performance method that we've got for our action.

10:57.530 --> 11:02.600
So it's going to be current action dot pre perform.

11:03.860 --> 11:09.140
And in there we can do any checks that we need to do, like for physical attributes that need to be

11:09.140 --> 11:14.060
said if the number of, say, cubicles is available and all of those sorts of things.

11:14.270 --> 11:24.800
And then if that is true, we want to go if current action target equals null.

11:25.990 --> 11:34.870
And current action target tag does not equal an empty string.

11:35.110 --> 11:48.430
Then what we're going to do is go that current action dot target equals game object dot find with tag

11:48.730 --> 11:51.640
current action dot.

11:52.370 --> 12:05.060
Target tag so we can set the target for the nav mesh agent to move to by setting it in the inspector.

12:05.570 --> 12:08.390
Or we can find it using a tag.

12:08.390 --> 12:14.330
So if you've made a prefab of your character, it's going to be much easier for you just to set the

12:14.330 --> 12:14.720
tag.

12:14.720 --> 12:19.330
So then it does this search when it gets into the environment to look for it.

12:19.340 --> 12:23.390
Now, as you can see, I'm getting errors with our target tag here.

12:23.660 --> 12:30.260
If I just quickly save that and switch back into my G action and you might want to have a look here,

12:30.260 --> 12:37.670
you can see that we set target tag to be a game object when in fact it's meant to be a string.

12:37.670 --> 12:41.780
So that was just a little mistake I made before and will still be in your code.

12:41.780 --> 12:46.520
So just save that and then we can switch back to our agent.

12:46.790 --> 12:53.450
The other thing you can see here is that the game objects to find with tag needs to be a capital G in

12:53.450 --> 12:53.930
there.

12:54.050 --> 12:55.550
Now let's finish this.

12:55.550 --> 13:00.410
If statement we've got our target object, then we're going to go.

13:00.410 --> 13:06.800
If current action target does not equal null.

13:06.800 --> 13:14.360
So it still could be that we end up having nowhere to go as far as moving our agent around.

13:14.360 --> 13:20.180
And if that's the case, we're going to check that it's not equal to null.

13:20.180 --> 13:30.920
So if it's not equal to null, the target, not the tag, I should say current action dot running equals

13:30.920 --> 13:31.340
true.

13:31.340 --> 13:35.240
And this is where the action actually starts to take place.

13:35.330 --> 13:46.310
Let's get rid of that T and current action dot agent dot set destination and that's our nav mesh agent

13:46.310 --> 13:49.910
setting the destination to the current.

13:51.580 --> 13:52.510
Action.

13:53.290 --> 13:57.340
Dot target, dot transform.

13:57.970 --> 14:00.160
Dot position.

14:00.940 --> 14:01.420
Okay.

14:01.420 --> 14:09.460
So that is the starting point for setting the destination for our agent.

14:09.460 --> 14:14.320
And then up higher is where we're actually testing if we've made it to the destination.

14:14.320 --> 14:20.080
Now remember the nav mesh agent code is going to go and do its thing automatically in the background

14:20.080 --> 14:22.720
as far as moving the agent around.

14:22.750 --> 14:23.200
Okay.

14:23.200 --> 14:25.890
So we're just going to end that statement now.

14:25.900 --> 14:34.540
Else if we can't do our Pre-performance method, we're going to say action queue.

14:35.850 --> 14:37.290
Equals null.

14:37.770 --> 14:43.170
And that will then force us to get a new plan.

14:43.200 --> 14:44.310
Go back to the goals.

14:44.310 --> 14:45.160
Get a new plan.

14:45.180 --> 14:53.010
Now, if any one of your actions that you're working through or trying to work through fails, then

14:53.010 --> 14:58.890
it's going to replan the whole thing, which may in fact re plan the exact same sequence of actions,

14:58.890 --> 15:04.110
but send you back to the beginning so that it makes sure that you do do that.

15:04.110 --> 15:09.480
And it also makes sure that your agent doesn't get stuck somewhere through the middle of a plan.

15:09.960 --> 15:14.220
Okay, so now that we've got that, let's just save it.

15:14.250 --> 15:20.130
That is our main code for our late update.

15:20.730 --> 15:21.180
Right.

15:21.180 --> 15:23.130
Let's go back into unity.

15:23.130 --> 15:28.040
We're now going to create our basic class for our patient.

15:28.050 --> 15:32.690
So right click and create a new C-sharp script.

15:32.700 --> 15:36.960
We're going to call this patient and then open this up.

15:36.960 --> 15:38.940
It's going to be very short code.

15:38.940 --> 15:42.720
So the patient is going to inherit from G.

15:42.720 --> 15:46.380
Agent and it's not going to have an update.

15:46.380 --> 15:48.240
It's going to have a start.

15:48.240 --> 15:56.730
Now in the start we want to call the base start that's going on in the G agent script and then we're

15:56.730 --> 15:57.900
going to create a sub goal.

15:57.900 --> 16:09.060
So sub goal S one equals new sub goal and the sub goal will be is waiting and we'll give it a priority

16:09.060 --> 16:16.800
of one and we will say true, which will remove it from being a goal and then we have to add it to our

16:16.800 --> 16:17.250
goals.

16:17.250 --> 16:19.470
So goals add.

16:20.480 --> 16:26.360
S one and it's priority in this case is three.

16:26.540 --> 16:33.680
Now this particular start and our G agent, let's just save this let's go back to g agents want to point

16:33.680 --> 16:41.450
out something that you need to fix in here that I just did before is to add a public to the start,

16:41.450 --> 16:46.430
otherwise that you're going to get an error when you try and call that base start.

16:46.430 --> 16:51.230
But basically base dot starts going to set up all your actions that you've attached to your patient,

16:51.230 --> 16:56.000
and then you can go ahead and run the rest of this patient code, right?

16:56.000 --> 17:03.110
So we're going to save that, go back into unity and grab hold of that patient script and drag and drop

17:03.110 --> 17:05.390
it onto your patient.

17:05.660 --> 17:06.020
Yep.

17:06.020 --> 17:07.280
And there it is.

17:07.280 --> 17:13.310
Now we actually need an action to follow, so let's make a quick action.

17:13.310 --> 17:18.020
And in here, let's go create C-sharp and let's call this.

17:18.020 --> 17:20.120
Go to hospital.

17:20.390 --> 17:22.490
Open that up to edit it.

17:22.880 --> 17:25.880
And this time, it's not going to be a mono behavior.

17:25.880 --> 17:28.340
This is actually going to be a g action.

17:28.850 --> 17:33.050
And inside of here, we're going to get rid of our start and update.

17:33.050 --> 17:44.330
And all we need to do is put a public override bool for our pre perform which we will just put return

17:44.330 --> 17:52.880
true in there and then a copy of that down here for our post perform.

17:54.250 --> 17:58.660
So that is a basic stock standard action.

17:58.660 --> 17:59.800
So just save that.

17:59.800 --> 18:01.480
Now switch back to unity.

18:01.480 --> 18:09.610
We're going to attach this gold hospital to our patient, which will give it access to be able to perform

18:09.610 --> 18:12.210
that particular action.

18:12.220 --> 18:17.530
Now this action, remember, we've got pre conditions and we've got post conditions that we can put

18:17.530 --> 18:18.310
in here.

18:18.310 --> 18:22.420
Let's say that just for now, there's no precondition.

18:22.930 --> 18:28.000
The after effect is going to have to match whatever our goal is.

18:28.000 --> 18:34.870
Now, the goal for our patient, if we go back into the script here is looking for is waiting.

18:35.470 --> 18:40.720
Okay so back in unity on our patient we're going to add one.

18:41.640 --> 18:47.890
Aftereffect and it's going to be is waiting and this is where it needs to be spelt correctly.

18:47.910 --> 18:51.990
Now, the value in this case doesn't matter because we're not really using it for anything.

18:52.350 --> 18:58.290
The other thing we need to know is where the target for this action is going to occur now, because

18:58.290 --> 19:06.180
the agent is in the scene, in the hierarchy with, let's say we're going to go to the hospital in the

19:06.180 --> 19:10.340
waiting area, so get the waiting area and drag and drop that into your target.

19:10.350 --> 19:11.400
It's going to be okay.

19:11.400 --> 19:16.560
It's not a prefab, so we don't actually have to look for it using the tag of the waiting area.

19:16.920 --> 19:17.370
Okay.

19:17.370 --> 19:27.300
So now if we press play, our agent is going to formulate a plan to make sure that is waiting goal occurs.

19:27.300 --> 19:33.930
And as you can see, it has done that because all it needed to do was run the go to hospital script

19:33.930 --> 19:36.480
in order to satisfy its goal.

19:36.900 --> 19:38.190
So there we go.

19:38.190 --> 19:46.050
There's our very, very basic, very, very simple plan system working in our rope system that we've

19:46.050 --> 19:47.160
just put together.

19:47.970 --> 19:52.260
So now onwards and upwards to more complex actions.
