Can't play animation from C++

Hey

My team created some Anim sequences, and converted them to Anim montages, so it would be played from code.

The code

bool AMyCharacter::PlayAnimMontage(class UAnimMontage* AnimMontage, float InPlayRate, FName StartSectionName)
{
	USkeletalMeshComponent* UseMesh = GetPawnMesh();
	if (AnimMontage && UseMesh && UseMesh->AnimScriptInstance)
	{
		return (UseMesh->AnimScriptInstance->Montage_Play(AnimMontage, InPlayRate) > 0 ? true: false);
	}

	return false;
}

The mesh matches the Animations, and everything plays out smoothly in the editor, but I can’t for the life of me get the animations to activate from code.

Is there anything I might have missed??

Dear Abdullah,

Several things to check:

-Do you have a FullBody slot in your animation Blueprint for your character

-Did you make sure to type in the name of your slot such as FullBody into every montage that you are calling?

-Are you sure that your montage references are not null?

Post your code for how you are referencing your montages in your code if you are not sure about this part

if(!MyMontageRef) //log = this montage is NULL!

If this answers your question please add a comment to explain what part was the solution for you and mark the check by my answer so others know it has been resolved. This will help with ppl searching the UDN in the future.

Hey Rama
I created a FullBody slot, linked it to the montages, and checked the referencing. Everything is solid, but it is still not working.

I even checked the return of this function:

    return (UseMesh->AnimScriptInstance->Montage_Play(AnimMontage, InPlayRate) > 0 ? true: false);

and it returned true. Even the name of the AnimMontage was correct. I reference the montages from the editor

 USTRUCT()
    struct FComboMontages
    {
    	GENERATED_USTRUCT_BODY()
    	
    	UPROPERTY(EditDefaultsOnly, Category=Animation)
    	UAnimMontage* LightSlash;
    
    	UPROPERTY(EditDefaultsOnly, Category=Animation)
    	UAnimMontage* HeavySlash;
    };
    
    //Inside Character.h class declaration
    UPROPERTY(EditAnywhere, Category=Sword)
    FComboMontages MyComboMontages;

Also a couple of screenshots for further clarification

untitle2d.png

well I am not in any way trying to sound demeaning, but are you sure that your character is using the latest version of your animation blueprint that you showed a picture of? could you confirm this by using a different default animation sequence besides the jump one?

Because everything you are describing sounds great to me.

The only other thing you could consider checking,

your struct is EditAnywhere but the contents are edit defaults only

could you just, for my sake anyway,

just reference the montages directly in your character.h like this:

this is how I do it:

UCLASS(config=Game)
class AJoyWarriorBase : public AVictoryPlayerCharacterBase, public IVictoryVibesInterface
{
	GENERATED_UCLASS_BODY()
		
	//~~~ Swing Montages! ~~~

	UPROPERTY(EditDefaultsOnly, Category=JoyAnims)
	UAnimMontage* Swing1;

Again this is all just for debugging purposes, I am not saying anything is wrong with your setup, I’m just trying to get more data by trying different things

:slight_smile:

Rama

Just changed the character.h file to reference the montages directly just like your setup, but with no luck.

I did try four different montages, and none of them worked. I think the problem maybe in the animation blueprint because I checked the codes a thousand times and I really can’t find anything wrong with it.

Really appreciate the help, man

Are you sure that, in your character blueprint, you have your current animation blueprint listed?

and did you try changing that default animation to something else to confirm you are working with the latest version of the anim BP ?

It worked !!!
I changed the position of the “FullBody” slot to be right before the end of the “Idle/Run” state in the Anim Blueprint.
Not really sure how or why it worked, but it did :slight_smile:
Time to figure out Root Motion

wohoo!!!

glad it worked!

please mark the check next to my answer so that others know this issue is resolved :slight_smile:

Rama

You got it.