Welcome back.
Now our loot is looking pretty nice.
If we spawn our loot in, we have some nice dynamic effects for those potions and those crystals and
they look great.
Now the only thing missing is sound and we're going to handle that in this video.
Now the Aura Effect Actor C plus plus class really could have a spawning or creation sound, and it
can have a consume sound.
But as far as hitting the ground, that's something that pickups specifically have.
So that could be something in our pickup base class.
And we can handle that right here in blueprint.
First thing we can add a new variable.
We can call this ground impact sound.
And we can set this to sound base.
Object reference and we can take a look at content assets.
Sounds.
Potions.
Look at that.
We have potions and we have hit ground.
And here's a sound.
So I'm going to take this and go to pick up base and set ground impact sound to this.
And we know when we hit the ground, it's when this particular timeline has finished, or at least when
it hits about this part right here.
This is point eight in the graph.
And we can even just check this elevation curve.
We could see if it's point eight and just play a sound.
If that's the case, by moving these things out a bit, moving these nodes down a bit and checking if
elevation is greater than point eight, and if it is, we can play a sound.
Now I want to use a do once.
That way we'll only play the sound once.
And on update, we'll check the branch and whether this is true or false.
And we should probably actually do this after setting actor scale 3D.
So here's what I'm going to do.
I'm going to take elevation and promote to a variable.
We'll call it elevation.
Let's keep this clean here and we'll use elevation here.
See if it's greater than 0.8 and use a do once all the way at the end here.
That'll give us a lot less messiness.
And we can plug elevation in here as well and avoid crisscrossing that wire there.
So we just have one little cross.
We can even promote scale.
Let's just promote scale.
And get rid of this one as well.
That's going to be.
Annoying otherwise.
There.
That's much better.
Now.
We can hook these up.
And after doing these each update, then we're going to check.
We're going to see.
If elevation is greater than 0.8, and if so, we'll use a do once.
So we only ever do this once and we'll play sound at location.
We'll use get actor location.
For the location and we'll use our ground impact sound for the sound.
Let's try this now.
All of our pickups are going to have the same ground impact sound, so it might sound funny when those
crystals hit the ground, but of course, we could just go in there and change those sounds for those
blueprints if we wanted to.
Let's just see how this works, though.
Now, it's kind of hard to tell when those sounds are playing relative to the location.
So I'm going to go into ability system data and loot tiers and only spawn health potions.
For now, I'm going to set the spawn chance to zero for the others and only spawn a single potion.
That way I can see a little bit better when that sound is playing relative to when it drops.
Didn't see a potion.
Didn't get a potion.
So BP.
Health potion.
Chance to spawn.
Max number to spawn.
Do I have an off by one error?
I'm going to try to.
Okay, I have an off by one error.
We'll need to fix that.
But.
It looks like I'm playing the sound too early, and that's because I'm checking the value, not the
time.
It's the time that's point eight, not the value.
So the easiest way, I think, would be to check if the value is less than zero, because it only becomes
less than zero right around here, we can even go a little bit less than that value.
So we could say if elevation is less than.
We'll go to like negative.
Let's see.
Negative point.
One because we go all the way down to negative point two.
So let's see if we're down below negative point one.
And then we should play that at the correct time.
Yeah, that looks good and sounds good.
Okay, so that takes care of that sound.
Now let's fix that off by one error.
We had max number to spawn set at two.
We're going to need to see what's going on when we're spawning.
Let's go over to blueprints character.
BP enemy base.
Where we're spawning.
Right here in spawn loot item.
We're checking spawn loop count now.
We should be fine if we exit as long as spawn loop count is less than loot items length, we shouldn't
have to subtract one from this.
But we did see that we were getting errors when we didn't.
Let's see if we can figure out why we are getting those two potions.
But when are we accessing?
None.
We're attempting to access index two from array of loot items of length two.
So we're accessing the index of the third item in a two item array.
We should never try to access index two right.
So let's get that error back and see exactly where the violation is occurring.
We can figure this out if we go ahead and get some loot spawning.
Escape here and we can look at our error and it says it's on a branch.
So we're getting this branch.
We're getting the boolean value from here which is getting spawn loop count.
And it looks like we're doing this after incrementing spawn loop count.
So there's the violation.
So we actually need to increment spawn loop count after.
So that's okay.
We can do that.
But we have to increment spawn loop count regardless of whether the cast fails or succeeds, and regardless
of the value of true or false here.
And you know what?
This cast failed is enough to check the validity.
We only need to continue if the cast succeeds.
Is valid is unnecessary.
The cast will take care of that for us.
So if the cast fails, we're still going to increment the loop count and we're still going to increment
it whether this is true or false.
So all of these cases will result in the incrementing of the loop count.
Bit of an ugly bunch of nodes here.
But that's okay I'm just going to stick this right here.
And we can clean that up later, but let's make sure.
That this eliminates the error, and that we also get two potions when we should get two.
We get two and no errors.
Perfect.
Okay, so with that, we're now playing a sound when our pickups hit the ground.
But our Aura Effect actor can have an on spawn sound and a consumed sound if we like, so we can add
that right in to Aura Effect actor.
Now add that to the C plus plus class if you like.
But really I only care about pickups, so I'm just actually going to add this right here in the blueprint.
We'll play a spawn sound on Begin Play.
So we'll go ahead and add a variable.
It'll be a sound base.
We'll say spawn sound.
And I'm just going to copy play sound at location with Get Actor location and we'll get one here.
We'll use our spawn sound.
We'll compile and give this a default value and we'll go into content assets, sounds potions, spawn,
and we have a spawn sound.
I'm going to get that.
And set it here.
And then finally on consume.
We need to know when our potion is destroyed.
We can just use event.
Destroy.
Event destroyed.
And we can copy play sound at location.
And we can have a consumed sound.
We'll promote this to a variable called consumed sound.
We'll compile and we'll give this a default value as well.
And in potions we have consume.
Let's use that.
There we go.
And with that, we can test out these sounds.
All right.
They sound great.
Let's consume them.
Perfect.
That looks great.
So now I'm going to lower the chance to spawn these.
I'm going to lower the chance to spawn a health potion to 20%, and a mana potion also to 20%.
And I'm going to consider the crystals much more rare.
We'll give the health crystal and mana crystal a 5% chance to spawn two of them each, and health potion
will be three.
Max and mana will be three max.
And with that, we'll see those potions.
But not for every enemy, not every time.
So there was one.
And there was one.
Excellent.
All right.
Awesome job.
We now have some really cool looking and sounding potions and pickups.
This adds quite a bit to our game, and you can use this same technique for any types of pickups you
like.
Okay, a little bit of testing and I do get an error trying to set actor level because we're trying
to set the actor level here.
On aura effect actor.
Whether or not that actor is valid, it could be pending kill.
So setting the actor level actually needs to be done if the actor is valid, and only if the actor is
valid.
So what we can do is move this set actor level right here.
And only do it if that actor is valid.
Like so.
There we go.