Play Aninimation form C++, How to get current and remaining time?

I have some questions up on AnswerHub regarding this, but I don’t think that is working any more. Either there are so many questions posted that they disappear off the page before anyone even sees them or Epic is swamped with questions and can’t keep up. Here is the link to this question on there:

Basically, I want to know the correct way to call play on a montage in C++. I know the function to call it, but it’s just a one off.

Where should you call it so that you can check on it each tick? Do you have to override tick and look to see if a Montage is playing and poll it? Or is there a way to call a task/thread that will enable that, and then go away after it’s fininshed playing allready part of the API?

Secondly, I can get the total length of the animation, but how do I get the total time remaining? Is that part of the API, or do I need to set a timer when I first call play on the montage and then compare with the current time on each tick, then subtracting from the SequenceLength?

These seem like things that should be part of the API. I feel the ways I described are workarounds, which I’ll do as a backup. But, I wanted to find out if there was a non-hacky way of getting time remaining on a montage while in play.

Thanks for any input.

**Anim Instance
**
Everything you need is in the Anim Instance class!

You need to create a custom anim instance class for your project.

Then reparent your animation blueprint to use this custom anim instance class as its base class (File->ReParent)

Then checkout AnimInstance.h!

All the functions you want are there, including what the currently playing montage is, so you can then access any info you want about it!

You retrieve your custom anim instance at runtime from your character like this:



//inside Character class

if(!Mesh) return;
//~~~~~~~~~~~~~~~
 

UYourAnimInstance * Animation = 
  Cast<UYourAnimInstance>( Mesh->GetAnimInstance() );

if(!Animation) return;

//Now you can do stuff with your custom vars or functions in your anim instance class!

Animation->Celebrate(); //custom function in your anim instance C++ class



**My Anim Instance Tutorials**

Two relevant tutorials of mine here, **with C++ Code class structure and pics!**

https://wiki.unrealengine.com/Animation_Blueprint,_Set_Custom_Variables_Via_C%2B%2B

https://wiki.unrealengine.com/Animation_Blueprint,_Implement_Custom_C%2B%2B_Logic_Via_Tick_Updates

Anim Instance Functions relevant to your question



:
	/** Get Current Montage Position */
	float Montage_GetPosition(UAnimMontage* Montage);
	/** Has Montage been stopped? */
	bool Montage_GetIsStopped(UAnimMontage* Montage);

	/** Set position. */
	void Montage_SetPosition(UAnimMontage* Montage, float NewPosition);

	/** Get PlayRate */
	float Montage_GetPlayRate(UAnimMontage* Montage);

	/** Get next sectionID for given section ID */
	int32 Montage_GetNextSectionID(UAnimMontage* Montage, int32 CurrentSectionID);

	/** Get Current Active Montage in this AnimInstance **/
	UAnimMontage * GetCurrentActiveMontage();

	/** Get Currently active montage instace **/
	FAnimMontageInstance* GetActiveMontageInstance();



**Relevant Properties on FAnimMontageInstance, accessed via above functions**



```


UPROPERTY()
	float Position;

	UPROPERTY()
	float PlayRate;

	UPROPERTY()
	bool bPlaying;


```



Further Research
**
Check out all of
**
AnimInstance.h
AnimMontage.h

Enjoy!

:heart:

1 Like