Casting help.

I want to get a variable from my player character and cast it to my animation blueprint so that PlayerSpeed is = to the variable MaxWalkSpeed in my player character. I tried Cast<APlayerCharacter>(objref) and i dont think im getting the objref part. I keep failing. Ive made new objects over and over ive tried get owner and i just cant understand this. Please help :frowning:

Oddly figured it out myself. Took me weeks. I still dont understand how i got it though. Can someone please explain? this is the code.

in obj initialization

UPlayerBPAnimInstance *PlayerSpeedptr = Cast<UPlayerBPAnimInstance>(UPlayerBPAnimInstance::StaticClass());

float APlayerCharacter::GetPlayerSpeed()


CharacterSpeed = GetCharacterMovement()-&gt;MaxWalkSpeed;


return CharacterSpeed;


so from my stand point is that i created a function called GetSpeed(float x), i then referenced it from the player character and casted the object aka the static class to then call get speed from my the animbp and reroute my “characterspeed” to my getspeed function thus spitting out my characterspeed in the animbp? just wanna make sure i got this so i can replicate this to other objects in the future.

wait bump it actually doesnt work lol. Was getting a caught signal 11.

I really don’t understand what you’re trying to do.
AnimBP usually reads from Character class, not other way around, you already can GetCharacterMovement() from Character.

[USER=“434”]BrUnO XaVIeR[/USER] im trying to cast getcharactermovement() from my playercharacter to my animbp to set my playerspeed variable in there = to my maxwalkspeed

You can’t access blueprint properties in your C++ code (as far as I know). There’s basically two approaches you can take:

  1. Create a C++ class extending UAnimInstance, there you create a variable “UPROPERTY(EditAnywhere) float Speed;”. In UE4, you open your anim BP, go to file->reparent blueprint and select the class you just created. Inside your anim BP you now use get Speed to access your speed variable. In your character C++ code you can now cast like this:

UAnimInstance* baseAnimInstance = GetMesh()->GetAnimInstance();
UMyAnimInstanceClass* myAnimInstance = Cast<UMyAnimInstanceClass>(baseAnimInstance);

// UMyAnimInstanceClass is the name of the new class you just created

now you can access your Speed variable like this:

if (myAnimInstance)//always check that your pointers are valid!
    myAnimInstance->Speed = 100.0;

Why this works is because you already know that GetMesh()->GetAnimInstance() gets you an anim BP of type UMyAnimInstanceClass, because you reparented it earlier. GetAnimInstance always returns the pointer as a UAnimInstance though.

Basically C++ interprets the object as a basic UAnimInstance at first, you have to specifically tell it, by casting “explicitly”, what subclass of UAnimInstance it actually is. So you don’t cast a variable inside your anim instance, but you cast a pointer to your anim instance so C++ understands it contains the variable you need.

To elaborate on casting pointers: for example, you can take a pointer to your character and cast it to AActor, because all characters eventually extend AActor. All subclass pointers can be casted to any of their parent classes.

AMyCharacter* myCharacter = this;
AActor* myCharacterInterpretedAsActor = Cast<AActor>(myCharacter);

  1. The second approach would be to do everything in your animation blueprint. For example you could create a “Speed” variable in your blueprint, in the tick event get the player character, access your movement component and its speed from there and copy the value to your blueprint Speed variable.

@Morphcider Hey! thank you for replying, i actually ended up doing it through blueprint but ill do it this way actually since i really want the majority of my code to be C++ i just reparented my AnimInstance back to the original