WEBVTT

00:03.240 --> 00:09.660
In this video, we're going to start adding the cubicles in as resources that are available to the patient

00:09.660 --> 00:10.590
and the nurse.

00:10.620 --> 00:14.250
First of all, what we need to do is to go into our world.

00:14.250 --> 00:24.360
So g world and add in here a queue of cubicles, just the same way that we've got this queue of patients.

00:24.390 --> 00:30.930
Now, I'm going to leave that as an initial challenge for you to do, and I'd like you to think about

00:30.930 --> 00:38.160
the setting up of the cubicle and how the values of each cubicle that's already in the game world are

00:38.160 --> 00:45.180
going to get into the queue in the first place because they're not like the patients that can kind of

00:45.180 --> 00:53.160
inject themselves in there when their goals have been achieved or an action gets to a certain place.

00:53.280 --> 00:57.360
Okay, so go away and try and do that.

00:57.360 --> 01:03.120
So add a queue and add all the necessary bits and pieces that need to go throughout this whole G world

01:03.120 --> 01:05.810
class in order to look after that queue.

01:05.820 --> 01:12.450
And when we come back, I will show you how to do that and put it together.

01:12.450 --> 01:15.330
So pause the video now and good luck.

01:15.990 --> 01:16.900
Welcome back.

01:16.920 --> 01:18.060
How did you go?

01:18.090 --> 01:22.380
Well, first of all, we have to create a queue, so that should be pretty straightforward for you,

01:22.380 --> 01:29.250
which is to copy the patients, place it in there and call it cubicles.

01:30.060 --> 01:30.600
Okay.

01:30.600 --> 01:32.070
We can then go ahead.

01:32.070 --> 01:35.130
And in the world is construct that queue.

01:35.160 --> 01:42.540
So let's just grab that and make a copy of it and we'll call that cubicles as well.

01:42.990 --> 01:43.500
Okay.

01:43.500 --> 01:48.450
So then we need two helpers, which is our add cubicle and remove cubicle.

01:48.450 --> 01:53.550
So we will just copy those and put them down in here.

01:53.730 --> 02:05.760
So we'll just rename them as add cubicle and remove cubicle and then we'll also change the queues inside

02:05.760 --> 02:09.390
of those so that we've got the correct ones.

02:11.220 --> 02:15.540
Cubicle and cubicle.

02:16.860 --> 02:17.160
Right.

02:17.160 --> 02:24.420
So now we need to actually populate that queue because these cubicles are things that exist in the game

02:24.420 --> 02:25.360
environment.

02:25.380 --> 02:31.470
So one way you could do it is you could write a script that you put on each cubicle.

02:31.470 --> 02:37.950
So as a resource it has a little script, and then it says in the start method it's say something like

02:37.950 --> 02:46.530
Gee, world cubicles, ad cubicle and then injects itself into that queue, which is fine.

02:46.830 --> 02:53.520
That would work fine and that would be good if you wanted to add cubicles dynamically into the world.

02:53.520 --> 02:58.890
But what we're going to do here is actually just pick up all of the cubicles that are in the world.

02:58.890 --> 03:09.060
So we're going to do a game object array and let's just call this cubes equals game object dot find

03:09.060 --> 03:10.980
objects with tag.

03:11.010 --> 03:18.780
Remember that all of those cubicles are tagged with cubicle with a capital C so we can go through the

03:18.780 --> 03:21.600
environment and pick up the ones that are already existing.

03:21.900 --> 03:23.370
Then we can loop through them.

03:23.370 --> 03:35.310
So for each game object see in cubes, cubicles dot in queue.

03:35.310 --> 03:39.930
So we stick them into the queue and we're putting C into that queue.

03:40.350 --> 03:51.060
Then after that we also need to insert a state into the world about the cubicles and whether they're

03:51.060 --> 03:54.000
free or not, just like if a patient is waiting.

03:54.000 --> 03:59.100
So initially we're going to say if cubes dot length.

04:00.240 --> 04:02.130
Is greater than zero.

04:02.730 --> 04:05.100
Then we want to put world.

04:06.260 --> 04:12.500
Dot modify state free cubicle.

04:15.050 --> 04:18.020
Cubes at length.

04:18.020 --> 04:25.640
And then when that goes into the world states, it automatically gets picked up by our little updater

04:25.640 --> 04:28.820
that's putting it into that gooey that's on the screen.

04:30.230 --> 04:30.560
All right.

04:30.560 --> 04:32.750
So save that and switch back into unity.

04:32.750 --> 04:34.460
And we just want to test that it's working.

04:34.460 --> 04:36.680
So just press play.

04:36.680 --> 04:43.610
And because of the world is doing it itself, we're not reliant on any of our agents updating the state.

04:44.270 --> 04:44.460
Okay.

04:44.600 --> 04:53.120
So now you can see it says free cubicle and there's three available and therefore we can use free cubicle,

04:53.330 --> 05:00.920
something that our patient or our nurse is looking for in order to do another action so it can be a

05:00.920 --> 05:02.120
precondition.

05:02.690 --> 05:05.630
So now we have this new resource in the environment.

05:05.630 --> 05:12.980
So it's not an agent and it's not a goal or sub goal.

05:13.490 --> 05:18.650
It's not really a state, but it's existence causes a state.

05:18.650 --> 05:26.120
So the cubicles are causing a state of free cubicle or I guess no free cubicles, which is kind of,

05:26.120 --> 05:27.230
I guess the opposite.

05:27.350 --> 05:35.690
And therefore, we need another way of organizing these things that the agents need on them or available

05:35.690 --> 05:37.400
in order to complete a task.

05:37.400 --> 05:42.560
And that's now where the idea of an inventory comes in.

05:42.830 --> 05:49.160
So in our GOOP folder, what we're going to do is create an inventory.

05:49.340 --> 06:00.740
So let's go see shop and we'll call it a G inventory just to keep up with the way we've been coding

06:00.740 --> 06:03.200
so far with the conventions of the names.

06:03.200 --> 06:12.140
Now, the inventory itself is not a mono behavior and therefore will not have a start and an update.

06:12.560 --> 06:15.920
So it's basically just keeping a list.

06:16.220 --> 06:22.790
So a list of game objects, which will be our items that we can put in our inventory or backpack or

06:22.790 --> 06:24.200
whatever you want to call it.

06:24.800 --> 06:30.110
So let's call it items and it's going to equal a new list of game objects.

06:31.610 --> 06:36.020
Now it's going to require three help or functions.

06:36.020 --> 06:39.590
And let's just get rid of that, as always, put that in there.

06:39.710 --> 06:40.020
Okay.

06:40.100 --> 06:43.730
So it's going to have a public void add item.

06:46.460 --> 06:49.490
And we give it a game object eye.

06:49.490 --> 06:57.130
And it's going to add that to your items, which is quite straightforward because it's just items don't

06:57.140 --> 06:57.830
add.

06:57.830 --> 07:08.390
And then I the next one is going to be a public game object that it returns and it will be find.

07:10.170 --> 07:17.160
Item with tag given the string tag of it.

07:19.040 --> 07:19.400
Right.

07:19.400 --> 07:22.920
So to find the item in here, we'll just loop through it.

07:22.940 --> 07:28.760
So for each game object I in items.

07:30.650 --> 07:35.480
If I don't tag equals tag.

07:36.520 --> 07:39.130
Then we're just going to return it.

07:41.250 --> 07:42.990
Return I.

07:43.350 --> 07:50.940
Otherwise we're going to finish our full loop not having found it and will return a null so that we

07:50.940 --> 07:53.550
know that that item is not in our inventory.

07:53.580 --> 07:56.310
Finally, we're going to have a remove.

07:56.310 --> 08:00.510
So we've got the ability to put it in inventory.

08:00.510 --> 08:02.220
Now we have to get it out.

08:02.220 --> 08:02.820
So.

08:03.030 --> 08:04.830
Public void.

08:05.130 --> 08:05.970
Remove.

08:05.970 --> 08:06.870
Item.

08:07.830 --> 08:10.530
Game object.

08:11.700 --> 08:12.660
I.

08:15.190 --> 08:20.230
And this is we need to, first of all, find the index to that particular item.

08:20.230 --> 08:29.860
So index to remove equals minus one, which means we actually haven't found that item for removing for

08:29.860 --> 08:30.760
each.

08:33.410 --> 08:35.120
Game object.

08:38.610 --> 08:48.360
Or in items then going to set the index to remove equals.

08:50.000 --> 08:51.850
Plus plus to increment it.

08:51.860 --> 09:01.520
And then if I equals the item that we pass through, which is an AI, which is a bad idea to call them

09:01.520 --> 09:02.090
that isn't it.

09:02.090 --> 09:09.560
Let's call this one G for game object and then let's put G down here instead.

09:09.650 --> 09:16.640
And if that's the case, then we're going to break out with the index set for the one that actually

09:16.640 --> 09:17.930
just matched and then we'll go.

09:17.930 --> 09:31.190
If index to remove is greater than or equal to minus one, then we can do an item's dot remove at.

09:32.620 --> 09:35.290
Index to remove.

09:36.590 --> 09:42.200
Now we need to do it this way where we loop through the whole thing first and then remove it using its

09:42.200 --> 09:43.070
index afterwards.

09:43.070 --> 09:48.410
Because lists do not like to have things taken out of the middle of them when you're looping through

09:48.410 --> 09:48.650
them.

09:48.650 --> 09:50.810
So that's why we've done it like this.

09:50.930 --> 09:54.410
Okay, so that's our G inventory, so save that.

09:54.770 --> 09:59.330
Now that all agents are going to require access to the inventory.

09:59.360 --> 10:03.170
Go back into your G agent CSS script.

10:03.170 --> 10:08.780
And at the top we're going to add in a new item which will be our inventory.

10:08.780 --> 10:19.730
So public G inventory inventory equals a new G inventory, right?

10:19.730 --> 10:25.670
So now all of our agents, the patients and the nurses will have access to put things in the inventory

10:25.670 --> 10:28.160
and check that things are in the inventory.

10:28.670 --> 10:34.760
Now, the question is, when do we get hold of the cubical and put it into our inventory?

10:34.850 --> 10:41.330
You don't want it in there all the time because the cubicles can only be owned by one patient at a time

10:41.330 --> 10:44.150
or one sort of nurse patient pair.

10:44.480 --> 10:49.910
What we're going to do in this particular case, and this is not the only way that you could do it,

10:49.910 --> 10:58.790
but first of all, we're going to have the nurse before it even starts the get patient plan, and it

10:58.790 --> 10:59.780
will become a plan.

10:59.780 --> 11:03.410
Currently, it's only one task that we've got in there.

11:03.500 --> 11:06.410
We're going to add a second precondition.

11:06.410 --> 11:13.610
So we don't even want the plan to be able to start until there's also a free cubicle.

11:13.610 --> 11:18.440
So add free cubicle into there.

11:18.440 --> 11:24.560
Make sure your spelling is correct, exactly the same spelling as you end up seeing up here when the

11:24.560 --> 11:26.810
cubicles are added to the world state.

11:27.050 --> 11:37.280
So now we've got this condition that we know there's a free cubicle in the world when the patient is

11:37.280 --> 11:38.780
actually running.

11:38.960 --> 11:47.810
Now the agent has an inventory, then the actions can get hold of that inventory from the agent.

11:47.810 --> 11:52.310
But you'd have to do a get component call to get hold of it.

11:52.310 --> 11:59.030
So what I want to do in this G action class is actually just grab hold of it in here as well so that

11:59.030 --> 12:07.430
we can refer to it without having to write such long code or go to the optimized version of running

12:07.430 --> 12:10.040
get component every time we need to.

12:10.190 --> 12:14.480
So it's G inventory inventory in our actions.

12:14.480 --> 12:18.920
And that's just going to point back to the agent that has it.

12:18.920 --> 12:24.140
So up in here, when we're constructing our action, we're going to add into there.

12:24.140 --> 12:31.460
Actually, I'm not doing it in the constructor, I'm doing it in the awake and in here we'll have inventory

12:32.450 --> 12:36.680
equals this dot get component.

12:37.610 --> 12:40.040
Which will be our agent.

12:41.360 --> 12:43.190
DOT inventory.

12:44.090 --> 12:44.450
Okay.

12:44.510 --> 12:50.990
So that's going to make it a lot easier to work with and go to get patient.

12:50.990 --> 12:52.550
So open that up.

12:52.760 --> 12:58.190
And the first thing we're going to do is get hold of a resource.

12:58.190 --> 12:59.780
And I've actually already started this code.

12:59.780 --> 13:01.300
You can see it there.

13:01.310 --> 13:04.610
The resource is going to be whatever you want to put into your inventory.

13:04.610 --> 13:09.910
And in this case, in get patient, it happens to be the cubicle.

13:09.920 --> 13:16.580
So we're going to get the cubicle after the target has either succeeded or fail because if we don't

13:16.580 --> 13:19.220
get the target, we don't need a cubicle.

13:19.220 --> 13:30.860
So let's go down here and add in resource equals g world dot instance dot remove cubicle and that will

13:30.860 --> 13:37.190
give our resource a pointer to the cubicle that's first in that queue.

13:37.220 --> 13:40.370
Now we also want to test if it's null or not.

13:40.370 --> 13:48.950
So first of all, we will say if resource does not equal null, then we're going to add it to the nurses

13:48.950 --> 13:49.700
inventory.

13:49.700 --> 13:53.990
So inventory dot add item.

13:56.010 --> 13:57.060
Resource.

13:58.110 --> 14:03.510
And if it's null, if resource equals null.

14:04.420 --> 14:06.490
Which I guess could be an L statement, couldn't.

14:06.490 --> 14:11.500
It makes more sense, because if it's not null, it's going to be null.

14:11.840 --> 14:14.140
Okay, so let's just get rid of that else.

14:15.070 --> 14:19.450
If it happens to be null in this case, then g world.

14:21.170 --> 14:26.150
Dot instance, dot add patient.

14:27.940 --> 14:28.780
Target.

14:29.110 --> 14:35.290
So what we're doing here is if we can't get hold of the cubicle, we're also going to release the patient

14:35.290 --> 14:43.000
because we don't need the patient anymore and the whole pre perform is actually going to fail.

14:43.090 --> 14:51.010
So we will also set target here to equal now and then we're going to return a false.

14:53.240 --> 15:00.710
Now, if we get beyond this else and we're down here, it means that we are, in fact, have a true

15:00.710 --> 15:01.400
state.

15:02.520 --> 15:05.850
We had a free cubicle, we took away a cubicle.

15:05.850 --> 15:08.970
So we need to affect the world state as well.

15:08.970 --> 15:09.540
So.

15:09.570 --> 15:10.800
G world.

15:11.670 --> 15:13.140
Dot instance.

15:13.950 --> 15:15.450
Dot get world.

15:17.240 --> 15:18.860
Dot modify state.

15:20.800 --> 15:30.640
And in here we'll be free cubical and we're going to do modifier of minus one.

15:31.000 --> 15:33.430
So what's that going to do?

15:33.460 --> 15:40.030
Well, it's going to reduce the count that's on that world condition by one.

15:40.060 --> 15:46.390
Now, remember, back in Modify State in this particular example, we've got it so that if it gets to

15:46.390 --> 15:50.800
zero, it actually removes the state completely out of the environment.

15:50.800 --> 15:57.160
So therefore, when you run out of cubicles, there won't be any cubicles sitting in the state because

15:57.160 --> 15:59.710
we're taking them away as we pick them up here.

15:59.740 --> 16:02.590
Now, what's going to be on the post perform?

16:02.590 --> 16:08.860
Well, if we've got through and we've successfully done the action, then on the Post perform, we want

16:08.860 --> 16:11.470
to take away a waiting patient.

16:11.470 --> 16:13.300
So G world.

16:14.020 --> 16:17.950
Taught instance don't get world.

16:19.480 --> 16:23.740
The Modify State and in this case its waiting.

16:24.910 --> 16:26.770
And minus one.

16:29.210 --> 16:37.010
And then we're also going to say if we have a target, which in this case is our agent that we've headed

16:37.010 --> 16:42.350
to in the waiting room, then target dot get.

16:43.240 --> 16:44.620
Component.

16:47.110 --> 16:49.300
G agent.

16:51.390 --> 16:56.190
Dot inventory dot add item.

16:58.760 --> 16:59.330
Resource.

16:59.330 --> 17:09.980
So we're adding the cubicle to the agent, the patient's inventory, so the nurse finds the cubicle.

17:10.070 --> 17:18.590
And then if it finds if the nurse finds the patient, then the nurse gives the patient access to the

17:18.590 --> 17:19.100
cubicle.

17:19.100 --> 17:27.020
So now they both have the same cubicle in their inventory, which means that we can now go ahead and

17:27.020 --> 17:35.840
create the actions where both of our agents head over to that cubicle and we'll come back and do that

17:35.840 --> 17:37.040
in the next video.
