How to make bEnableAutoBlendOut work in C++?

I want to have a “Play Slot Animation as Dynamic Montage” node where I can dynamically edit the “Enable Auto Blend Out” option that montage assets have.

So I created a C++ Blueprint Function Library and created a function called:

Cpp_BPFL_Animate_PlaySlotAnimation

Here is the function:

UAnimMontage* UCpp_BPFL_Animate::Cpp_BPFL_Animate_PlaySlotAnimation(
    UAnimInstance* AnimInstance,
    UAnimSequenceBase* Asset,
    FName SlotNodeName,
    bool bEnableAutoBlendOut,
    float BlendInTime,
    float BlendOutTime,
    float InPlayRate
)
{
    UAnimMontage* CreatedMontage = nullptr;

    if (!AnimInstance || !Asset) {
        UE_LOG(LogTemp, Warning, TEXT("AnimInstance or Asset is null"));
        return nullptr;
    }

    if (AnimInstance && Asset)
    {
        // Play the animation and get the created UAnimMontage instance
        CreatedMontage = AnimInstance->PlaySlotAnimationAsDynamicMontage(
            Asset,
            SlotNodeName,
            BlendInTime,
            BlendOutTime,
            InPlayRate
        );

        if (CreatedMontage)
        {
            // Modify the montage EnableAutoBlendOut property
            CreatedMontage->bEnableAutoBlendOut = bEnableAutoBlendOut;

            // Modify other properties if needed
            CreatedMontage->BlendIn.SetBlendTime(BlendInTime);
            CreatedMontage->BlendOut.SetBlendTime(BlendOutTime);
            CreatedMontage->RateScale = InPlayRate;
        }
        else {
            UE_LOG(LogTemp, Warning, TEXT("Failed to create Montage"));
        }
    }

    return CreatedMontage;
}

Everything is working as intended except for:

CreatedMontage->bEnableAutoBlendOut = bEnableAutoBlendOut;

I can play the Animation Sequence that I need to play, and set its Blend In/Out or Play Rate but the Enable Auto Blend Out setting is not getting activated.

I need to play a montage from any given Animation Sequence and I want that montage not to go back to its original pose after it ends.

TLDR:
How to make CreatedMontage->bEnableAutoBlendOut work in C++?

This is simply playing a normal

AnimInstance->PlaySlotAnimationAsDynamicMontage

But I assumed that CreatedMontage->bEnableAutoBlendOut = bEnableAutoBlendOut; would still affect the CreatedMontage ?

Ok I got it working:

.h

// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "CoreMinimal.h"
#include "Kismet/BlueprintFunctionLibrary.h"
#include "Cpp_BPFL_Animate.generated.h"

/**
 * 
 */
UCLASS()
class MyGame_API UCpp_BPFL_Animate : public UBlueprintFunctionLibrary
{
	GENERATED_BODY()

public:
	
	UFUNCTION(BlueprintCallable, Category = "Animation")
	static UAnimMontage* Cpp_BPFL_Animate_PlaySlotAnimation(
		UAnimInstance* AnimInstance,
		UAnimSequence* AnimAsset,
		FName SlotName = FName("DefaultSlot"),
		float BlendInTime = 0.0f,
		float BlendOutTime = 0.0f,
		float InPlayRate = 1.0f,
		bool bEnableAutoBlendOut = false
	);
};

.cpp

// Fill out your copyright notice in the Description page of Project Settings.


#include "Cpp_BPFL_Animate.h"
#include "Kismet/BlueprintFunctionLibrary.h"
#include "Animation/AnimMontage.h"
#include "Animation/AnimInstance.h"
#include "Animation/AnimSequence.h"

UAnimMontage* UCpp_BPFL_Animate::Cpp_BPFL_Animate_PlayAnimation(
	UAnimInstance* AnimInstance, 
	UAnimSequence* AnimSequence,
	FName SlotName,
	float BlendInTime, 
	float BlendOutTime, 
	float InPlayRate,
	bool bEnableAutoBlendOut
)
{
    if (!AnimSequence || !AnimInstance)
    {
        return nullptr;
    }

    int32 LoopCount = 1; // Number of loops, it should only play once

    // Create the dynamic montage
    UAnimMontage* NewMontage = UAnimMontage::CreateSlotAnimationAsDynamicMontage(
        AnimSequence,
        SlotName,
        BlendInTime,
        BlendOutTime,
        InPlayRate,
        LoopCount
    );

    // Edit the necessary variables
    NewMontage->bEnableAutoBlendOut = bEnableAutoBlendOut;

    // Now play the created montage
    if (NewMontage)
    {
        AnimInstance->Montage_Play(NewMontage, InPlayRate);
    }

    return NewMontage;
}

This made the montage stay in position after it plays

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.