GetWorldTimerManager().SetTimer() not working?

So I have the following functions below being called by character input functions.

I have verified that StartFire() and StopFire() are being called correctly with a debug message. I know that ASWeapon::Fire function works because I can manually call it in blueprint and it works as intended. I also tried manually calling Fire() directly in the character input functions and It works.

However what i’m trying to accomplish is using a timer to handle time between shots. This code worked in unreal 4.27 but does not work in 5.0, I tried 5.0.3 and now I have tried it on 5.2.1 and I get the same result. Am i missing something or is something actually just wrong with SetTimer()?

void ASWeapon::StartFire()
{
	GetWorldTimerManager().SetTimer(TimerHandle_TimeBetweenShots, this, 
        &ASWeapon::Fire, 1.0f, true, 0.0f);
}

void ASWeapon::StopFire()
{
	GetWorldTimerManager().ClearTimer(TimerHandle_TimeBetweenShots);
}
1 Like

Looks correct.

Is the TimerHandle_TimeBetweenShots being used elsewhere?
Is the ASWeapon instance being destroyed?

TimerHandle_TimeBetweenShots is only used in that class in those functions. The instance of the weapon isn’t being destroyed at all currently only created.

So I’ve figured it out though i’m not sure why this mattered for it to work correctly.
It needed to have a check to make sure the timer wasn’t already active.

void ASWeapon::StartFire()
{
	if (!GetWorldTimerManager().IsTimerActive(TimerHandle_TimeBetweenShots))	
		GetWorldTimerManager().SetTimer(TimerHandle_TimeBetweenShots, this, 
                &ASWeapon::Fire, FireRate, true, 0.0f);
}

void ASWeapon::StopFire()
{
	GetWorldTimerManager().ClearTimer(TimerHandle_TimeBetweenShots);
}
1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.