Why is it called only once? (4-26)

Hi,

I want to make a Burst shooting weapon. It should pew pew pew but it only pew once using this code.

FTimerHandle BurstTimeHandle;
for (int i = 0; i < 3; i++)
		{
			GetWorld()->GetTimerManager().SetTimer(BurstTimeHandle, this, &AWeapon::BurstFire, BurstTime, false);
		}

BurstFire() is called only once. Can anyone help me to figure out why does that happen?

Check this article on a different approach and with some explanations on the matter:

Your issue thou, is that the loop is really fast and SetTimer is being overwritten twice, so only the last one is valid. You will want to use the example with the article above with a Delegate which will do a Pew and at the end it will restart the timer controlling how many times that happen, so you will use a counter to control that.

You would need three different FTimerHandles, if you want three separate calls.

That said, do you want it to go pew… pew… pew… or do you want it to go PEW three times at once at the same time?

Thank you for replying.
I was following this tutorial but I couldn’t solve it.

Thank you for replying.
What I want to do is. pew then after 0.1 second another pew and 0.1s then pew. With one mouse click.
How would you do it?

either you need three FTimerHandles when you start, and offset them each by 0.1sec, or after each one fires, you start another one.

1 Like

Thank you for your replay. I will post my solution bellow

So here is my solution: (Thanks to eblade for helping me)

BurstTime = 0.1f;
FTimerHandle BurstTimeHandle;
BurstCounter = 0;
GetWorld()->GetTimerManager().SetTimer(BurstTimeHandle, this, &AWeapon::BurstFire, BurstTime, false);


void AWeapon::BurstFire()
{
	if (BurstCounter < BurstNumberOfBullets)
	{
		FireFunctionality();
	}
	BurstCounter++;
GetWorld()-GetWorld()->GetTimerManager().SetTimer(BurstTimeHandle, this, &AWeapon::BurstFire, BurstTime, false);
}

Pretty close there, but you should only set the timer if there’s any more left to fire. Otherwise, you’ve got this timer firing every 0.1 seconds forever.

I’d go with something like

BurstCounter = BurstNumberOfBullets;
...
if (BurstCounter-- > 0) {
    FireFunctionality();
    if (BurstCounter > 0) {
        ...Set new timer
    }
}
1 Like

Oh yeah. Thank you very much :slight_smile:

1 Like