Calling an animation BP from Character c++ class

Hello.

I have 2 c++ codes,

  1. Character Class C++
  2. Animation Instance C++

And I have generated Blueprints from those C++

  1. Character Class → CBP_MyCharacter
  2. Animation Instance Class → ABP_MyAnim

My question is

  1. I want to call Animation Instance C++ 's Function from Character Class C++ file
  2. I want to call Animation Blueprint ( From Animation Instance C++ ) from Character Class C++ file

Here is an example of Animation Instance C++ header

#include "Animation/AnimInstance.h"
#include "MyAnim .generated.h"

/**
 * 
 */
UCLASS()
class CHARACTER_MODULE_API UMyAnim  : public UAnimInstance
{
	GENERATED_BODY()

public:
	
	void myFunction();
	
};

And I want to call myFunction(); from Character class.

Can you help me this?

Thank you.
I added a pinter of the class.
But I’m stuck in moment that I can’t cast it.
I need to call exactly object that my character is using.
And I can’t figure it out the way how to find the animation instance that my character is using…

The Character class needs to know about your AnimationInstance Class. In other words you need a pointer to it. You can achieve this by for example creating the animation instance in the character class. All the creation methods, for example NewObject return a pointer to the object you just created that you can use. When you have this pointer (lets call it Animation) you can access all its public members with the → operator. So in your case you would simply write this Animation->myFunction(); to call the function.

I don’t know anything about Animation instances, so I can’t give you the perfect explanation. But: Do you Spawn/Create the animation instance in blueprint/code, or did you drag it into the level manually?

Can you post the code where you are trying to cast? Basically write down what you think should work but does not.

It’s my main character’s Anim instance.
So if I play, it just spawn from “player start” location.
and Anim Instance is my character’s skeletal mesh’s object.
I tried to cast or referenced but c++ gives me denying message at the compile procedure…
Not easy. I tried to find another answer but couldn’t figure it out.

wait, is the < UMyAnim> gone? that happens sometimes. The cast should be:

Cast < UMyAnim>(GetMesh()->GetAnimInstance());

If you get an access violation one the MyAnim->f_DeltaTime = 0.3f line, that means that MyAnim is a nullptr. That can mean two things: Either the GetAnimInstance() returns a nullptr, which means there is no anim instance (yet?, maybe you make this call, before the instance gets assigned?), or the cast fails and returns nullptr, which would mean that GetAnimInstance() does not return an object that is a UMyAnim

-header-
UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Category = Anim)
class UMyAnim* MyAnim

-cpp-
MyAnim = Cast (GetMesh()->GetAnimInstance());
MyAnim->f_DeltaTime = 0.3f;

And it’s making an error.
Debugger says Violation of writing…

Ah they removed the tag.

my casting was

MyAnim = Cast <<UMyAnim>>(GetMesh()->GetAnimInstance());

because I need to access that class’s property.
It’s so difficult.

I can access UObject class that my custom classes,
but AnimInstance is mystery.

Thank you! I’ve solved this problem.
I just made a function for casting and if there is no class pointer or no animation instance, I just simply returned the function.
and few ticks later, it has casted class and assigned an anim instance.
So it’s clearly working well. Thank you!