Prevent multiple mouse clicks when firing weapon

I’m trying to prevent multiple mouse clicks when firing my characters weapon, every time I click to fire a shot is made (or multiple on a timer if held) however, if released and clicked again another shot will be made.

Is there a way to prevent the player from spamming the fire button?



You will want to make a variable called “canFireAgain” and a const variable called “fireDelay”. Fire delay will be some amount of time. You then want to get the time when they fired and set canFireAgain to that time plus fireDelay.

Each time they fire do a check, is the current time greater than canFireAgain? If so, let it fire, if not, don’t.

You are going to ask about time, this is probably what you are looking for.

Edit: I guess you could also use a DoOnce when you fire, and hook a timer up to the reset, but this might not be cost efficient, and would probably be more complicated than what I said above.

This whole thing is not as easy as it looks.
At some point you want autofire weapon it will mess everything up.
I made autofire actor component, but sadly last thing that is unsolved is that rapid clicking.
So i cannot share blueprint really (it also has some marketplace content)
But i can write what i did:

  • i made 2 mouse events (in player controller) that are “start fire” and “stop fire”. created also dispatchers for them, so mouse event directly calls corresponding dispatcher
  • then i made 2 events in weapon blueprint that i assigned to dispatchers in player controller.
  • when i start firing i remember “game delta seconds”
  • then i fire shoot, and calculate next time weapon can shoot )it is game time seconds plus autofire delay)
  • then i just wait until current game time seconds is more that that calculated time. (on event tick simple branch until time for shoot)
  • when its time i call “Shoot event”

For spamming mouse clicking you need to do one more gate, and do not update next time to shoot when mouse is clicked

“Autofire & Authority” macro:

Whole graph for component: