How could I charge a weapon by holding down the fire button?

Hey guys I couldn’t find a repeated or hold function from input and I tried to set up a timer to charge a weapon shot. Need help!

Try this:

  • Wire your input’s Pressed pin in to a While Loop
  • Create a New Bool variable, Call it what you’d like, it’ll be used to determine whether or not the button was released, Have it set to False by default
  • Drag it to the blueprint, Click Set, and make sure the check mark is clicked in that Set node
  • Proceed to Wire up your logic for however you wish to handle what happens when the button is released

Next we’ll focus on the Pressed logic

  • Grab the Release variable again and click get this time.
  • You’ll need something to compare the Charge to the full value, So grab your Charge Variable (i assume you have one) and hit Get,
  • Drag the pin from the variable and type in Equal(float) or click on it once you see it on the list
  • Type in the Max Value of your charge on the second pin…
  • Now You have a choice: One way is faster, the other is more readable, i’ll explain why

Option 1)

  • Grab the red pin from either the Release or the Charge Compare nodes and apply a NOT Boolean to either one of them
  • Connect those two to an AND Boolean and wire that to the pin

Option 2)

  • Instead of all that, you can instead optionally wire both of those up to an OR boolean then wire that to a NOT boolean

They should both function the same, Option 1 is a bit more verbose, Option 2 is slightly less stuff, uses an optimization trick i learned in a digital logic course,

Anyway, Wire the end result of whatever option you chose to the Condition pin of your WhileLoop Node. Wire Charging logic to the Loop Body Pin (Usually an addition and a delay would work good here) Also Add logic to the Completed pin,

Hope it helps!

Wow thanks for the help speewave! I’ll try it out today!

No Problem, Message back here if it was a bit unclear about anything, i can recreate it in a Blueprint and post the screenshot!

Hi.

I tried the WhileLoop method with a custom projectile for full automatic firing. Somehow the Play mode stopps immediately when I press the fire button!

I have a delay at the end with a Shot Finished bool - so it can’t fire up quadrilions of projectiles at once. Right after the While Loop a Branch checks the Shot Finished bool. If it’s True it sets the bool to False and then comes the projectile stuff… The delay works without the Hold bool before the While Loop but not with it.

Do you have any idea?

This is a while back but if you could post a screenshot that’d be really helpful!

Hope it helps…

The functions shoot and charge are just blueprint functions and in my case are just place holders…

Let me know if you have any questions

Sorry i’m a bit late, i’ve been extremely swamped lately… Anyway, if you’re still having troubles, send me a screenshot of your layout and i’ll see what i can do

The easiest way is to do it, is to do it in Tick().
Assuming you derived from AActor or implemented your own ticker, it is fairly easy to do.

You crate two Input options OnButtonPress() and OnButtonReleased().

You will also need some way to track “state” of weapon chargind, to check whether you are charging weapon or not. In header create variable:

bool isWeaponCharging;

Now:
OnButtonPress()
{
isWeaponCharging = true;
}

You also need some helper variables:
float currentChargeTime;
float MaxChargeTime;

In your Tick:

Tick(float DeltaTime)
{
Super::Tick(DeltaTime)

if(isWeaponCharging)
{
	currentChargeTime += DeltaTime;
	if(currentChargeTime >= MaxChargeTime)
	{
		currentChargeTime = 0;
		FireWeapon();
	}
}

}

that’s it. you can make it more sophisticated by passing currentChargeTime to function that will charge weapon, and determine from that value,power of shot.

You do not want to increment the chargeValue in a while loop, that is very bad practice in this case. Do what iniside said below… use a boolean to manage the increment inside the Tick() function. This even gives you a DeltaTime value so you can use the passage of time to do the chargeValue.

Good for C++ code, but this seems to be needing a blueprint implementation…

I don’t have a lot of experience with the Blueprint system yet, but all my programmer instincts tell me this locks the blueprint or game in the same tick until the thing is charged?

Hi cloudnine1098,

We have not heard back from you in a few days, so we are marking this post as Resolved for tracking purposes. If you are still experiencing the issue you reported, please respond to this message with additional information and we will offer further assistance.

Thank you.

I tried Speewaves version but pie would quit on me when I let go of the mouse - similar to AWYN.
I’d like to try Inisides verion but he lost me at “the easiest way to do it would be”
Would someone be so kind and show what it looks like in BP? (BP now stands for Baby Pictures)

Actually was doing something similar to this just today (auto repeating fire button) and found using a Gate is a really nice way of doing it

https://docs.unrealengine.com/latest/INT/Engine/Blueprints/UserGuide/FlowControl/index.html#gate

Oh excellent thanks heaps, sorry for the delayed response.

If you’re using a key and not the touch pad, you can use this Blueprint by Steve Allison.


More here!