Trying to "reset" automatic weapon fire when game is paused

I’m currently working on a shooter project, and one thing I’m struggling with is how pausing, then unpausing the game affects automatic weapons that were firing at the time of the pause.

So if a weapon is automatic, I have it set to fire on a looping timer:

void APlayerCharacter::StartFire()
{
	if (!ActiveWeapon->WeaponStats.bIsAutomatic)
	{
		FireWeapon();
	}
	else
	{
		FireWeapon();

		GetWorldTimerManager().SetTimer(FireHandle, this, &APlayerCharacter::FireWeapon, ActiveWeapon->WeaponStats.FireRate, true);
	}
}

And when the input is released, the timer is cleared:

void APlayerCharacter::StopFire()
{
	if (ActiveWeapon->WeaponStats.bIsAutomatic)
	{
		GetWorldTimerManager().ClearTimer(FireHandle);
	}
}

Here’s the FireWeapon() code, in case it might help:

void APlayerCharacter::FireWeapon()
{
	if (ActiveWeapon->WeaponStats.CurrentMagazine <= 0)
	{
		StopFire();
		return;
	}

	ActiveWeapon->Shoot();
}

So if the player is holding down the “fire” input when the game is paused, the weapon will continue firing on its own when the game is unpaused. I fixed it by calling StopFire() when the game is paused, but now the player is forced to press the “fire” input twice in order to fire again. I haven’t been able to figure out why; does anyone else know? My logic for pausing/unpausing is handled in Blueprints.

You can use FireHandle to pause and unpause the timer.

You can expose the timer via a blueprint callable function if you need bp access or expose it directly through a UPROPERTY

1 Like

I gave pausing and unpausing the timer a try. So far, I’m still having the same issue where “fire” has to be pressed twice in order to fire again. It’s so strange…

You have to link the funtion that pauses and unpauses the weapon timer to the main pause object delegate

Main pause function probability triggered in either player controller or hud). Add a new delegate there with a bool input to indicate if game is being paused or unpaused

Bind the timer to the main pause delegate on begin play

Then from within the main pause function a the end of it call the delegate to which the weapon is bound

Weapon => on play => get ref to where pause input happens => bind event to pause delegate

Main pause function => pause onput pressed => call delegate

1 Like

Sorry, I’m having trouble following…maybe it’ll help if I show you my Blueprint logic.

In my player controller, this is my Event Graph:

Here’s my Pause Game function:

Here’s my Resume Game function:

And here’s the delegate I created per your guidance, but I don’t understand what needs to be done with it.

Could this be related to how the EnhancedInput plugin handles inputs? My StartFire() and StopFire() functions are bound to corresponding Input Action assets, but the triggers are set to Pressed and Released like they should be…outside of pausing, they work as expected.

I double-checked a previous shooter project where I used the default Unreal input system and the problem doesn’t occur when the game is paused. But looking back over my code, I don’t see any difference between how shooting is handled aside from the use of EnhancedInput on this newer project.

Did you try adding a print string to the stop fire delegate function to see if it is being triggered correctly? (with the corresponding passed bool)

1 Like

If I place that function in the Pause Game and/or Resume Game functions, it gets called correctly. I’ve also noticed that for some reason, pausing the timer does not stop the gunfire but unpausing it does. So I have to flip the Branch conditions around, which is weird:

P = Pause / Unpause
left click = Fire

1 Like

Thank you for sharing that. I’ll check out the project but in the meantime…I mentioned that this issue doesn’t occur on a previous project, which uses Unreal’s default input system instead of EnhancedInput. I bound the firing functionality in my current project to the default PlayerInputComponent, and voila! No more extra “fire” input required after the game is unpaused.

So now I’m inclined to believe that EnhancedInput, or at least my use of it, is to blame somehow. But again, I’m happy to look at your project and see how you’ve approached this.

I tried opening up your project; got some errors that prevented it from being successful. Here’s what the log file said:

What version of UE was it created with? I tried opening the project with v5.0.3.

It was in 5.1. You need to generate the visual studio project first then run it from the ide

1 Like

Yeah, those errors are what I got when attempting to generate the project files. I’ll just install 5.1; I need to get it at some point anyway.

Hi again, I looked at your project and I essentially did the same thing you’ve got there: adding code to pause the timer when the game is paused, and unpausing the timer when the game is resumed. And even though your project is set up to use EnhancedInput, you’re using the default input system to execute your shooting code.

At this point I’m going to continue tinkering with the Input Actions and Input Mapping Context in my own project to see if I can find a way past this, and if not I’ll just leave the firing functionality on DefaultInput since I’ve confirmed the issue doesn’t happen in that case.

I’ll also mark this question as resolved. Thank you so much for all your help!

Quick follow-up: I stumbled across this forum post, which turned out to be exactly what I needed! When I first set my project up for EnhancedInput, I couldn’t get the start fire/stop fire functionality to work with just one Input Action. So I created Start Fire and Stop Fire actions, which is apparently what led to my problem occurring in the first place.

Hopefully this helps others in the future!