WEBVTT

0
00:10.220 --> 00:16.520
In the last lecture, we set up the basic GOAP system with a simple agent and now we're going to add some actions.

1
00:16.750 --> 00:19.470
So create yourself a new C# file called

2
00:19.600 --> 00:26.520
'BakeBread' and I'll just bring one in that I've written before and we'll have a look what's in there.

3
00:26.770 --> 00:28.180
So, if we open it up...

4
00:28.180 --> 00:33.880
all of the actions that you write will be of this same format.

5
00:33.880 --> 00:39.440
Now they're all separate and individual modules by themselves, so there's no linking together.

6
00:39.460 --> 00:41.550
Remember: what links them together are

7
00:41.960 --> 00:45.850
their Preconditions and their Effects matching.

8
00:45.940 --> 00:51.750
Now this is called BakeBread and it's a GoapAction

9
00:51.910 --> 00:55.740
so it's going to inherit our stuff that's in that GoapAction set.

10
00:55.810 --> 01:02.020
The first thing we're going to do is to set 'completed' to False, so this will be a boolean value

11
01:02.020 --> 01:08.260
we keep, to know when we've finished our task. Tasks take time as well,

12
01:08.290 --> 01:16.120
so we're going to keep track of a startTime for the task and initialize it to 0 and then set a workDuration

13
01:16.120 --> 01:22.660
(so how long the task takes). It's public, so that you can set it in the Inspector.

14
01:22.960 --> 01:27.210
So I've got it for 2 seconds and you can change it as you like.

15
01:27.310 --> 01:36.940
Next we have our public constructor for this class, which is where we add in our Preconditions and our Effects.

16
01:36.970 --> 01:43.390
So these are probably the most important things in this whole system because these have to match each

17
01:43.390 --> 01:45.760
other, in other actions,

18
01:45.880 --> 01:52.900
in order for them to chain together. Now you can see here that our Precondition for BakeBread is that

19
01:52.900 --> 02:00.730
'hasFlour' is True and our addEffect (after we've baked the bread)

20
02:00.880 --> 02:04.230
is going to say that 'doJob' is True.

21
02:04.330 --> 02:17.560
So 'doJob' is True in our goal that we set, back here in Worker, which is up here...THAT is matching THAT.

22
02:17.570 --> 02:24.300
So that's going to make the goal backward-chain to THIS, so that it can then run it

23
02:24.620 --> 02:31.250
and then if 'hasFlour' is, in fact, True then it will start running this particular action,

24
02:31.250 --> 02:33.970
otherwise it's going to chain back up the list,

25
02:34.010 --> 02:39.500
if there are other actions, to see if it can keep satisfying them at some point. Now, in this case, we

26
02:39.500 --> 02:40.480
do have flour

27
02:40.490 --> 02:46.490
(because remember we've given our agent 5 bags of flour) and then back in this Worker system here, we're

28
02:46.490 --> 02:53.340
injecting hasFlour, which will give it a True because flour is greater than one.

29
02:53.480 --> 02:53.770
Right.

30
02:53.770 --> 03:00.560
So the name here is just for debug purposes, so that we can print it out if we need to (in the Console)

31
03:00.560 --> 03:01.990
to see what's going on.

32
03:02.030 --> 03:08.770
Right, then every action needs a 'reset', so that when that action is performed again, it's going to start

33
03:08.780 --> 03:09.950
with new values. So, startTime

34
03:09.980 --> 03:13.170
obviously back to 0 and 'completed' back to False

35
03:13.520 --> 03:20.150
and then there's a few other methods that are required, one's returning 'completed'

36
03:20.150 --> 03:28.280
(isDone, to say "Are we done or not?"), then we have another one called 'requiresInRange' and this is a function

37
03:28.280 --> 03:34.760
that you can set to False if you allow your agent to perform a task when they're not actually at the

38
03:34.760 --> 03:37.790
location they need to be at.

39
03:37.790 --> 03:45.800
So we're saying that "Yes, you do have to be at your assigned location to perform your task (or this particular

40
03:45.800 --> 03:47.130
task).

41
03:47.170 --> 03:54.710
You then have another option for more procedural Preconditions, so you can check that other things

42
03:54.800 --> 04:03.110
in the environment are True or False as well, before the agent carries out any of these actions.

43
04:03.120 --> 04:05.170
So at the moment we don't have any.

44
04:05.340 --> 04:07.320
We're just baking bread,

45
04:07.500 --> 04:14.430
quite simply but you might want to check something like maybe "You need to have 10 loaves of bread, before

46
04:14.430 --> 04:17.620
you deliver them to the bakery." or

47
04:17.790 --> 04:22.040
"You might not be able to bake if it's flooding or raining, depending on the weather.",

48
04:22.050 --> 04:28.920
all sorts of other things that don't fit in with the actual chain of actions that you would put into

49
04:28.920 --> 04:29.750
here.

50
04:29.790 --> 04:37.920
Next we have our 'perform' method, which ticks over while the task is taking place, so it initializes the

51
04:37.920 --> 04:39.510
startTime

52
04:39.540 --> 04:45.240
and I've got some Debugs in here as well, so that you can see them in the Console when it's starting a particular

53
04:45.240 --> 04:45.910
job

54
04:46.110 --> 04:51.360
and when it's finished that job, so this just ticks over for the amount of time it takes to perform that

55
04:51.360 --> 04:52.150
task.

56
04:52.170 --> 04:54.870
For every action, this is pretty much going to be the same

57
04:54.900 --> 04:59.370
but then you can perform different things based on which action you're in.

58
04:59.370 --> 05:01.320
This is the BakeBread action,

59
05:01.500 --> 05:07.920
so at the end of the BakeBread action, I'm saying that our flourLevel in our inventory is going to go

60
05:07.920 --> 05:15.240
down by 2 (because remember it takes 2 bags of flour to make one loaf of bread) and

61
05:15.240 --> 05:21.930
then we've got rid of 2 bags of flour and then we're going to add 1 loaf of bread into our inventory,

62
05:22.230 --> 05:24.180
then we say 'completed' is True

63
05:24.360 --> 05:29.600
which will finish this task because it will be returned to the system up here.

64
05:30.490 --> 05:34.620
So that's the entire code for our BakeBread.

65
05:34.680 --> 05:42.510
So save that...go back to Unity...you want to add that code onto your agent.

66
05:42.750 --> 05:47.340
So this is an action, you drag-and-drop it onto the agent

67
05:47.340 --> 05:51.330
and now you can create as many different actions as you like and you just keep dragging-and-dropping

68
05:51.330 --> 05:55.330
them onto the agent and the agent will know about these actions.

69
05:56.100 --> 06:00.920
Now the only thing you need to set at the moment is the Target for BakeBread.

70
06:00.990 --> 06:02.340
Where do you want to bake bread?

71
06:02.340 --> 06:09.990
We're going to bake it at the Bakery, so we find our bakery marker (which is my Bakery 'Door' there) and I'm

72
06:09.990 --> 06:16.010
going to drag-and-drop THAT as the Target for baking.

73
06:16.460 --> 06:16.800
Ok.

74
06:16.820 --> 06:22.730
Now you're ready to run it, so I'm going to switch to the Console so I can see what's going on...just clear that and let's

75
06:22.730 --> 06:24.350
press Play.

76
06:24.740 --> 06:29.790
So it's going to move to the location where it can perform the BakeBread action

77
06:29.930 --> 06:35.000
and then when it gets there, it will start baking bread and you can see that in the Console. When it's

78
06:35.000 --> 06:40.490
finished, you'll see that your inventory and your flour will go down 2 each time.

79
06:40.490 --> 06:41.800
Now that's happened quite quickly

80
06:41.810 --> 06:47.680
but if you go back and look through the Console, there's an awful lot there.

81
06:47.690 --> 06:52.830
Let's just pause it because now it can't find any plan, it's just going to keep going 'Failed Plan'.

82
06:52.930 --> 06:56.340
We can find where the baking happened...here we go.

83
06:56.340 --> 06:58.670
So we're moving just here.

84
06:58.740 --> 07:00.120
So we've moved to BakeBread...

85
07:00.120 --> 07:06.840
we're starting BakeBread...finished BakeBread...moved to BakeBread...starting BakeBread...finished

86
07:06.840 --> 07:07.140
BakeBread.

87
07:07.140 --> 07:12.180
So we've made 2 loaves of bread and then it says NO PLAN

88
07:12.270 --> 07:17.610
and that's because we've run out of flour. So there's only 1 lot of flour left and we've got 2 loaves

89
07:17.610 --> 07:19.650
of bread out of that.

90
07:19.650 --> 07:23.430
Now there's no way to get more flour, at this point.

91
07:23.430 --> 07:26.910
So the plan for doing his job has failed.

92
07:26.910 --> 07:30.150
He can't keep going when processing it.

93
07:30.150 --> 07:37.500
So what we can do now is add in another action that helps him get some flour, so that he can keep baking.

94
07:37.940 --> 07:40.540
Right, so stop playing that

95
07:40.710 --> 07:45.940
and in the project you're going to create a new C# file called 'PickupFlour'

96
07:46.380 --> 07:49.490
and that will look like this one here.

97
07:49.650 --> 07:51.020
So PickupFlour...

98
07:51.180 --> 07:59.610
if it's easier for you, then just make a complete copy of the BakeBread action and then put it in your

99
07:59.680 --> 08:04.090
PickupFlour script and then just make sure you change the name here...

100
08:04.990 --> 08:07.180
and in the constructor...

101
08:07.180 --> 08:15.280
so THERE and then also the PickupFlour as well. Everything else can stay exactly the same because

102
08:15.280 --> 08:17.890
it's just processing the action.

103
08:17.890 --> 08:25.540
Now the Precondition and the Effect for PickupFlour is that we're going to get the agent to pick up flour

104
08:25.540 --> 08:26.620
when we've run out of flour.

105
08:26.620 --> 08:32.770
So when 'hasFlour is False' is when this action should take place

106
08:33.070 --> 08:38.190
and then the Effect after that, is that hasFlour will be equal to True.

107
08:38.230 --> 08:45.520
So this Effect here is setting hasFlour to True, which will then allow the BakeBread action to run.

108
08:45.520 --> 08:48.090
So that's the linkage that will happen there.

109
08:48.910 --> 08:57.130
Now further down in our 'perform', where we need to set up how the inventory is going to change, is that

110
08:57.160 --> 09:03.640
after we've finished picking up the flour, then we can pick up the flourLevel in our inventory. In this

111
09:03.640 --> 09:04.420
case,

112
09:04.420 --> 09:10.950
I'm saying that when he goes to the mill to pick up the flour, he can actually carry 5 lots of flour

113
09:10.960 --> 09:16.780
and so we're going to add 5 into the inventory, which will allow him to keep going and creating more

114
09:16.780 --> 09:18.400
bread for a little while.

115
09:18.400 --> 09:18.640
Right.

116
09:18.640 --> 09:26.710
So once you've got all that in there, just save that, go back into Unity and that script again can get dropped

117
09:27.070 --> 09:30.540
(PickupFlour) onto the agent.

118
09:30.730 --> 09:34.960
Remember we need to set up our Target destination for picking up the flour

119
09:34.960 --> 09:37.740
and in this case, we'll say that it's at the Windmill Door.

120
09:37.750 --> 09:42.840
So we'll use the Windmill Door for our Target for picking up the flour.

121
09:43.090 --> 09:50.680
Now when we press Play, we can watch the flour go down (so it'll go down 2 with each loaf of bread that's

122
09:50.680 --> 09:56.600
produced) and then we know when there's 1 lot of flour left and 2 bread,

123
09:56.620 --> 10:02.260
he's going to have to go and pick it up from the mill and here you can see him toddling off to pick up the

124
10:02.260 --> 10:03.000
flour.

125
10:06.380 --> 10:12.770
Now if this is all just a little bit too slow for you, remember you can click on your BAker agent, go to

126
10:12.770 --> 10:16.070
your Nav Mesh Agent and change his Speed.

127
10:16.070 --> 10:21.170
So if we set it to 15, which should speed things along a bit,

128
10:21.170 --> 10:23.970
it won't increase the time it takes to complete tasks,

129
10:23.990 --> 10:29.540
it will just increase how long it takes for him to go between places, so that you can see it happening

130
10:29.930 --> 10:30.680
a little bit quicker.

131
10:30.680 --> 10:37.130
Now we can see he's going back over...getting the flour...the flour in your inventory will go up to 6 because

132
10:37.130 --> 10:43.010
you had 1, he's added 5 and then he will go ahead and use that flour and bake more bread

133
10:43.010 --> 10:46.910
and when he runs out, he will go back and get that.

134
10:46.910 --> 10:53.870
Now you can make quite a complex chain of all the different events that you want to occur in any

135
10:53.870 --> 10:55.300
particular behavior

136
10:55.310 --> 11:01.290
but just with a couple you can get some quite complex things happening, as you can see here.

137
11:01.530 --> 11:04.570
All right, so that's setting up your little chain

138
11:04.700 --> 11:07.400
and in this case, it's kind of like a supply-chain.

139
11:07.610 --> 11:13.610
In the next lecture, what we'll get him to deliver the bread (when he's got enough) to the

140
11:13.610 --> 11:14.600
markets.