WEBVTT

0
00:10.890 --> 00:16.240
In the last lecture, we came up with a plan for making bread and also picking up the flour to make that

1
00:16.240 --> 00:16.930
bread.

2
00:16.930 --> 00:23.350
Now we want to add in another plan where we deliver the bread. Delivery of the bread is different

3
00:23.350 --> 00:24.290
to the other

4
00:24.290 --> 00:30.190
chain. It doesn't really fit in because we want to deliver bread but only when we've got a certain quantity

5
00:30.220 --> 00:31.200
of bread.

6
00:31.360 --> 00:38.590
BakeBread produces just 1 loaf but we want to accumulate them into a delivery.

7
00:38.640 --> 00:43.590
We're going to begin with the previous project where the Baker was baking bread and picking up flour

8
00:43.650 --> 00:50.940
as needed and to begin with, we're going to insert a new action for delivering the bread so that we will

9
00:50.940 --> 00:59.370
get a chain that looks like this...that we have Pickup Flour...Bake Bread and then Deliver Bread.

10
00:59.370 --> 01:05.810
Notice that the 'doJob' effect is moved to the end of deliver bread and a

11
01:05.820 --> 01:09.610
'hasBread' True is used to link the baking and the delivery.

12
01:09.840 --> 01:13.460
So to achieve this you need a new action script, so create

13
01:13.480 --> 01:21.620
another C# script and call it DeliverBread and it's going to look like this.

14
01:21.630 --> 01:27.390
So, the DeliverBread is a DeliverBread action.

15
01:27.390 --> 01:32.800
Remember you can copy this from your other actions to save you typing it all out again and just modify

16
01:32.800 --> 01:37.470
DeliverBread there and then also in the constructor.

17
01:37.470 --> 01:44.480
Now the Precondition for delivering bread is that you have bread and the Effect is for doing the job.

18
01:44.490 --> 01:49.530
So for the Baker to be doing his job completely,

19
01:49.530 --> 01:53.100
he has to deliver the bread, not only make it.

20
01:53.130 --> 01:58.890
Now when we deliver bread (if we go back down into our performance) that will mean that the bread level

21
01:58.950 --> 02:02.370
in the inventory is going to go down by 1.

22
02:02.370 --> 02:08.470
So make sure that you add that physical environment Effect into your script here.

23
02:08.610 --> 02:10.070
Save that, then

24
02:10.060 --> 02:17.560
back in your BakeBread action, you're going to have to change your final Effect here to...

25
02:17.580 --> 02:26.730
instead of doing the job ('doJob'), it's going to be 'hasBread' is True and then that will link that action with

26
02:26.790 --> 02:28.260
the DeliverBread action.

27
02:28.260 --> 02:31.320
So make sure you save that as well.

28
02:31.320 --> 02:38.310
Now go back into Unity, select the Baker and you want to drag-and-drop the DeliverBread script onto

29
02:39.090 --> 02:44.250
the Baker and then set the delivery location, which will be the Market Door.

30
02:44.250 --> 02:46.410
So lets drag-and-drop that there.

31
02:46.500 --> 02:47.940
Now when we press Play,

32
02:47.970 --> 02:52.080
we won't initially have any bread in our inventory, so we can't deliver anything

33
02:52.260 --> 02:55.700
but he will make bread (and you've got bread is 1)

34
02:55.710 --> 03:01.050
and then go and deliver it and drop it off and then he will come back and he's already got enough flour

35
03:01.050 --> 03:05.880
so he can still make another loaf of bread and then go and drop that off

36
03:05.970 --> 03:12.410
and now he doesn't have enough flour so he'll have to go to the mill to grab some flour

37
03:13.350 --> 03:21.270
and then he will come back to the bakery and bake some more bread...but only bake 1 loaf of bread because

38
03:21.270 --> 03:27.620
that sets the 'hasBread' to True, which is going to cause that link.

39
03:27.620 --> 03:33.290
Now the reason that we can't test for how many pieces of bread that we've got in order to do

40
03:33.290 --> 03:41.240
this is because each action has to have the Preconditions and the Effects that link together, so that

41
03:41.240 --> 03:46.820
when the A* algorithm is running through the plan and trying to find all of the actions that will

42
03:46.820 --> 03:48.020
link together,

43
03:48.320 --> 03:51.890
if it doesn't have a match, then it's not going to work.

44
03:51.890 --> 03:59.150
So you can't have the bakery say 'hasBread' is False and then have the market say 'hasBread' is True and

45
03:59.150 --> 04:06.620
expect that chain to be formed in the planning stage, which means that it's never going to have a plan

46
04:06.620 --> 04:12.290
that just starts with baking bread and then it's going to just break and fall over

47
04:12.290 --> 04:15.550
and you can try that if you like, it just will not work.

48
04:15.560 --> 04:25.110
So instead of doing that, you want to add a separate plan. So we'll go back in to our DeliverBread and

49
04:25.490 --> 04:34.090
at the top, we're going to set this back to 'hasDelivery'. So a delivery will be a bigger thing and it's

50
04:34.090 --> 04:36.790
going to satisfy the 'doJob'.

51
04:36.820 --> 04:42.370
Now when a delivery has occurred, let's say that there's 5 loaves of bread in a delivery, so at the

52
04:42.370 --> 04:47.600
bottom here that will make our inventory level go down by 5.

53
04:47.860 --> 04:49.270
So that's all well and good.

54
04:49.270 --> 04:56.410
So save that, let's go back into BakeBread. Now BakeBread is going to go back to what it was before,

55
04:56.410 --> 05:01.900
so we're going to 'doJob' and continue to do the job. Save that

56
05:01.900 --> 05:04.320
and then you want to go over to Worker

57
05:04.570 --> 05:15.280
and we need to inject the 5 loaves of bread as a World State, which will then trigger off the delivery.

58
05:15.310 --> 05:18.930
So we're going to put it up here in the GetWorldState with the flour,

59
05:18.940 --> 05:20.080
so up here we'll just add...

60
05:23.250 --> 05:24.210
worldData.Add

61
05:27.380 --> 05:35.980
'hasDelivery' and it will be equal to the inventory's breadLevel

62
05:37.140 --> 05:39.360
greater than 4

63
05:39.390 --> 05:43.750
which means we have 5 (or more) loaves of bread that we can deliver

64
05:43.890 --> 05:50.460
and then when that does reach that and 'hasDelivery' is True, then a delivery can occur.

65
05:50.460 --> 05:58.640
So we'll save that and switch back into Unity and press Play. So, initially we've got no bread so we're

66
05:58.640 --> 06:08.570
going to start making bread...got 2 loaves...go and get some more flour and get enough flour that we should be

67
06:08.570 --> 06:15.950
able to make 3 more loaves...those 3 loaves will be made at which point (we hope!) we should be able to

68
06:15.950 --> 06:19.610
deliver because we'll have 5 lots of bread and there he goes

69
06:19.610 --> 06:24.740
and then when he gets there and that action completes, it will reduce the bread in the inventory down

70
06:24.740 --> 06:25.680
to 0.

71
06:26.000 --> 06:27.230
There's no flour either

72
06:27.260 --> 06:30.850
and we have to go back to the mill to pick that up.

73
06:30.920 --> 06:38.210
So now we have a complex series of events with just some really simple chains of actions in

74
06:38.210 --> 06:44.570
them and you can see from this how easy it is to just keep adding actions to do all sorts of different

75
06:44.570 --> 06:50.710
tasks. In the next lecture, we'll look at adding a global inventory and another agent.