Welcome back.
Now we're calculating some spawn locations.
But one important feature that I'd like is to be able to spawn my minions asynchronously.
Not necessarily all at the same time.
I'd like to spawn them all one after the other, perhaps, and not necessarily in order either.
So in this video we're going to work on an algorithm for spawning them asynchronously.
So to do that, first of all, after we get our spawn locations in our summon ability, I'm going to
promote that array to a variable and I'm going to call this spawn locations.
Now, after the spawn locations array is stored, I'd like to draw a debug sphere for each of them,
but I don't want to use a for loop.
I don't want to use a while loop.
Those are all going to loop over every single element in this array all at once.
Atomically if you will, all in the same frame.
I want to do this over the space of multiple frames.
So what I'm going to do is keep count of which of these spawn locations.
I'm at a spawn location index, if you will.
So I'm going to create a new variable.
It's going to be an integer, and I'm going to call this spawn location index.
It'll start off at zero, but I'm going to have a loop, not a while loop, not a for loop, but just
a blueprint loop right here where I'm going to check first.
If my spawn location index is less than the number of elements in that array, so I'm going to use a
less than node and get my spawn locations array.
Get its length.
And take that length and pass it in.
And use this as a condition for my branch.
So as long as spawn location index is less than spawn locations length, then we'll continue.
And for now, I'm just going to draw a debug sphere.
So draw debug sphere here.
And this one will be a little bit bigger.
We'll make it have a radius of 25 and its center is going to be at Spawn locations.
So I'm going to get Spawn locations and I'm going to get a copy at Spawn Location Index, and that's
where I'm going to draw the debug sphere.
So we're choosing that one and I'm going to make this a red, probably magenta color with a duration
of three like so.
Now after I've chosen and drawn a debug sphere, I'm going to take spawn location index and increment
it by one.
So I'm going to use a plus plus operator.
We're going to add one to it.
And after that, I'd like to repeat the process.
I'd like this to loop back around all the way back to the branch again.
Now, we can't tell that this is looping around, so I'm going to drag this node up, add a reroute
node and drag it back down like this so we can see that it's looping.
I'm going to add two reroute nodes like so and we can see that it's a loop.
Now I don't want this to happen immediately, otherwise this is just a glorified while loop.
I'm going to add a delay right here at the end.
So a delay node, it can be 0.2 seconds, That's fine.
And every 0.2 seconds we'll go back around.
We'll check that spawn location index, makes sure we're not out of bounds, and then we'll pick the
spawn location and draw the debug sphere increment spawn location index and start again.
So let's go ahead and try this out and see what happens.
And there we go.
We see those magenta spheres.
Now the thing is they're going in order.
So it's not very random.
It's going from right to left from our perspective.
So if we really wanted this to look a little bit more random, we could shuffle the array.
Now there happens to be a function for doing that.
We can right click type shuffle and we have utilities array shuffle.
This takes in an array and it's going to do what it says.
It's going to shuffle that array.
So now if we press play, we can see that it's no longer going in order.
We're seeing those magenta spheres and it's a little bit distracting with all the other debug shapes.
So at this point I'm going to go ahead and just remove the other debug shapes from our algorithm.
We don't need those anymore.
They're still in the commit for last lecture.
So you can always go back to the GitHub and see that commit, but I don't think we need them anymore.
So I'm going to go ahead and launch once again.
And I'm noticing before I go back there's a couple of dead includes here.
Sometimes writer auto includes things and sometimes I remove things that don't need anymore.
So I'm just removing those.
Next time we compile that will be there, I'm going to go ahead and open that summon ability and we're
going to play test.
And there we see our spawn location.
So as you can see, it's a pretty nice semi-random algorithm that gives us some good looking spawn locations.
We have a nice spread there, so that's looking good.
Now, the thing is that we could be on a hill.
We're not always necessarily going to be on flat ground.
There could be stairs, there could be a ramp.
And so it might be wise if we just make sure that our spawn locations are always on the ground.
And for that reason we can do a line trace straight down from above on those locations and then we'll
get those spawn locations and they won't necessarily be under the ground or too far above the ground.
So before we wrap up, let's go ahead and just make sure that those spawn locations are exactly on the
ground.
So I'm going to close and come back to get Spawn locations.
And each time we get a chosen location, I'm going to do a line trace from above and we're going to
store the hit result of that line trace.
So I'm going to call get World.
Line trace single by channel.
We're going to need a hit result.
So f hit results hit.
We'll pass that in.
We're going to need a start location, and that's going to be our chosen spawn location.
But we're going to add to it.
We're going to add a vector pointing straight up.
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.
That might be too much.
Let's do 400 units in the air and the end is going to be the same location minus that much.
So this will be well into the ground.
We'll change this to a minus here, and that way we'll trace to just below that spawn location by 400
units.
So we have our hit, we have our start location, we have our end location, and we need a collision
channel.
We're going to chase against SEC, underscore visibility.
So there's our line trace.
Now what we'll do is if we get a blocking hit, so if hit dot be blocking hit, then we're going to
change our spawn location to the hit result impact point.
So I'm going to remove const from chosen spawn location and if we get a blocking hit, I'm going to
overwrite chosen spawn location with hit dot impact point like so.
And now our spawn locations should all be on the ground.
Let's test that out.
Here we go.
Let's hit play.
And there they are.
They're all on the ground.
And before they actually weren't right, they were a little above the ground.
But now this works even with a nice big slope.
So, for example, I'm just going to take about three of these three by three.
So a grid of three by three big squares.
I'm going to right click and I'm going to go to group Control G.
Will do this.
So now we have one big floor and I'm going to angle this floor a bit.
45 degrees is a bit much.
I'm going to choose a snap increment of ten and we'll do just ten for now.
And let's play test.
And as you can see, these are all on the ground.
Perfect.
I can even go into detach mode.
And look at this a little closer.
And we can see that we're always getting spawn points on the ground.
Perfect.
All right.
Now, I don't really want this tilted, so I'm just going to move it back to how it was.
And it's looking like I'm going to have it just off by a little bit.
I'm just going to bring it up a little.
It won't be perfect, but that's fine.
And in fact, I can take the adjacent tile and take its location, Z, which is zero.
And I can just take this one and set the location Z at zero.
Perfect.
Okay, so this is looking great.
We now have a nice algorithm and it's asynchronous for spawn locations.
Now we need to actually spawn things at those locations and that is one of our next steps.
So we'll handle that next.
Excellent job.
I'll see you soon.