Get the Length of a Section in AnimMontage

I have this code to play an AnimMontage or a particular section of it:

float MyClass::PlayAnimMontage(class UAnimMontage* AnimMontage, float InPlayRate, FName StartSectionName)
{
	USkeletalMeshComponent* UseMesh = GetPawnMesh();

	if (AnimMontage && UseMesh && UseMesh->AnimScriptInstance)
	{
		float Duration = 0.f;

		// If the Montage is not playing it must be started
		if( !UseMesh->AnimScriptInstance->Montage_IsActive(AnimMontage) )
		{
			Duration = UseMesh->AnimScriptInstance->Montage_Play(AnimMontage, InPlayRate);
		}

		// After it has been started (if required) in case a Section has been explicited it is played
		if( StartSectionName != NAME_None )
		{
			// Jump to the specified section of the montage
			UseMesh->AnimScriptInstance->Montage_JumpToSection(StartSectionName);
		}

		return Duration;
	}

	return -1.f;
}

I’m not sure this is the correct use of sections but, if it is, I need to get the duration of a specific section. In the way above it always give me the Duration of the whole AnimMontage.
Is there a way or I am doing something that shouldn’t be done?

Another way to solve my problem is by using BranchPoints or AnimNotifies but at this point I don’t know how to catch a notify directly from code instead than from a blueprint how is shown in the AnimMontage online docs.

I have found now that exists the function:

GetSectionLength(int32 SectionID);

directly in the UAnimMontage class.
This can solve well the problem.

to measure a section you can use

/** Makes a montage jump to a named section. */
	UFUNCTION(BlueprintCallable, Category="Animation")
	void Montage_JumpToSection(FName SectionName);

	/** Makes a montage jump to the end of a named section. */
	UFUNCTION(BlueprintCallable, Category="Animation")
	void Montage_JumpToSectionsEnd(FName SectionName);

and the function

/** Get Current Montage Position */
	float Montage_GetPosition(UAnimMontage* Montage);

and subtract

:slight_smile:

Rama

1 Like

Thanks, this work but is a little inconvenient for what I want to do.
I think I will try with Anim Notifies the would probably fit best.
Thanks.

For anyone in future there’s a video tutorial for this problem: