Action Mapping

Hi there ! I currently working on a gun system and I’ve encountered the problem what I do is I set up two action mappings for space bar pressed and released in methods witch are bind to them I set a bool variables to true in pressed and false in released and in my tick method I process the input based on this bool variable, the goal is to have bullet fired when space is clicked and fired at the interval when space is held everything works fine except that before the released action is executed the tick method ticks at least two times, and when I click space to fire just one bullet two bullets are fired instead. Any idea how to get this sorted ?

You can make a float variable that adds the deltatime from tick together, and compare it against some value you choose, if it’s over, reset you counter to 0 and fire.

Do this only if your bool is true. On the release you will want to reset the counter value to 0.

Yeah it crossed my mind and also a counter variable incremented every tick, but this may be a pain in the ■■■ if you have to do it for multiple actions, and it also creates another problem what if I need pressed and released actions in single frame ? or in nested if statements similar to this

	if (buttonPressed)
	{
		// do something but...

		if (buttonReleased)
		{
			// do something else
		}
	}

if statement (buttonReleased) will not be executed therefore it will probably never be executed as this action will happen 2 frames later witch means the first if statement will not be true at that time. How can I handle something like this any ideas ?

Another option would be to use a Timer to control the repeated firing of the weapon. This would make controlling the fire rate pretty straight forward and might even save a (tiny) bit of performance because you wouldn’t be executing code on every tick.

To do this, you would wrap the core fire logic into a function called something like “DoFire”. Then you would call that function repeatedly from a Timer that is created whenever the fire key is pressed and destroyed when the fire key is released. This also makes it easy to control the fire rate through a variable. See the attached blueprint. Note that the variable “FireTimer” is of type TimerHandle and “FireRate” is a float.

Yeah it will work great for my basic weapon but there is another type of weapon I have it doesn’t just shoot, the space bar needs to be held down for certain amount of time and based on that time a distance is calculated and when space is released the bullet is shoot at this distance. I’m gonna try both ways on each weapon type and than I will decide what to do thanks !