# Cast variable to Animation BP

**URL:** <https://forums.unrealengine.com/t/cast-variable-to-animation-bp/477027>\
**Category:** Character & Animation\
**Tags:** cast-to, unreal-engine, question\
**Created:** [January 10, 2021, 4:35am UTC](https://forums.unrealengine.com/t/cast-variable-to-animation-bp/477027 "2021-01-10T04:35:32Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tata\_Bittar](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Tata\_Bittar](https://forums.unrealengine.com/u/Tata_Bittar)\
**Post date:** [January 10, 2021, 4:35am UTC](https://forums.unrealengine.com/t/cast-variable-to-animation-bp/477027/1 "2021-01-10T04:35:32Z")

</div>

Hi,  
I have a variable in the character c++ class that I want in the animation blueprint. I managed to get it there by casting in the character blueprints, however I want to do this with c++.

 ![326835-casting.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/f/d/d/fdd12d70360394ddae24680834854a680b6561bf.png)

Basically I need to convert this to c++, or another method.

- 

I have tried this but got an error stating: " 'MovementDirection ': is not a member of ‘UAnimInstance’ ".

cpp

```
 Mesh = FindComponentByClass<USkeletalMeshComponent>();
	AnimInstance = Mesh -> GetAnimInstance();

	AnimationBP = Cast<UAnimInstance>(AnimInstance);

	if(AnimationBP)
	{
		 AnimationBP->MovementDirection = Axis;
		
	}

```

h

```
USkeletalMeshComponent* Mesh;
UAnimInstance * AnimInstance;
UAnimInstance * AnimationBP;
```

---

<div class="post-metadata">

**Author:** ![Lardo\_Deepdelver](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Lardo\_Deepdelver](https://forums.unrealengine.com/u/Lardo_Deepdelver)\
**Post date:** [January 10, 2021, 12:34pm UTC](https://forums.unrealengine.com/t/cast-variable-to-animation-bp/477027/2 "2021-01-10T12:34:55Z")

</div>

AnimationBP-\>MovementDirection seems to be a bp variable which is very hard to set in C++

would recommend creating your own AnimationInstance in C++ with the MovementDirection variable, then casting to it is not a problem

```
UCLASS()
class UMyAnimInstance : public UAnimInstance
{
  GENERATED_BODY()

public:
  UPROPERTY(EditAnywhere, BlueprintReadWrite)
  FVector MovementDirection;

};

MyAnimation = Cast<UMyAnimInstance>(AnimInstance);
 
     if(MyAnimation )
     {
          MyAnimation->MovementDirection = Axis;
         
     }
```

---

<div class="post-metadata">

**Author:** ![Tata\_Bittar](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Tata\_Bittar](https://forums.unrealengine.com/u/Tata_Bittar)\
**Post date:** [January 10, 2021, 11:18pm UTC](https://forums.unrealengine.com/t/cast-variable-to-animation-bp/477027/3 "2021-01-10T23:18:02Z")

</div>

I see. I created an Anim Instance c++ class and made it the parent class of my animation blueprint. What would the AnimInstance have to be now? I am getting an error for casting.

h

```
UMyAnimInstance * AnimationBP;

```

cpp

```
AnimationBP = Cast<UMyAnimInstance>(AnimInstance);
```

---

<div class="post-metadata">

**Author:** ![Lardo\_Deepdelver](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Lardo\_Deepdelver](https://forums.unrealengine.com/u/Lardo_Deepdelver)\
**Post date:** [January 11, 2021, 5:34am UTC](https://forums.unrealengine.com/t/cast-variable-to-animation-bp/477027/4 "2021-01-11T05:34:53Z")

</div>

what’s the error?

---

<div class="post-metadata">

**Author:** ![Tata\_Bittar](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Tata\_Bittar](https://forums.unrealengine.com/u/Tata_Bittar)\
**Post date:** [January 11, 2021, 3:44pm UTC](https://forums.unrealengine.com/t/cast-variable-to-animation-bp/477027/5 "2021-01-11T15:44:31Z")

</div>

There is a very large log:

 ![326957-error.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/5/0/1/50149b49579cda87e35e77b79ff89d12c84e3220.png)

---

<div class="post-metadata">

**Author:** ![Lardo\_Deepdelver](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Lardo\_Deepdelver](https://forums.unrealengine.com/u/Lardo_Deepdelver)\
**Post date:** [January 11, 2021, 4:10pm UTC](https://forums.unrealengine.com/t/cast-variable-to-animation-bp/477027/6 "2021-01-11T16:10:35Z")

</div>

Think you are missing the include for UMyAniminstance in Default\_Player.cpp

---

<div class="post-metadata">

**Author:** ![Tata\_Bittar](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Tata\_Bittar](https://forums.unrealengine.com/u/Tata_Bittar)\
**Post date:** [January 11, 2021, 4:19pm UTC](https://forums.unrealengine.com/t/cast-variable-to-animation-bp/477027/7 "2021-01-11T16:19:44Z")

</div>

Yesss, it works now. I realized I had the wrong include.  
Thank you very much for the assistance.
