Get the Animation State from C++

Is it possible to know the name of the current animation state in the animgraph of an animinstance.

Basically I have a character having an animinstance having an animation State. I want to check if the animation is in a particular state before taking an action. Is it possible to check?

After some digging I found about the AnimBlueprintGeneratedClass but when I tried to check if my animinstance is of that class, the results was false.



if (UAnimBlueprintGeneratedClass* AnimBlueprintClass = Cast<UAnimBlueprintGeneratedClass>(CoverAnimInstance))
{

I don’t know if there is a function for it already, but this should help you achieve what you want: (I don’t guarantee it will work because I have not tested it :D)



        int32 MachineIndex = 0; //Set the index of a state machine of which state you want to get

        if (UAnimBlueprintGeneratedClass* AnimBlueprintClass = Cast<UAnimBlueprintGeneratedClass>((UObject*)GetClass()))
	{
		if ((MachineIndex >= 0) && (MachineIndex < AnimBlueprintClass->AnimNodeProperties.Num()))
		{
			const int32 InstancePropertyIndex = AnimBlueprintClass->AnimNodeProperties.Num() - 1 - MachineIndex;

			UStructProperty* MachineInstanceProperty = AnimBlueprintClass->AnimNodeProperties[InstancePropertyIndex];
			checkSlow(MachineInstanceProperty->Struct->IsChildOf(FAnimNode_StateMachine::StaticStruct()));

			FAnimNode_StateMachine* MachineInstance = MachineInstanceProperty->ContainerPtrToValuePtr<FAnimNode_StateMachine>(this);

			FName StateName = MachineInstance->GetStateInfo().StateName;
		}
	}


Just put this code in some funtion in your class that derives from UAnimInstance.

EDIT: Unfortunately this won’t work because GetStateInfo() is protected.

Thanks for the reply… Let me try to see if there is a way to get it through blueprints…


FAnimNode_StateMachine* MachineInstance = MachineInstanceProperty->ContainerPtrToValuePtr<FAnimNode_StateMachine>(this);

1>C:\Users\Srikant\Documents\Unreal Projects\PSE_LYFE\Source\PSE_LYFE\Player\PSE_LYFE_AnimInstance.cpp(26): error C2065: 'FAnimNode_StateMachine' : undeclared identifier

I searched the classes AnimBlueprintGenerated class and related class but couldn’t find that structure.

It’s located in “Runtime/Engine/Classes/Animation/AnimNode_StateMachine.h” (Documentation: https://docs.unrealengine.com/latest/INT/API/Runtime/Engine/Animation/FAnimNode_StateMachine/index.html)

But as I have said it will not work because GetStateInfo() is a protected function. I have added a pull request to incorporate the GetCurrentStateName() into the engine: https://github.com/EpicGames/UnrealEngine/pull/1225

How to get an Anim graph’s current state name and time elapsed/remaining, etc.

In version 4.13 of the engine, I did it this way:
I subclassed AnimInstance and made my Anim Blueprint parent to my new subclass. Then in my Anim Blueprint’s BlueprintUpdateAnimation event I call into a native code Tick method in my subclass. In that Tick method I get the state machine by name (the one I wanted was named “BaseStateMachine” in the Anim Blueprint) followed by getting the FName of the current state:


	FAnimNode_StateMachine *BaseStateMachine = GetStateMachineInstanceFromName(FName("BaseStateMachine"));
	if (BaseStateMachine)
	{
		FName CurrentStateName  BaseStateMachine->GetCurrentStateName();
	}

Then you can compare the FName of the state to the one you are interested in:


	if (CurrentStateName == FName("YourStateName"))
	{
		// Do something
	}

You can then also get information about Time Elapsed/Remaining by calling the getter methods like this:


	indexBaseStateMachine = GetStateMachineIndex(FName("BaseStateMachine"));
	float TimeRemaining = GetRelevantAnimTimeRemaining(indexBaseStateMachine, BaseStateMachine->GetCurrentState());

My intent was to control all the anim state transitions from C++ code by changing an enum variable in code and making all the transitions in the actual Blueprint anim graph transition based on the enum variable.

Hi John,

I have a very specific (and justified) need to not only get the current animation and play position, but also to be able to set that on BeginPlay. It looks like what you’ve done (well done BTW) was make the first part possible. I was wondering if you knew off the bat how I can take that info and then use it to set the current animation and play position so a duplicate actor will start up in relative sync with the actor who I got the info from.

Thanks,