Welcome back.
In this lecture, we're going to make our weapons full auto.
This is going to involve a sort of fire timer as well as a specified amount of time that we have to
wait before the next round gets fired.
So a fire delay is in order here.
So let's get started.
Now we're going to go into our combat component class.
This is where we handle firing the weapon.
Now we can take a look at what's happening when we fire the weapon.
I'm going to go into combat component that C.P and I see I still have a comment here in my includes
I'm going to remove that and I searched for fire button pressed and here is where we're tracing under
the crosshairs and calling server fire.
Now we're tracing under the crosshairs every frame.
So really we could get away with just passing in our hit target here.
Hit target and we can remove the line trace here.
It's not too important.
We just know that we're already tracing every frame.
And just to show you, we can grab a weapon and fire the weapon.
We see that it's working properly.
Now we're calling server fire, but we'd like to start a timer here and at the end of that timer call
server fire again, at least if fire button is still pressed.
So let's add a fire timer handle.
We're going to go down to the private section down here at the bottom.
I'm going to add a multi-line comment here and say automatic fire and I'd like an EF timer handle called
fire timer and a float called Fire DeLay.
Now, the float can be a new property and I'd like it to be added anywhere and I'll stick it in the
category combat.
So the reason I'd like it to be added anywhere is because we'll have multiple different weapon types
and we can set fire delay on the weapon blueprint to control how fast the automatic fire works for the
weapon.
Now we have a fire timer.
We'll need a function callback for when the timer has finished.
So I'll make a void function called fire timer finished.
And to make things nice and neat, I'm going to have another function called Start Fire Timer.
So we'll handle setting the timer and start fire timer and fire timer finished will be the timer callback.
So let's define our functions.
So now we have function definitions for these.
And start fire timer is simply going to start the fire timer so we can check to make sure we have an
equipped weapon first.
So if that's null pointer, we'll return.
Now to set a timer, we'd like to call get world timer manager, which will return the world timer manager.
But we need to have it include here so we can use that type.
And that include is simple.
It's just timer manager, not age.
So we're going to include timer manager that age and now we can use the timer manager, but this world
timer manager function exists on the character.
So we need to call character get world timer manager, which means we also need to make sure character
isn't null here.
So we'll say equals null pointer and if it's null will return.
Now with the world timer manager, we can call set timer here and set timer takes the timer itself,
which is our timer handle called fire timer.
So we'll pass in fire timer.
And the overload I'd like to use takes the user object next followed by the function callback, which
is going to be you combat component fire timer finished.
And it needs the fire delay.
And we have a float called fire delay.
So we're going to pass and fire delay.
So that's all our start fire timer function is going to do.
Now our fire timer finished function needs to check to see if we still have our fire button pressed.
So we're going to say if be fire button pressed and if this is true, we'd like to fire the weapon.
Now, let's take a look at fire button pressed again here.
If we are pressing the fire button we're calling server fire and we're also checking if we have an equipped
weapon and setting the crosshair shooting factor.
So this would be a good chunk of code to refactor into a simple fire function.
So I'm going to highlight these lines.
Right click quick actions and refactoring and extract function.
And I'm going to call this function fire and click.
Okay, so we have now fire and we're calling that instead of doing multiple things here.
And fire will simply call server fire and set our crosshair shooting factor.
So now we have a nice simple function and we can go back to fire timer finished.
And here a fire button is still pressed.
We can simply call fire.
So this makes our code quite a bit simpler.
Now, we're never actually calling Start Fire Timer.
We should call that as soon as we fire the weapon.
So let's go into our fire function.
And after we call server fire and we check our equipped weapon, we're going to call start fire timer.
I'm going to go ahead and remove that extra line.
And now this is a bit more compact.
Now, all of this assumes that our weapon is automatic.
So as long as we're holding the fire button down, all weapons will continue to fire.
Once that fire delay has been reached, but not all weapons are going to be automatics.
So we need a boolean that we can set to enable automatic fire.
So let's make a ball here.
So ball be automatic.
Now this can also be added anywhere.
Category combat and fire delay and be automatic should have default values for fire delay.
I'm going to set it 2.1, five and four.
Be automatic.
I'm going to set it to true by default.
So we have to actively uncheck this in blueprints to make this a non automatic weapon.
Now we need to check, be automatic.
So let's go to our callback called Fire Timer.
Finished here and fire timer finished.
We're checking if fire button is still pressed and then firing again.
But we should only fire again if this is an automatic weapon.
So we're going to check and be automatic as well so we won't automatically fire unless we actually are
an automatic weapon.
Now, another thing I'd like to do is have a boolean for whether or not we can fire, and that way we
can't just spam the fire by and then fire as fast as we want.
We would have to wait for our fire timer finished to allow us to fire again.
So I'm going to add another boolean.
This one doesn't need a new property.
It's just going to be a bull called be can fire.
And we'll set it to true by default.
And as soon as we fire, we're going to set Beacon Fire to false.
So let's go into the fire function and we'll check to see if we can fire here.
So if we can fire, then we'll do all of this stuff.
Now, we also need to set Beacon Fire to false here.
That way we can't fire by spamming the fire button.
So we're going to say be can fire equals false.
And we're not going to set this back to true again until our fire timer has finished.
So here in fire timer finished Mozambique and fire oops I control B instead of control v now it's compiling
but we'll set Beacon Fire two true here and we're going to set it to true regardless of whether our
fire button is pressed or we're in automatic weapon.
That way, if we're a semi-auto or a non automatic weapon, we're still going to set Beacon Fire to
true after our fire delay.
So there's a mandatory delay for non automatic weapons that we have to wait before we can fire again
and then we can't just spam as fast as we can click.
So let's go ahead and compile our code.
So if we press play and we can grab a weapon.
And automatic fireworks.
Now we placed all these variables on the combat component, and I was mentioning that some of these
should be set on the weapon, so we really should be adding be automatic and fire delay to the weapon
itself.
So we're going to go into weapons at age.
So I'm going to take fire, delay and be automatic and copy those and go into a weapon that age.
And I'm going to stick these up here in the public section.
So I'll make another comment here.
And this will say automatic fire and I have fired away and be automatic.
So instead of having these on the combat component, these will be on each individual weapon and they
can change from weapon to weapon.
So in combat component age, I'm going to remove be automatic and fire delay and we'll go into combat
component here and we'll check be automatic on the weapon.
So let's start with start fire timer.
We're checking if equipped weapon is null.
And since we know it's not, we're going to get fire delay from equipped weapon.
So we're going to say equipped weapon.
Fire delay.
And we're passing that in to set timer now as soon as the timer is finished.
We're checking be automatic.
So I'm going to copy this line up here and paste it right here.
And fired timer.
Finished.
We don't really need to check the character, just the equipped weapon.
And we're going to get that be automatic variable from the weapon itself.
Okay, so this is all set up, but I'm noticing my functions are sort of separated from each other.
So I'd like to have similar functions together.
So trace under crosshairs.
I'm going to minimize that and minimize server fire and multicast fire and minimize equipped weapon.
This way I can sort of see where everything is.
Now I'm going to take trace under crosshairs and move it down here at the bottom.
Now, as soon as I move it, of course it expands itself again.
And server fire and multicast fire can be right here under fire.
I'm okay with that and fire button pressed.
It makes sense for that to be right above fire.
But we also have these fire timer functions, and I think these should be right next to the fire functions.
So I'm going to take start fire timer and fire timer finished and I'm going to cut these.
And place them here right under fire.
And now if I minimise these, I see that my fire functions are all together.
Now unwrap equipped weapon should be right under equipped weapon.
So I'm going to make room here and move this function down there.
So I'm going to simply cut it.
And pasted under equipped weapon.
Now we have set aiming and server set aiming.
Those are together.
I'm happy with that.
And we have interpreted for V, which we're using to interpolate our field of view, which has to do
with aiming.
So that's okay.
And we have set HUD crosshairs, so interrupt F of V and set HUD crosshairs along with our function
trace under crosshairs.
These are things that we're doing every frame.
So what I like to do is take these four functions set aiming service at aiming in type F of V and set
HUD crosshairs and cut those and put them down here towards the bottom.
So we have all of our per frame functions that we're calling and tick down here towards the bottom.
So that looks good.
And now at the top, we have the constructor.
Get life time replicated props begin play and tic component.
I'm okay with those being at the top there.
So now we're a little more organized.
So now I'm going to compile.
And back here in the editor, I'm going to open up my weapon folder and open BP assault rifle.
And here in our weapon blueprint, we have a combat section now with fire delay and automatic.
So let's see how these work.
Now, I can run up to a gun on a client.
Pick it up and fire.
And I'm holding the mouse button down.
And I have automatic fire and I can spam the left mouse button as fast as I can.
And I'm not shooting faster than regular automatic fire because of the fire delay.
So this is great.
I can run up to a weapon on the server now.
And I see that automatic fireworks here as well.
Now we can uncheck automatic for this weapon.
And now if I grab a weapon and I hold the left mouse button down.
I just fire once.
And if I spam the button.
I cannot fire faster than fire delay.
Great.
Now, lastly, we can change the fire delay itself.
Let's say 0.1 5 seconds is not fast enough.
I can set it 2.1 and I can grab a weapon.
And we're firing quite a bit faster.
Awesome.
For the assault rifle, I'm okay with 0.15, so I'm going to leave it at that value compile and save.
So in this lecture we converted our weapons to full auto and we also added a boolean that we can check
or uncheck easily on our weapon blueprints to convert them back to semi-auto.
So that way we don't have to use a different class just for changing a weapon from non automatic to
automatic.
And we also have a fire delay that we can change on the blueprint to alter how quickly we can fire the
weapon.
We did all this using a timer which we're setting as soon as we fire a weapon and at the end of that
timer, then we're firing the weapon and we refactored some of that fire code into a nice little fire
function that handles firing for us.
Great job.
We now have automatics.
I'll see you in the next lecture.