WEBVTT

00:06.510 --> 00:06.900
Hi.

00:06.900 --> 00:11.670
Welcome back to our building of this ghost system from scratch.

00:11.670 --> 00:17.790
We're now going to having already created the world and the world states is work on the programming

00:17.790 --> 00:19.140
of the actions.

00:19.140 --> 00:22.740
The first thing we're going to build are some actions.

00:22.740 --> 00:31.950
So let's go into that script and the action itself is going to perform.

00:31.950 --> 00:39.600
The action is going to take care of the not necessarily the running of action, but all of the details

00:39.600 --> 00:41.850
about a particular action.

00:41.850 --> 00:48.430
Now, we're not going to use this g action as itself, okay?

00:48.510 --> 00:51.090
We're actually going to inherit from it.

00:51.090 --> 00:55.500
So we're going to create an abstract class in this case.

00:55.500 --> 01:03.870
Now, before we continue through the rest of the code, we also need to use the unity engine dot I because

01:03.870 --> 01:10.770
we're using a nav mesh and therefore we need to access that and you'll see that a little bit later.

01:10.800 --> 01:11.100
Okay.

01:11.100 --> 01:17.190
So up the top, let's go ahead and declare all of the things that we can set about a particular action.

01:17.190 --> 01:20.100
So these will all be public so that you can set them in.

01:20.100 --> 01:20.850
The Inspector.

01:20.850 --> 01:28.590
First of all, we have an action name and you might just want to set that to action just as a default

01:28.590 --> 01:35.040
there, then a public float for the cost of the action.

01:35.040 --> 01:37.590
We will set that to one by default.

01:37.650 --> 01:47.400
I should mention about this cost is that your planner will find the cheapest plan to work through.

01:47.400 --> 01:52.980
So each action has a particular cost and it's going to try and go with the one that's going to be sort

01:52.980 --> 01:55.530
of a less strain on your agent.

01:55.600 --> 02:03.900
Doesn't mean that you'll necessarily always do the lower cost plans, especially if they're not achievable

02:03.900 --> 02:05.880
and you might have to do another.

02:05.880 --> 02:12.660
But I guess you can think of cost as like does the agent actually want to do that particular action?

02:12.670 --> 02:19.560
Like how much does it really dislike doing that action so that it will do something that's a little

02:19.560 --> 02:20.460
bit easier?

02:20.460 --> 02:25.980
And I guess you could think of it in terms of games like The Sims, where they will have things that

02:25.980 --> 02:30.210
are their more favorite actions and that that they'll do and they'll do them more automatically.

02:30.210 --> 02:33.570
But if that's not available, then they will do something they don't like.

02:33.570 --> 02:39.180
And if they don't like it because its cost is much higher, then their emotions get affected.

02:39.180 --> 02:44.760
So you could also use this cost in factoring in how your agent feels.

02:44.760 --> 02:51.060
If you want to go into that kind of emotional side of programming, these agents which were not here,

02:51.060 --> 02:57.720
I must say so next we need a public game object for the target.

02:57.720 --> 03:03.510
Now the target is the location where this action is going to take place so that we have to move our

03:03.510 --> 03:07.920
nav mesh character from one spot to the other.

03:08.040 --> 03:17.970
Okay, then we're going to have a public game object, target tag, and this is so we can pick up game

03:17.970 --> 03:25.110
objects using the tag, find them if they exist in the hierarchy, just in case we don't actually have

03:25.110 --> 03:26.790
a target up here.

03:27.240 --> 03:29.400
And you'll see that used shortly.

03:30.000 --> 03:31.530
Public float.

03:31.530 --> 03:34.590
We want a duration of the action.

03:34.590 --> 03:36.240
How long is it going to take?

03:36.240 --> 03:44.460
How long should we have our agents standing around at a particular waypoint virtually performing the

03:44.460 --> 03:47.970
action and then we need some world states.

03:47.970 --> 04:04.320
So we need a public world state for the pre conditions and we need a public world state for the after

04:04.320 --> 04:05.220
effects.

04:06.240 --> 04:12.690
So these are the items that our planner is going to match on either side of the action.

04:12.690 --> 04:19.290
If you think back to the diagram that we had with all our actions linking up like matched dominos,

04:19.650 --> 04:20.040
okay.

04:20.040 --> 04:28.380
So next we need a public nav mesh agent, which is our nav mesh agent attached to our agent for moving

04:28.380 --> 04:29.130
it around.

04:29.340 --> 04:33.630
And then we're also going to create a public dictionary.

04:37.040 --> 04:38.150
String.

04:40.630 --> 04:42.400
String int.

04:44.030 --> 04:45.710
Preconditions.

04:46.840 --> 04:48.310
And a public.

04:50.560 --> 04:58.360
Dictionary string each of the effects.

04:58.780 --> 05:06.750
And what will do with these world states here is this allows us remember how we made the world state

05:06.760 --> 05:10.030
system serialize will up here and we're using world state.

05:10.030 --> 05:12.040
We can see these in the inspector.

05:12.040 --> 05:16.510
So you can type in all of your preconditions and your effects in the inspector.

05:16.510 --> 05:22.360
But when we want to go and plan with them in our planner, it makes a lot more sense for them to be

05:22.360 --> 05:27.040
in a dictionary, just makes it way easier to actually work with them.

05:27.040 --> 05:33.190
So we're going to use this to get them via the inspector and then we're going to put them into these

05:33.190 --> 05:34.780
two dictionaries.

05:34.930 --> 05:45.400
Next thing we need is another public world states which are our agent beliefs or the state of our actual

05:45.400 --> 05:47.110
agent itself.

05:47.110 --> 05:54.910
So it's got an internal set of states that we'll work with and then we finally up the top here, public

05:54.910 --> 05:57.070
bool for running.

05:57.070 --> 06:00.610
So are we performing this action at this time?

06:00.610 --> 06:07.000
And we'll initially set it to false because when we are performing an action, then we don't want to

06:07.030 --> 06:09.640
go on and be performing another action.

06:10.630 --> 06:13.390
So now let's go ahead and write our constructor.

06:13.390 --> 06:21.460
So there will be a public g action where we declare our precondition.

06:21.460 --> 06:34.390
So pre conditions equals new dictionary string int and the.

06:36.130 --> 06:42.010
Affect equals new diction every string int.

06:43.690 --> 06:44.400
Okay.

06:45.040 --> 06:47.930
Now, what's going to happen on the awake?

06:47.950 --> 06:54.340
Well, we need to set up all the bits and pieces that are going on with our actions.

06:54.360 --> 07:01.300
Initially, we're going to have to populate these two dictionaries with the values that are put into

07:01.300 --> 07:02.560
the inspector there.

07:02.560 --> 07:09.370
And we also need to get hold of the nav mesh agent that is on the same game object.

07:09.370 --> 07:13.600
So let's create a public void awake.

07:17.030 --> 07:19.040
And we'll set out agent up first.

07:19.050 --> 07:29.750
So agent equals this dot game object get component and we're getting hold of our nav mesh agent.

07:32.330 --> 07:33.350
For that.

07:33.590 --> 07:39.800
And then we need if preconditions does not equal null.

07:39.800 --> 07:46.250
So there's actually something in those preconditions in the inspector, then we're going to loop through

07:46.250 --> 07:46.430
them.

07:46.430 --> 07:58.340
So for each world state w in preconditions with the capital C, then we're going to go and add it to

07:58.340 --> 08:00.560
our preconditions dictionary.

08:00.560 --> 08:11.990
So pre conditions dot add w dot key and w dot value like that.

08:12.290 --> 08:16.970
And then we need the same thing again for our aftereffect.

08:16.970 --> 08:20.300
So let's just copy that and put it down here.

08:20.510 --> 08:30.890
This will be if the after effects aren't null, then we're going to get all the world states out of

08:30.890 --> 08:32.240
after effects.

08:33.890 --> 08:37.940
And we're going to add them to our effects.

08:40.610 --> 08:45.730
So we're now going to add in a few helper methods here that our planner is going to need.

08:45.740 --> 08:51.110
And first of all, once a public ball is achievable.

08:52.610 --> 09:00.020
To determine if we can actually achieve this particular goal and inside of here, which is going to

09:00.020 --> 09:00.590
return.

09:00.590 --> 09:01.640
True.

09:03.680 --> 09:05.990
Just assuming that we can.

09:06.000 --> 09:17.660
And then the more important one, which is a public bool, is achievable given and this will be is our

09:18.170 --> 09:25.680
action achievable given that there's certain preconditions coming in.

09:25.970 --> 09:32.900
So this will be dictionary which will be our string int.

09:34.160 --> 09:35.900
Four conditions.

09:37.280 --> 09:47.450
And then inside of here, we're going to go for each key value pair of string.

09:48.280 --> 09:49.000
INT.

09:51.000 --> 09:55.920
So our precondition in pre conditions.

09:55.920 --> 10:03.300
So we're looping through our preconditions that we've got stored for this particular action and we're

10:03.330 --> 10:08.550
looking for each of these conditions to make sure that it actually does match them.

10:08.550 --> 10:18.480
So if there's no sign of conditions, DOT contains key.

10:21.450 --> 10:23.790
O p dot ki.

10:26.790 --> 10:29.880
Then we will return false.

10:29.880 --> 10:34.710
So we can't actually do this because the preconditions aren't met.

10:34.890 --> 10:36.930
And then we will return.

10:36.930 --> 10:43.980
True if we've made it all the way through and found that we have matched all of the preconditions for

10:43.980 --> 10:44.430
that.

10:44.610 --> 10:50.820
Now, finally, we just need to and we'll get rid of those two methods because we're not going to use

10:50.820 --> 10:51.300
them.

10:53.310 --> 10:58.350
We're going to add in two public abstract methods.

10:59.320 --> 11:07.120
Bool pre perform and public abstract bull.

11:07.880 --> 11:09.770
Post perform.

11:09.920 --> 11:14.190
So remember, we're going to inherit from this particular class and want to make sure that we force

11:14.190 --> 11:20.030
that there's a pre performance and a post performance method that you write and that will allow you

11:20.030 --> 11:28.160
to put in customized code for each action that you can do other checks before an action actually starts,

11:28.160 --> 11:34.790
which is to do things like ensuring certain resources are available or other agents are available,

11:34.790 --> 11:41.330
and then any other post conditions that might come out of the particular action as to whether you're

11:41.330 --> 11:47.240
going to affect the world state with that action or whether you only affect the agent.

11:47.420 --> 11:47.780
All right.

11:47.780 --> 11:51.650
So we'll save that now that we've got the action done.
