Welcome back.
In the previous video, we downloaded this level so we can have something a little bit more pleasing
to look at while we're developing.
So I'm going to find our player start.
When I was selected and had f to focus on it, and I'm going to go ahead and just delete this one.
And we're going to find a nice, quiet area that we can spawn our character, and so I found this little
office area and I decided to place the players star here.
So once we start playing, we'll have this area and there's still some ambient noise, so I went ahead
and stopped recording the desktop audio so that you can just hear my voice and you don't have to worry
about the noise.
But for now, this will be a nice place for us to spawn when protesting, OK, so in this video we're
going to implement automatic fire.
So when we press and hold the trigger, which is the left mouse button or the right trigger on the console
controller.
Rather than shooting once, we're going to want to shoot many times very rapidly like an automatic weapon
would do.
To do that, we're going to make use of timers.
Let's get started.
Here we are in shooter character Don H and I'm going to scroll down to the private section where we
can add some private variables.
Down here at the bottom, I'm going to want a boolean for when our fire button is being pressed.
So I'm going to create a book called B Fire Button Pressed.
For the comment.
I'll say left mouse button or write.
Console trigger pressed.
Now, I'm also going to create a boolean called Be Sure-fire, and this will be a boolean that will
use to know whether or not we should be firing a bullet.
In other words, as soon as we fired a bullet, we're going to start a timer.
And during the duration of that timer, we should not be firing a bullet.
But as soon as the timer is over, we can set this ball back to true so that we can fire again.
So we'll say true when we can fire.
False when waiting for the timer.
Now we want to know how often we'd like to fire the weapon, so we're going to create a float called
automatic fire rate and this will tell us how many seconds should pass in between each bullet fire.
So we're going to say rates of automatic gunfire.
And finally, because we're using timers, we're going to create enough time to handle and we'll call
this auto fire timer.
And for the comment, we'll say sets a timer between gunshots.
Now we're going to want to create functions that will set this fire button pressed boolean and we're
going to bind these to the fire button.
So let's create fire button pressed and fire button released functions and we'll stick these up in the
protected section.
The first one will be void fire button pressed.
And the next will be void, firemen released.
These function names are self-explanatory, so I'm not going to give these comments.
Let's create function bodies for these.
Intelligence always has to think when it creates the second one.
OK, so we have the function definitions and these are simply going to set the Boolean B fire button
pressed to true for fire button pressed and B, fire button pressed to false.
For fire button released and now we can go down to set up player input component and we can take our
fire button, find action, function, call, and instead of calling fire weapon, we're going to call
fire button pressed.
So now pressing the left mouse button or the trigger on the console no longer results in fire weapon
being called, now we're simply setting this boolean to true.
So we're going to have to handle firing the weapon in a different way.
We're going to copy this function, call and paste it.
And instead of IHI pressed, we're going to use IHI released.
And we're going to call fire and released when the button is released.
OK, so we're going to be setting timers, so we'd like to have a function that can start the timer,
so let's create a void function called start fire timer.
And we're also going to want a function that gets called when the timer is complete for this, we're
going to create a function called void auto fire reset.
And because it's going to be a callback for a timer, it needs to be a new function.
So let's create bodies for these as well.
Now for Auto Fire reset, all we're going to do is set B should fire to true.
Now, B should fire is the bullion we're going to check to see if we should be firing the weapon if
it's false.
We will not fire.
So here's how it's going to work now.
As soon as we call Start Fire timer, we're going to check to see if we should fire.
So if we should fire.
If we can fire, we're going to call fire weapon.
That takes care of the actual firing of the weapon.
Now, as soon as we fired the weapon, we set be sure fire to false.
Now, we can't fire anymore.
Now, since we're firing the weapon, we're going to start the timer.
So we're going to call it world timer manager.
And call the set timer function and we're going to pass an auto fire timer.
We'll put that on its own line.
Next, we pass in this.
As we can see by going to our sixth overload of the function, which takes the entire object, next
is the function callback from which we're going to use a shooter character.
Hotto.
Fire reset, finally, the delay time, which will be automatic fire rate.
Now we're going to call start fire timer in the fire button pressed function, so here in fire button
pressed, not only are we setting fire button pressed to true, we're also calling start fire timer
as well.
So far, button pressed calls start fire timer, which checks to see if we should fire, if B should
fire is true, we fire the weapon and set B should fire to false.
Then we start the timer and we can't fire again until B should fire is true again.
So if we press fire button pressed before the timer is over, we'll call start fire timer.
But start fire timer will not make it inside.
B should fire now.
As soon as the timer is done we call auto fire reset which sets should fire to true.
But as soon as auto fire reset gets called, we should check to see if we're still pressing the fire
button so we can say if the fire button pressed and if we're still holding the fire button down, we
can start fire timer all over again.
So call start fire timer.
So if we continue to press the fire button, as soon as the timer finishes auto fire reset, we'll check
to see if we're still pressing.
If we are, we'll start the fire timer all over again.
And so we'll continue to fire now.
Automatic fire rate needs to have a value.
So let's go up to the constructor.
And up here in the initialise or list, let's add an initialization for it and we'll start it off with
zero point one F four point one seconds and we'll comment this.
Automatic gunfire right now, something to keep in mind this point, one seconds needs to be a larger
value than the timer rate for interpolating for the crosshairs spread.
We need enough time between gunfire shots for the crosshairs to spread and shrink back.
And remember, the crosshairs spread timer uses zero point zero five, which is half of this value.
So as soon as we fire the crosshairs, start spreading outward four point zero five seconds and then
begin to shrink back inward.
And we won't fire another bullet for another point, one seconds after that.
So just keep in mind that if you increase automatic fire rate, you have to increase that interpellation
time or time for the gunfire spread factor.
So let's take a look at everything that's going on here.
So the first thing that happens is we press the left mouse button or the console trigger and fire button
pressed, gets called fire button pressed, sets the boolean fire button pressed to true and calls start
fire timer start fire timer will first perform an F check to see if we should fire.
If we succeed in that check, then fire the actual weapon by calling fire weapon.
Set B should fire to false because we shouldn't be able to just press the button as fast as we want.
We should have to wait for the fire timer to tell us we can fire again.
We also start the timer.
This gets the timer going and we have to wait the number of seconds determined by automatic fire rate.
Now, as soon as the timer finishes, then auto fire reset gets called because it's the callback for
the timer.
Auto fire reset sets B should fire to true again and then performs a check to see if the fire button
is still pressed.
If the fire button is still held down, then we call start fire timer again, which again checks B should
fire, which we just set to true an auto fire reset, which means we'll fire the weapon again.
Set B should fire to false and start the timer.
Once the timer finishes, we set B should fire to try to again check to see if the button is still pressed
and call start fire timer and we'll continue doing this indefinitely until we release the fire button,
which causes fire button released to be called.
And that sets B fire button pressed to false.
This means when auto fire reset gets called at the end of the timer, we check to see a fire button
pressed is true and it won't be, which means we'll stop calling start fire timer and the weapon will
stop firing.
So that's how we set up automatic fire.
Now, the last thing we need to do is give our booleans default values.
We have fire button pressed and should fire.
Now, here in the character constructor up here at the bottom of our initialise, our list will add
we should fire and its default value should be true.
There's no reason you shouldn't be able to start firing the first time you press the button, but be
fire button pressed should be false by default because we don't start off pressing it until the button
gets clicked.
Well, go ahead and consider this its own section of the initialised list, and we'll change the comment
to say automatic fire variables.
So now that we've initialized our variables, let's compile and we're back in the ED, I went ahead
and turned on desktop audio so we can hear this.
We're going to give this a try.
And there we have it, we have automatic fire.
So to summarize, in this video, we created the variables and functions necessary for automatic fire.
In the next video, we're going to implement jumping and make sure that we play the correct animations
for jumping by taking care of that in our animation blueprint.