Make character equip a weapon in c++

How to make character equip a weapon when a key is pressed, from code? And how to switch between animation in the AnimBlueprint with the variable that is in the code?
thanks in advance for help :slight_smile:

Please use SSCCE from now on. I don’t think anyone is going to help you with your problem when your problem you stated is very vague and not specific/detailed enough. :confused:


Anim Blueprints:

I am doing a simple tps, i started setting the animation using the AnimStarterPack on marketplace, and i want make him switch the animation from the third person template idle to the idle where he has a weapon in the hand using a equipping animation in between. I am using code so i add the key event in ThirdPersonCharacter.cpp
Here is the code:

void AThirdPersonCharacter::SetupPlayerInputComponent(class UInputComponent* InputComponent)
	// Set up gameplay key bindings
	InputComponent->BindAction("Jump", IE_Pressed, this, &ACharacter::Jump);
	InputComponent->BindAction("Jump", IE_Released, this, &ACharacter::StopJumping);
	//I add this line to the orginal template to add the key binding. I also added it in the Project settings>Input>Key Bindings with the same name
	InputComponent->BindAction("EquipDisequipWeapon", IE_Pressed, this, &AThirdPersonCharacter::EquipDisequipWeapon);

	InputComponent->BindAxis("MoveForward", this, &AThirdCharacter::MoveForward);
	InputComponent->BindAxis("MoveRight", this, &AThirdCharacter::MoveRight);

	// We have 2 versions of the rotation bindings to handle different kinds of devices differently
	// "turn" handles devices that provide an absolute delta, such as a mouse.
	// "turnrate" is for devices that we choose to treat as a rate of change, such as an analog joystick
	InputComponent->BindAxis("Turn", this, &APawn::AddControllerYawInput);
	InputComponent->BindAxis("TurnRate", this, &AThirdPersonCharacter::TurnAtRate);
	InputComponent->BindAxis("LookUp", this, &APawn::AddControllerPitchInput);
	InputComponent->BindAxis("LookUpRate", this, &AThirdPersonCharacter::LookUpAtRate);

	// handle touch devices
	InputComponent->BindTouch(IE_Pressed, this, &APersonCharacter::TouchStarted);
	InputComponent->BindTouch(IE_Released, this, &APersonCharacter::TouchStopped);

void AThirdPersonCharacter::EquipDisequipWeapon()
	if (Controller != NULL)
		if (IsWeaponEquipped == false)
			IsWeaponEquipped = true;

I want to use this variable (IsWeaponEquiped) in the transition between the animation like here but i don’t know how to.

Can you help me please? :slight_smile:

Create the same variable in your animation blueprint. Go to the event graph of the animation blueprint. Search for Cast to character blueprint (Whatever your character is) than get the variable from your character and set it to the on from your animation blueprint. I’ve done something similar in one of my projects (See picture)

You’ll need to make sure that your variable in the class is specified with the correct access i.e. has the UFUNCTION and the correct settings to be read by blueprint (BlueprintReadOnly or BlueprintReadWrite) such that you can actually access it in the event graph of your anim blueprint. Simpler shows you the right way to pass this into your animBP, although honestly it feels a bit cludgy because all the event graph is doing is grabbing data and storing it, when it feels like that should be automated somehow.

You’re right. And when the state machine gets bigger and bigger you have to do this process for every variable and it gets annoying. But this is the only way I could come up with. If someone knows a better/ more efficient way of doing this let me know!

After the last update i have set and get funcion of the variable, istanced on the code, also from blueprint. And using “Cast to Character” like Simpler did make it funciotn. Thank you:)