Welcome back.
Now we have a projectile spell and a projectile, but our projectile really doesn't do anything except
fly through the air.
So in this video, we're going to make sure something happens when it hits something.
So for that, we're going to go into actor and or a projectile and we'll open up the CPP and header
files and I'm done with my combat interface.
Now, yes, we're going to want to cause damage and I want to use a gameplay effect for that and we'll
get to that.
But for now, I'd like to at least make sure that we take care of the cosmetics involved with a projectile
hitting something that's going to involve a at least effect Niagara system and a sound.
So we're going to have a couple of variables on the projectile for those things and those can be private.
So we're going to have a T object pointer and this will be for our Niagara system.
So I'm going to use you Niagara system.
Now, I'd like to include a forward declaration up there for Niagara system and I'm going to call this
impact effect.
Now we're using you Niagara system.
So I'd better go to Aura, build X and add Niagara to my dependencies here.
So I can go ahead and add that right here to my private dependency module names Niagara like so, so
I can go ahead and save that, close it out and we have an impact effect.
Now my impact effect is going to need a new property.
And we want to set this from the blueprint.
We'll make it edit anywhere.
Now, in addition to the impact effect, I want a sound, so I'm going to use T object pointer of type
u sound base.
Use sound base will allow me to use any type of sound that I want, whether it's a sound cue or a meta
sound.
We're going to use meta sounds.
So I'm going to call this impact sound and again, add it anywhere.
So when do we play these?
Well, obviously when our projectile is destroyed or when it overlaps with something, that's one circumstance.
And we're going to want to destroy the projectile when it hits.
Right?
So here in our on overlap function, on sphere overlap, currently nothing's happening.
But what I'd like to do is play the sound and spawn the effect.
So to play the sound, we use gameplay statics so I can use you gameplay statics play sound at location.
Now we're going to need to include gameplay statics.
So let's get that up here include and it's in kismet slash gameplay statics.
Not sure why Ryder didn't give me autocomplete there.
Now this function requires a world context object.
We'll pass in this.
It also needs the sound we'll pass in impact sound.
After that, we need the location, so we'll go ahead and use our actor location, get actor location.
It needs a rotation.
We'll just go ahead and use F rotator zero rotator.
And after that it has a whole bunch of optional parameters we don't need to pass in.
So that takes care of playing the sound.
We also want to spawn a Niagara system and for that we use use Niagara function library and we'll use
spawn system at location.
Now for this, it's very much like spawn sound at location.
We pass in a world context object.
We pass in the impact effect for the system and we need a location and a rotation.
In fact, we don't necessarily need a rotation, we just need the location and the rest are optional.
So we're going to use get actor location.
So this happens on overlap.
And if we're on the server, I'd like to destroy the projectile, but that's something the server I
think should handle.
So we're going to check if has authority and if we do have authority, we're going to destroy.
Now, there's a very unlikely event that this act of destruction could replicate down to the client
before the client has had the chance to have its on sphere overlap function called, in which case the
projectile would be destroyed before it could go ahead and play the sound and spawn the system.
So what we can do here is an on sphere overlap.
If we overlap before being destroyed on the client, what we can do is we can set some kind of boolean
and say we've already taken care of these effects.
And then once the act of destruction has replicated, we can check that boolean and see if we've already
played those effects.
And if not, we can play them.
So here's what I'd like to do.
I'd like to have a Boolean.
It can be private, a bool called Be Hit.
We'll set it to false and in or a projectile if we don't have authority.
So we'll have an else case here we're going to set be hit equal to true and then we can override a function
that gets called when our actor is destroyed.
That's a public function.
We can say virtual void destroyed and we can override destroyed.
So before calling super destroyed, we can check if B hit.
And we want to play the sound and spawn the system.
If destroyed is called before on sphere overlap, in which case be hit is going to be false.
So we're going to say if not be hit.
But also this only really pertains to clients.
So we're going to say if not be hit and not has authority.
And in that case, we'll go ahead and play the sound and spawn the system.
So on sphere overlap will happen on the server.
We'll get those cosmetic effects and then we'll check for authority.
We'll have it and we'll call destroy and that's it.
And on the client, one of two things will happen.
First, either on sphere overlap will happen first or the act of destruction will replicate down to
the client.
And that will happen first.
Now let's just say that destruction replicates before the clients on sphere overlap.
Well, in that case, be hit will be false and has authority will be false.
So we'll play those effects and the actor will be destroyed and on sphere overlap will never be called.
But in the case that on sphere overlap is called before destruction replicates, then we get those effects.
We check for authority and fail.
We get into the if statement and say be hit equals true, so we'll get those effects regardless.
Now on sphere overlap should not be called after destroyed has been called.
So now we should be able to see and hear some effects when our projectile hits something and we see
that it's set to overlap world, dynamic, world static and pawn.
So why don't we go ahead and compile and launch and see if we can find some assets to use?
Okay, so let's find our projectile.
Blueprint in blueprints.
And I believe this is an ability system.
Gameplay abilities.
Fire firebolt.
And it's our firebolt.
Here's our projectile.
Now we have a couple of assets to set here.
We have impact, effect and impact sound.
Now let's look at our assets folder.
We'll go to Effects projectiles.
And here we have a fire explosion.
There's two of them.
Let's check out one of them.
Here's what it looks like.
And we have a second one.
Here's what that looks like.
So they both look pretty good.
So we can choose one.
I'm going to browse to one of them.
I'll actually select the second one.
And here for impact effect, I'll set it there.
Now for the impact sound.
Let's see what sounds we have in assets.
We'll go to assets, sounds, abilities, firebolt.
And we have some sound effects.
Here's one for launching.
Here's SFX Firebolt impact and then we have a hiss.
And that's a looping sound that we can attach to our projectile.
But for now, let's use our impact sound.
I'm going to select it.
And set it there.
And now we can just test this out.
So I'm going to press play.
I'm going to click on an enemy.
And it looks like we hit something on the way and we hit something and the sound effect has an attenuation
set for it as well.
So it's going to sound quieter the farther away it is.
Now, I think we hit a potion.
Yeah, I think we hit a potion and that actually consumed the potion, funnily enough.
So we'll have to configure our collision settings exactly how we want, but at least we see that we've
got our effect for when our fireball hits something and it looks like it's working.
Excellent.
Now, we saw that we had another firebolt sound called SFX Firebolt.
We can use that in our attack animation.
So what I'd like to do is go to aura animations abilities.
I'm going to go into my cast firebolt montage and play a sound in here.
So I'm going to go ahead and add a new notify track called Sound and right here I'm going to add a notify
play sound.
And select SFX Firebolt.
Like so and saving all.
I'm going to try it out.
First on the client.
Nice.
And on the server.
Nice Looking good.
And the last thing to do is to spawn a sound effect and attach it and make sure it's looping.
We can attach it to the firebolt itself so we can have a looping sound.
And that's going to be your quest.
So for this quest, you're going to add a looping sound for your projectile.
So add the looping sound, take care of what you need to take care of in C plus plus and in blueprint,
choose a looping sound effect.
And when you're done, play test and make sure that it looks and sounds great.
So pause the video and conquer this quest now.
All right.
I'm going to close the editor.
And go back to Aura Projectile.
Now we're going to want a looping sound.
So I'm simply going to copy impact sound and paste another one.
And this will be our looping sound.
Now we want to spawn a sound and attach it.
So if we go back to Aura Projectile, we can go into Begin play and spawn a sound here with you.
Gameplay Static's spawn sound, but we can spawn one attached.
So this allows us to spawn the sound and attach it.
And this function returns a sound component that we can store so that we can stop the sound later.
So first we need to pass in the sound.
That's going to be our looping sound.
Next, we need a scene component to attach to.
I'm just going to call get root component so we can attach it to the root.
Now it takes a number of optional parameters.
We can literally leave them all out and spawn sound attached will spawn the sound.
Now it returns a new audio component.
I'd like to store that in a variable so we can stop the sound in on sphere overlap.
So let's get a u audio component pointer variable.
So this will be a T object pointer of type u audio component called looping sound component and this
will get its own U property.
There we go.
So spawn sound attached and similar functions in gameplay statics spawn a sound and return a component.
So this is one of those few cases where we're creating a component that doesn't live on an actor.
Usually components live on actors, but this one does not.
It's just a component floating around in the world.
But we can store that in our audio component, which we called looping sound component.
So we'll store that.
And then in on sphere overlap as we're playing the sound for impact and spawning the system for impact,
we'll go ahead and stop the sound.
So looping sound component we're going to call stop and when destroyed, we'll call stop here as well.
Let's test this out.
So let's open our actor.
And we'll search for looping.
Here's our looping sound.
We'll set it to SFX Firebolt Hiss.
And let's see how this works.
We'll try first on the client.
And I hit something right away.
I'll try on the server now.
So it looks like the sound is stopping.
Let's make sure that it's actually playing though.
Oh, yeah, we can definitely hear that.
That sounds great.
And that fireball just went off into the distance and it's never going to die.
So we should probably set the life span on our actor.
So before we wrap up, I'm just going to go ahead and set a lifespan.
I'll make this a uproperty variable, a float called Lifespan.
I'll set it to 15 seconds.
I think that's plenty and make it edit anywhere.
I'll make it edit defaults only actually.
And as soon as we spawn our projectile, I'm going to set its lifespan right here and begin play.
We'll call set lifespan to lifespan so that way our projectiles will only last for 15 seconds maximum
if they don't get destroyed before that.
Okay.
Great job.
Things are looking awesome.
They're sounding great.
We're now just about ready to start causing damage with our projectiles.
So we'll do that in the videos to come.
Great job and I'll see you soon.