Get Blueprint Animation variables in C++?

Maybe different than you’re thinking but does this help?

Hello, I’ve been able to get Curves and Notify’s from animations just fine, but I’m having a hard time getting or setting animation variables so the animations will actually play.

Here is how I got the curves and notifys:

void AUnit::BeginPlay(){
	Super::BeginPlay();
	Animation = Mesh->GetAnimInstance();
}
void AUnit::Tick(float DeltaTime){
	Super::Tick(DeltaTime);

	//Get Animation Curve
	float val = Animation->GetCurveValue(FName("Curve"));

	//Get Animation Notifies
	TArray<const struct FAnimNotifyEvent*> AnimNotifies = Animation->AnimNotifies;
	for (int i = 0; i < AnimNotifies.Num(); i++){
		AnimNotifies[i]->NotifyName.ToString();
	}

	//Get/Set Animation Variables.. These are fake, 
	//but I'm looking for something like this:
	Animation->SetBool("inAir", true);
	Animation->GetFloat("speed");
}

Now I can’t seem to figure out how to access the variables through the AnimInstance.

Any help would be greatly appreciated!
Thanks!

Bonus: If you know how to directly play/crossfade an animation through code that would be great too! :slight_smile:

Edit:
I ended up using that method. The code on that page has some errors as it’s a few years old and it’s missing some steps, but in the end it works. If you want to post that as an answer, I’d be glad to mark it as the correct asnwer.

I have two options for you. First is the way I suggested already. It’s the answer usually given:

Second Option:

    if (USkeletalMeshComponent *Mesh = MyActor->FindComponentByClass<USkeletalMeshComponent>())
    {
        if (UAnimInstance *AnimInst = Mesh->GetAnimInstance())
        {
            UFloatProperty* MyFloatProp = FindField<UFloatProperty>(AnimInst->GetClass(), AnimPropName);
            if (MyFloatProp != NULL)
            {
               float FloatVal = MyFloatProp->GetPropertyValue_InContainer(AnimInst);
               MyFloatProp->SetPropertyValue_InContainer(AnimInst, 180.0f);
               FloatVal = MyFloatProp->GetPropertyValue_InContainer(AnimInst);
            }
        }
    }

I put a way to get and to set in there.

This technique also works with regular blueprints, not just anim blueprints and with other types:

   UBoolProperty* BoolProp = FindField<UBoolProperty>(MyActor->GetClass(), PropName);
    if (BoolProp != NULL)
    {
        bool BoolVal = BoolProp->GetPropertyValue_InContainer(Actor1);
        BoolProp->SetPropertyValue_InContainer(Actor1, true);
    }

The names are just simple FName data types:

FName PropName = TEXT("MyBoolVariable");
FName AnimPropName = TEXT("MyFloatVariable");

Here’s the source I got it from:

I used the first method, ended up working very nicely, thankyou. :slight_smile:

check this as well: