WEBVTT

0
00:12.180 --> 00:17.970
In order to make a complex world, you're going to need to add more agents and you're also going to need

1
00:18.360 --> 00:20.250
to add other inventories.

2
00:20.320 --> 00:25.590
Currently, the inventories that we've created are living with the particular agent (in this case, it's just

3
00:25.590 --> 00:26.440
the Baker).

4
00:26.580 --> 00:31.190
So he's keeping track of how much flour is in the world and how much bread is in the world

5
00:31.410 --> 00:36.420
but in order for him to really go and pick up bread from the mill, he needs to know if there's any flour

6
00:36.510 --> 00:40.950
AT the mill and therefore the mill itself needs to have its own inventory.

7
00:41.160 --> 00:49.290
Here you can see what I've created is a little simulation that now includes a Farmer who is delivering wheat

8
00:49.410 --> 00:56.760
to the mill and the Baker (when he runs out of flour and there's no flour in the mill) sits around idle

9
00:56.770 --> 01:02.200
and waits for the Farmer to fill up the mill and then will go back to his job.

10
01:02.220 --> 01:06.710
So let's go about recreating this.

11
01:06.870 --> 01:12.870
First of all, I'm going to stop playing.  I'm going to take my Farmer I've got and turn it off so that it's not really part

12
01:12.870 --> 01:14.430
of the equation right now

13
01:14.430 --> 01:20.370
but what you will need to do is create yourself a farm or a paddock which I've just made a little

14
01:20.370 --> 01:20.900
square

15
01:20.910 --> 01:27.270
and I've made the door (which is the point where the action will take place) for harvesting, a little later

16
01:27.270 --> 01:27.450
on.

17
01:27.450 --> 01:31.550
I also took off the colliders on these things, so that the agent wouldn't have an issue and

18
01:31.560 --> 01:34.340
he can walk into the middle of the paddock.

19
01:34.470 --> 01:39.290
So the first thing you need to do is make an inventory for the Windmill.

20
01:39.300 --> 01:46.050
I've taken the Windmill and I've just added the inventory that I was already using

21
01:46.290 --> 01:47.110
for the Baker.

22
01:47.130 --> 01:54.330
So the inventory script has been dragged-and-dropped onto the Windmill but not before a few little

23
01:54.330 --> 01:54.980
changes.

24
01:54.990 --> 01:55.890
Let's go and have a look.

25
01:55.890 --> 02:04.920
So in here, I've just increased it by adding in a 'drawOffset' and a 'name' as well

26
02:04.920 --> 02:12.210
so that this just effects the GUI system, so that when it draws it on the screen, it uses the offset

27
02:12.780 --> 02:17.600
to where the inventory is positioned and also inserts the name,

28
02:17.600 --> 02:24.540
so you know whose inventory it is and this will show up for the Windmill. So if you make those changes

29
02:24.570 --> 02:32.850
and just save it, go back into Unity, then on the Windmill add that script and the Draw Offset I've got is 100

30
02:33.030 --> 02:34.780
and the name is Windmill.

31
02:34.850 --> 02:37.750
Now that means you'll need to set that for your Baker.

32
02:37.770 --> 02:44.930
So if you go to your Baker and find the Inventory script, just put the name for Baker and the Draw Offset

33
02:44.940 --> 02:50.910
can be 0, so that when you press Play, you will get the Baker at the top and the Windmill at the bottom

34
02:50.910 --> 02:55.280
and you can keep track of what's in each of those inventories.

35
02:56.270 --> 02:58.540
The script that needs to know about the windmill's

36
02:58.540 --> 03:00.500
inventory is Worker,

37
03:00.620 --> 03:08.290
so click on the Worker C# script and at the top, we're going to pass through the inventory from

38
03:08.380 --> 03:09.760
the Windmill.

39
03:10.000 --> 03:18.490
Add that in and then the flour levels in the windmill are going to be used down in the World State to

40
03:18.490 --> 03:22.250
inject whether 'hasStock' is True or not

41
03:22.300 --> 03:27.760
and you can see here that we're getting windmill.flourLevel greater than 4, which is going to

42
03:27.760 --> 03:35.130
tell our Baker that there's enough bags (it's 5 bags, at least, for it to go and pick up in there).

43
03:35.380 --> 03:39.250
That's all you need to add to the Worker class.

44
03:39.460 --> 03:40.990
So save that.

45
03:41.080 --> 03:46.980
The other thing that wants to know about that flour level is your PickupFlour action.

46
03:47.020 --> 03:53.980
So at the top of that, you also want to pass through the Windmill's inventory here and then in order to pick

47
03:53.980 --> 03:54.640
up flour,

48
03:54.640 --> 04:03.820
we now have another Precondition. So addPrecondition("hasStock", true) so that when

49
04:03.820 --> 04:07.460
that's true, then we can go and pick up our flour.

50
04:07.930 --> 04:10.710
So we're only going to do it when there's stock at the Windmill

51
04:10.720 --> 04:15.590
and we've run out of flour. So they're our first 2 initial conditions.

52
04:15.670 --> 04:23.470
Now when we finally perform the PickupFlour, you have to remember that you want to add the flourLevel

53
04:24.130 --> 04:31.360
to your own inventory, so that the Baker is now carrying the flour and the Windmill is losing the

54
04:31.360 --> 04:36.930
flour, so you need to take it out of the Windmill's level for inventory.

55
04:36.970 --> 04:42.790
So once you've done those, save all of that and then when you press Play you'll be able to watch this

56
04:43.180 --> 04:46.090
in action.

57
04:46.160 --> 04:50.200
The Baker's going to bake the bread, he's going to run out of flour,

58
04:50.210 --> 04:54.790
he's going to go pick up some flour (which will take all of the bags of flour out of the windmill that

59
04:54.800 --> 04:56.580
you can see in the inventory),

60
04:56.600 --> 05:02.990
he'll come back and bake 3 more loaves of bread and then he'll have enough for a delivery and he will deliver

61
05:02.990 --> 05:03.430
it

62
05:04.500 --> 05:09.060
and once he's delivered it, he's run out of bread,

63
05:09.070 --> 05:10.200
he's run out of flour,

64
05:10.240 --> 05:11.710
the mill's run out of flour,

65
05:11.770 --> 05:16.470
there's no flour anywhere, so he can't do anything and all his plans are going to fail.

66
05:16.540 --> 05:23.590
He's going to sit and wait at the markets until they get some flour in the windmill, so we can actually

67
05:23.590 --> 05:24.450
do that a

68
05:24.520 --> 05:31.870
bit sneakily. If we select the Windmill in the Hierarchy (we're still playing) and go over to the Flour Level

69
05:31.870 --> 05:37.740
and let's just deliver 10 lots of flour...and now you can see: off he goes.

70
05:37.980 --> 05:45.850
So we want to now add another agent (a Farmer agent) that can take wheat from the Paddock and deliver

71
05:45.850 --> 05:53.070
it into the Windmill, so that when the Windmill DOES run out of flour, that the Baker can get some more.

72
05:53.500 --> 06:02.320
So to create a new Farmer agent, I duplicated the Baker and I made it green and changed its hat, just so

73
06:02.320 --> 06:09.240
that it looks different. You then want to take all of the scripts relating to the Baker away, leave the GOAP

74
06:09.260 --> 06:16.270
agent, the Farmer doesn't need it's own inventory (it's not actually keeping track of everything, it's

75
06:16.270 --> 06:19.740
just picking up wheat and taking it to the Windmill).

76
06:19.810 --> 06:23.900
Later on, if you want to make that more complicated, then feel free to.

77
06:24.220 --> 06:32.170
We're going to add a Farmer script, which is the GOAP agent driving script, like the Worker script on

78
06:32.170 --> 06:37.380
the Baker and 2 actions (1 Harvest and 1 DeliverWheat).

79
06:37.540 --> 06:40.410
Let's have a look at the Farmer script first.

80
06:40.660 --> 06:47.450
You'll want to duplicate the Worker script and just rename it to Farmer.

81
06:47.560 --> 06:55.460
So at the top we've got Farmer and then everything else is the same. The GetWorldState,

82
06:55.480 --> 06:58.800
there's nothing in there because it doesn't need to do anything

83
06:58.810 --> 07:08.050
and then the job in the CreateGoalState is 'doJob' but this 'doJob' for this agent is based on the actions

84
07:08.050 --> 07:13.210
that will be on the agent, so he's not going to be baking bread and picking up flour.

85
07:13.540 --> 07:21.700
The rest of the code is exactly the same as for the Worker script that's on the other agent.

86
07:21.730 --> 07:25.170
So they're the only things in there.

87
07:25.210 --> 07:27.490
The 2 actions that we're adding...

88
07:27.490 --> 07:34.360
first of all, we've got a 'Harvest' action (which is going to the farm and picking the wheat),

89
07:34.660 --> 07:39.380
so it's called Harvest.cs, class is Harvest.

90
07:39.400 --> 07:42.280
The top is the same...the

91
07:45.350 --> 07:49.170
constructor, of course, is called Harvest.

92
07:49.250 --> 07:55.160
Now Harvest doesn't have any Preconditions, so you can have an action without Preconditions and

93
07:55.160 --> 07:58.610
you can also have an action without any Effects.

94
07:58.610 --> 08:03.530
Now, nothing needs to happen for the Farmer to harvest.

95
08:03.530 --> 08:09.410
You might want to set up some kind of day and night cycle in your game, so that the Farmer only harvests

96
08:09.680 --> 08:15.710
during the day and then you could put that in the world conditions that are coming through in the Farmer

97
08:15.740 --> 08:16.500
code

98
08:16.520 --> 08:18.690
but in this case, there's nothing like that.

99
08:18.740 --> 08:23.920
So the effect of the harvest is that you will have wheat, so 'hasWheat'

100
08:23.960 --> 08:28.110
is set to True. Now the rest of this action,

101
08:28.120 --> 08:31.210
there's really nothing going on in it at all.

102
08:31.210 --> 08:32.740
So it's just the default one.

103
08:32.800 --> 08:38.770
It's not updating any inventory in the 'perform' method or anything like that.

104
08:38.890 --> 08:41.160
Ok, so that's the harvest.

105
08:41.290 --> 08:47.980
Now the other one that we have on the Farmer is the DeliverWheat, which is to take the wheat to the

106
08:47.980 --> 08:49.020
flour mill.

107
08:49.240 --> 08:51.180
It's a little bit different.

108
08:51.220 --> 08:57.670
Open that up and you'll see that we've got a 'public Inventory' being passed into it and this is the

109
08:57.710 --> 09:04.930
Windmill's inventory. That's so that we can update the Windmill's inventory when this action has completed.

110
09:05.020 --> 09:13.360
So it's called DeliverWheat, it's constructor is DeliverWheat, its Precondition is 'hasWheat' is True,

111
09:13.390 --> 09:18.190
which is set to True when we do the harvest

112
09:18.760 --> 09:22.340
and then the Effect is

113
09:22.470 --> 09:23.140
("doJob", true)

114
09:23.290 --> 09:26.290
and this is what's causing this whole little chain to run

115
09:26.290 --> 09:34.890
because 'doJob' is the main goal of the Farmer and the 'name' here (which I haven't changed) should also

116
09:34.890 --> 09:37.110
be 'DeliverWheat'.

117
09:37.410 --> 09:40.500
This is what you get when you copy-and-paste all of your actions.

118
09:40.560 --> 09:45.770
Now the rest of the code is pretty much the same...going down...here.

119
09:45.780 --> 09:53.580
The only thing you'll see is when we perform this task of DeliverWheat, we're going to update the

120
09:53.580 --> 09:56.790
flourLevel in the inventory by 1 bag.

121
09:56.790 --> 09:59.910
This inventory is not the Farmer's inventory.

122
09:59.910 --> 10:00.630
He doesn't have one.

123
10:00.630 --> 10:05.110
It's the Windmill's inventory. Ok, so save those,

124
10:05.210 --> 10:13.190
then let's go over to the Farmer and you want to attach those 2 scripts (those 2 actions) and the Farmer script

125
10:13.610 --> 10:14.610
as well.

126
10:16.930 --> 10:22.930
Both of the actions (Harvest and DeliverWheat) will both need locations, so the Harvest script is

127
10:22.930 --> 10:30.010
going to happen at the Farm Door (which is our Farm/Paddock) and then the DeliverWheat is going to happen

128
10:30.100 --> 10:32.920
at the Windmill Door, where we drop that off.

129
10:32.920 --> 10:38.980
Now you'll see in the DeliverWheat action, we've also opened up that 'public inventory', so that we can

130
10:38.980 --> 10:45.220
pass the Windmill's inventory into it, so you can just drag-and-drop the Windmill into that spot and pick

131
10:45.220 --> 10:50.360
up the inventory of the Windmill that this script can now update.

132
10:51.560 --> 10:58.870
Now let's run it and what we'll see is both agents working towards their own goals.

133
10:59.270 --> 11:08.420
Ok, so as the Farmer delivers, you'll see that the windmill will go up 1 every time and the agent just

134
11:08.420 --> 11:16.210
picked up 5 bags, which has now taken them back to bake some bread and he'll go and deliver some bread.

135
11:17.790 --> 11:22.980
Now he's not going to be able to go back to the Windmill until there's 5 bags to pick up,

136
11:22.980 --> 11:27.720
so he's kind of stuck in limbo as far as baking bread because he's got no flour and there's none in

137
11:27.720 --> 11:28.770
the Windmill.

138
11:28.770 --> 11:37.830
When that Windmill inventory hits 5 flour (which it will right now), it will trigger the Baker to perform his

139
11:37.840 --> 11:45.120
goals and off he'll go, pick up some flour and then he can get into the job of baking bread.

140
11:47.380 --> 11:55.220
Ok, so you can see how this can get very complex, very quickly and how you can have inventory systems

141
11:55.220 --> 12:01.130
that belong just for the agents themselves so that they can keep track of what they've got

142
12:01.130 --> 12:06.920
but then there's also things in the world that need inventories as well, so that you can move products

143
12:07.010 --> 12:14.330
around. What's really important is if you take something from one inventory, that you add it to another

144
12:14.630 --> 12:19.130
and that you make sure you minus it from the value in that previous inventory,

145
12:19.130 --> 12:24.740
otherwise your numbers are going to get all out of whack and it might be a good idea if you actually

146
12:24.740 --> 12:30.260
set up a spreadsheet or something to keep track of all of these things that are going around

147
12:30.380 --> 12:37.460
or even have global variables in some kind of 'inventory count file' that you can keep the actual

148
12:37.460 --> 12:42.680
values that you're going to be adding and minusing, so that you only have to change them in one

149
12:42.680 --> 12:44.580
place in your code.

150
12:44.730 --> 12:45.030
Right.

151
12:45.080 --> 12:46.400
So that's the GOAP system

152
12:46.400 --> 12:51.710
and I hope that has inspired you with lots of creativity for designing different types of simulation

153
12:51.710 --> 13:00.230
games like this but also using GOAP for RPGs and First Person Shooters as well. So if you have a read

154
13:00.230 --> 13:05.390
through that research literature link that I sent you at the beginning, you'll see that this was first

155
13:05.390 --> 13:09.650
used in FPS-type games but it's very adaptable.