WEBVTT

00:06.840 --> 00:07.930
Welcome back.

00:07.950 --> 00:11.280
Now we're calculating some spawn locations.

00:11.280 --> 00:17.670
But one important feature that I'd like is to be able to spawn my minions asynchronously.

00:17.700 --> 00:20.500
Not necessarily all at the same time.

00:20.520 --> 00:26.250
I'd like to spawn them all one after the other, perhaps, and not necessarily in order either.

00:26.250 --> 00:32.910
So in this video we're going to work on an algorithm for spawning them asynchronously.

00:32.910 --> 00:38.520
So to do that, first of all, after we get our spawn locations in our summon ability, I'm going to

00:38.520 --> 00:43.770
promote that array to a variable and I'm going to call this spawn locations.

00:43.800 --> 00:53.070
Now, after the spawn locations array is stored, I'd like to draw a debug sphere for each of them,

00:53.070 --> 00:54.960
but I don't want to use a for loop.

00:54.960 --> 00:56.850
I don't want to use a while loop.

00:56.850 --> 01:02.490
Those are all going to loop over every single element in this array all at once.

01:02.520 --> 01:06.150
Atomically if you will, all in the same frame.

01:06.150 --> 01:09.430
I want to do this over the space of multiple frames.

01:09.430 --> 01:14.380
So what I'm going to do is keep count of which of these spawn locations.

01:14.380 --> 01:18.420
I'm at a spawn location index, if you will.

01:18.430 --> 01:20.320
So I'm going to create a new variable.

01:20.320 --> 01:25.510
It's going to be an integer, and I'm going to call this spawn location index.

01:25.660 --> 01:31.780
It'll start off at zero, but I'm going to have a loop, not a while loop, not a for loop, but just

01:31.780 --> 01:36.370
a blueprint loop right here where I'm going to check first.

01:36.370 --> 01:43.420
If my spawn location index is less than the number of elements in that array, so I'm going to use a

01:43.420 --> 01:47.650
less than node and get my spawn locations array.

01:48.390 --> 01:49.590
Get its length.

01:51.270 --> 01:53.760
And take that length and pass it in.

01:55.310 --> 01:58.640
And use this as a condition for my branch.

01:59.920 --> 02:06.890
So as long as spawn location index is less than spawn locations length, then we'll continue.

02:06.910 --> 02:11.190
And for now, I'm just going to draw a debug sphere.

02:11.200 --> 02:13.300
So draw debug sphere here.

02:15.740 --> 02:18.160
And this one will be a little bit bigger.

02:18.170 --> 02:26.900
We'll make it have a radius of 25 and its center is going to be at Spawn locations.

02:26.900 --> 02:35.150
So I'm going to get Spawn locations and I'm going to get a copy at Spawn Location Index, and that's

02:35.150 --> 02:37.010
where I'm going to draw the debug sphere.

02:37.010 --> 02:45.590
So we're choosing that one and I'm going to make this a red, probably magenta color with a duration

02:45.590 --> 02:48.050
of three like so.

02:48.050 --> 02:55.250
Now after I've chosen and drawn a debug sphere, I'm going to take spawn location index and increment

02:55.250 --> 02:56.000
it by one.

02:56.000 --> 02:59.270
So I'm going to use a plus plus operator.

02:59.950 --> 03:01.690
We're going to add one to it.

03:01.690 --> 03:04.870
And after that, I'd like to repeat the process.

03:04.870 --> 03:09.460
I'd like this to loop back around all the way back to the branch again.

03:09.490 --> 03:14.980
Now, we can't tell that this is looping around, so I'm going to drag this node up, add a reroute

03:15.010 --> 03:19.900
node and drag it back down like this so we can see that it's looping.

03:19.900 --> 03:24.370
I'm going to add two reroute nodes like so and we can see that it's a loop.

03:24.460 --> 03:31.120
Now I don't want this to happen immediately, otherwise this is just a glorified while loop.

03:31.120 --> 03:34.390
I'm going to add a delay right here at the end.

03:34.420 --> 03:38.110
So a delay node, it can be 0.2 seconds, That's fine.

03:38.110 --> 03:42.100
And every 0.2 seconds we'll go back around.

03:42.100 --> 03:48.550
We'll check that spawn location index, makes sure we're not out of bounds, and then we'll pick the

03:48.550 --> 03:55.150
spawn location and draw the debug sphere increment spawn location index and start again.

03:55.360 --> 04:00.710
So let's go ahead and try this out and see what happens.

04:00.710 --> 04:01.520
And there we go.

04:01.520 --> 04:03.890
We see those magenta spheres.

04:03.920 --> 04:06.230
Now the thing is they're going in order.

04:06.230 --> 04:08.090
So it's not very random.

04:08.090 --> 04:11.570
It's going from right to left from our perspective.

04:11.600 --> 04:17.150
So if we really wanted this to look a little bit more random, we could shuffle the array.

04:17.180 --> 04:20.210
Now there happens to be a function for doing that.

04:20.240 --> 04:26.000
We can right click type shuffle and we have utilities array shuffle.

04:26.030 --> 04:29.660
This takes in an array and it's going to do what it says.

04:29.660 --> 04:31.280
It's going to shuffle that array.

04:31.370 --> 04:36.650
So now if we press play, we can see that it's no longer going in order.

04:36.650 --> 04:42.590
We're seeing those magenta spheres and it's a little bit distracting with all the other debug shapes.

04:42.590 --> 04:48.590
So at this point I'm going to go ahead and just remove the other debug shapes from our algorithm.

04:48.710 --> 04:50.570
We don't need those anymore.

04:50.600 --> 04:53.810
They're still in the commit for last lecture.

04:53.810 --> 05:00.140
So you can always go back to the GitHub and see that commit, but I don't think we need them anymore.

05:00.170 --> 05:02.990
So I'm going to go ahead and launch once again.

05:03.410 --> 05:08.180
And I'm noticing before I go back there's a couple of dead includes here.

05:08.180 --> 05:13.640
Sometimes writer auto includes things and sometimes I remove things that don't need anymore.

05:13.640 --> 05:15.260
So I'm just removing those.

05:15.260 --> 05:22.160
Next time we compile that will be there, I'm going to go ahead and open that summon ability and we're

05:22.160 --> 05:23.600
going to play test.

05:24.450 --> 05:27.270
And there we see our spawn location.

05:27.270 --> 05:36.150
So as you can see, it's a pretty nice semi-random algorithm that gives us some good looking spawn locations.

05:36.150 --> 05:39.840
We have a nice spread there, so that's looking good.

05:40.350 --> 05:43.830
Now, the thing is that we could be on a hill.

05:43.830 --> 05:47.100
We're not always necessarily going to be on flat ground.

05:47.130 --> 05:50.190
There could be stairs, there could be a ramp.

05:50.190 --> 05:58.920
And so it might be wise if we just make sure that our spawn locations are always on the ground.

05:59.070 --> 06:08.400
And for that reason we can do a line trace straight down from above on those locations and then we'll

06:08.400 --> 06:16.110
get those spawn locations and they won't necessarily be under the ground or too far above the ground.

06:16.110 --> 06:22.170
So before we wrap up, let's go ahead and just make sure that those spawn locations are exactly on the

06:22.170 --> 06:22.710
ground.

06:22.740 --> 06:27.030
So I'm going to close and come back to get Spawn locations.

06:27.030 --> 06:34.740
And each time we get a chosen location, I'm going to do a line trace from above and we're going to

06:34.740 --> 06:37.830
store the hit result of that line trace.

06:38.160 --> 06:40.480
So I'm going to call get World.

06:42.150 --> 06:45.540
Line trace single by channel.

06:45.570 --> 06:47.460
We're going to need a hit result.

06:47.460 --> 06:50.190
So f hit results hit.

06:50.310 --> 06:51.840
We'll pass that in.

06:52.590 --> 06:58.470
We're going to need a start location, and that's going to be our chosen spawn location.

06:58.470 --> 07:00.000
But we're going to add to it.

07:00.000 --> 07:02.160
We're going to add a vector pointing straight up.

07:02.160 --> 07:11.430
So F vector it's going to be zero point f zero dot f, and we'll go a nice, let's say, 500 units.

07:11.430 --> 07:12.900
That might be too much.

07:12.900 --> 07:21.530
Let's do 400 units in the air and the end is going to be the same location minus that much.

07:21.540 --> 07:24.180
So this will be well into the ground.

07:24.180 --> 07:31.410
We'll change this to a minus here, and that way we'll trace to just below that spawn location by 400

07:31.410 --> 07:32.280
units.

07:32.400 --> 07:39.300
So we have our hit, we have our start location, we have our end location, and we need a collision

07:39.300 --> 07:39.780
channel.

07:39.780 --> 07:44.830
We're going to chase against SEC, underscore visibility.

07:45.670 --> 07:47.470
So there's our line trace.

07:47.500 --> 07:57.640
Now what we'll do is if we get a blocking hit, so if hit dot be blocking hit, then we're going to

07:57.640 --> 08:02.770
change our spawn location to the hit result impact point.

08:02.770 --> 08:09.280
So I'm going to remove const from chosen spawn location and if we get a blocking hit, I'm going to

08:09.280 --> 08:16.210
overwrite chosen spawn location with hit dot impact point like so.

08:16.210 --> 08:20.530
And now our spawn locations should all be on the ground.

08:20.560 --> 08:22.180
Let's test that out.

08:22.990 --> 08:23.770
Here we go.

08:23.770 --> 08:24.880
Let's hit play.

08:25.810 --> 08:26.750
And there they are.

08:26.770 --> 08:28.420
They're all on the ground.

08:28.540 --> 08:32.370
And before they actually weren't right, they were a little above the ground.

08:32.380 --> 08:36.620
But now this works even with a nice big slope.

08:36.640 --> 08:43.150
So, for example, I'm just going to take about three of these three by three.

08:43.150 --> 08:46.570
So a grid of three by three big squares.

08:46.810 --> 08:51.610
I'm going to right click and I'm going to go to group Control G.

08:51.640 --> 08:52.680
Will do this.

08:52.690 --> 08:58.660
So now we have one big floor and I'm going to angle this floor a bit.

09:00.630 --> 09:02.550
45 degrees is a bit much.

09:02.550 --> 09:09.120
I'm going to choose a snap increment of ten and we'll do just ten for now.

09:09.330 --> 09:11.010
And let's play test.

09:12.260 --> 09:16.340
And as you can see, these are all on the ground.

09:21.250 --> 09:22.120
Perfect.

09:22.690 --> 09:26.110
I can even go into detach mode.

09:27.600 --> 09:29.640
And look at this a little closer.

09:30.910 --> 09:34.810
And we can see that we're always getting spawn points on the ground.

09:35.260 --> 09:36.100
Perfect.

09:36.100 --> 09:36.670
All right.

09:36.700 --> 09:43.450
Now, I don't really want this tilted, so I'm just going to move it back to how it was.

09:47.670 --> 09:53.130
And it's looking like I'm going to have it just off by a little bit.

09:54.210 --> 09:56.730
I'm just going to bring it up a little.

09:57.180 --> 10:00.030
It won't be perfect, but that's fine.

10:00.800 --> 10:08.480
And in fact, I can take the adjacent tile and take its location, Z, which is zero.

10:08.900 --> 10:13.480
And I can just take this one and set the location Z at zero.

10:13.490 --> 10:14.270
Perfect.

10:14.810 --> 10:16.610
Okay, so this is looking great.

10:16.640 --> 10:23.120
We now have a nice algorithm and it's asynchronous for spawn locations.

10:23.150 --> 10:30.410
Now we need to actually spawn things at those locations and that is one of our next steps.

10:30.410 --> 10:32.060
So we'll handle that next.

10:32.090 --> 10:33.260
Excellent job.

10:33.290 --> 10:34.340
I'll see you soon.
