Get Animation Blueprint boolean Variable to C++

I am trying to get a variable from animation blueprint to c++, so I can change it / update it in my c++ code.

In my header file I have an Fname property as follows

in my cpp file I am getting the Mesh > AnimInstance > Boolean

Upon debugging, my boolean variable is undefined. I just want to get the property and set it to true in code.

Personally when I want to use animation blueprint variable in c++ I create a c++ class derived of UAnimInstance and I reparent my blueprint class to my custom AnimInstance. After you can cast to your custom AnimInstance. You can create variables exposed to the blueprint.

There is maybe other better way to get hold of the ABP in cpp but I found it really practice to control this class in CPP.

let me see if I am on the right track.

First I created a class PlayerAnimations from UAnimInstance

Then I reparented my AnimationBlueprint to the newly created PlayerAnimations class


Screenshot_6

In my Event Graph in ABP and Transition Rules I am using the boolean variable created in the PlayerAnimations class


The goal at this point then is to change the player animation from Standing_Idle to Rifle_Idle, by turning my IsRifleCombatMode boolean to true (when rifle is equipped from back to hands).

Video here is from when I was just using Blueprints for the variable to flip between true and false to change the idle states (and in turn the unequipped and equipped blendspaces)

My PlayerCharacter class is calling a method in new PlayerAnimations class

Screenshot_11

Problem at the moment is either of the two methods I have tried to set the boolean variable, (both methods in screenshot above) I am getting an Exception_Access_Violation.
I just want to be able to set the variable to true or false depending on player input. What am I missing to play around with the state of this variable, or rather whats the best way to set the variable in code at this point?

I’ll need to play around with it myself to give a concrete answer but showing the line numbers and code in the PlayerCharacter.cpp that it mentioned would be useful info.

my code with the line numbers (moved some lines/comments around)

PlayerCharacter.cpp

PlayerAnimations.cpp

Exception Access Violation errors


in PlayerAnimations.cpp it crashes in line 9. If I comment that out same access violation errors crashes on line 13.

From what you describe and judging based on the code then PrimaryWeaponToEquip is most likely nullptr.

PrimaryWeaponToEquip is not null at that point. its spawning fine on player’s back,

then I am just changing the attachment socket from back > to hand in the EquipPrimary section.

Seems like the issue is in the RifleCombatMode variable

from my understanding after reading the answer here
https://answers.unrealengine.com/questions/301155/get-blueprint-animation-variables-in-c.html#answer-302449

My assumption was that the FName TEXT just has to match the variable you need in Animation Blueprint. Which is why I have it like this in PlayerAnimations.h

allowing me to use the FNAME property in line 13

FBoolProperty* BoolProp = FindFProperty(AnimInstance->GetClass(), RifleCombatMode);

which seems to be wrong if the parent class cannot read/access variable from Blueprint.

That says this is nullptr. Which would mean SetAnimations is nullptr. Where is that set?

Ah yes my bad, I had mindlessly left out this line before calling EquipRifle (line 115)

but now seems I am not able to change the state of the boolean to TRUE
line 16BoolVal remained undefined

Are you debugging a Development build or a Debug build? If it’s the former then the variable is just optimised away.

I am debugging in development.
Why do I have a feeling GetPropertyValue_InContainer is deprecated?

Surely from this instance should it not be something like

AnimInstance->IsRifleCombatMode_ = true; ??

Are there any other ways to extract the properties of the UAniminstance in this case?

I’ll think I’ll try and get this all set up on my side sometime today.

Edit:
So I just tried this with “Method A”, just setting it and it appeared to work. Tested using the jump transition nodes and an input binding to toggle a bool variable i.e.

void AShooterCharacter::Test()
{
	if (auto Anim = Cast<UAnimInstanceTest>(GetMesh()->GetAnimInstance()))
	{
		Anim->bIsInCombat = !Anim->bIsInCombat;
	}	
}

And it properly transitioned.

Ah yes that works. I think my main problem all this time was not having set a new boolean variable for my weapon in the Event Graph > which then sets isRifleCombat to true.

I struggled with your solution for a day before realizing I was forgetting something else in my Event Graph. Live and learn :slight_smile:
Now thinking even the GetPropertyValue methid probably works if I had not forgotten about that other variable. But your way is simpler.

Thanks for the assistance DanM. Much appreciated.
Also thanks @Philx for hinting on how to solve this.

2 Likes

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms