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.
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:
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.
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.