Accessing state information from State Machine used in Animation Blueprints in C++

Hi all,

How would I go about to access the current state in the state machine from Animation Blueprint (edited) from C++ ?

Many thanks.

Cheers,
darkZ

Unfortunately, and I think more to the point, there is not an overarching state machine to be used in the context of UE4, let alone C++ in general. You are able to pull off State like behavior if you use switches or lots of testing in your methods, but overall the answer is you don’t.

That said, you can find a lot of people with some very elegant solutions to how to organize and create a state machine in C++ on Stack Overflow and the web in general. Dr. Dobbs, Stack Overflow 1 Stack Overflow 2

One common practice is to define a set of states as Enumerations, and to use those values to branch your code:

void MyWeaponClass::OnMousePrimaryHit()
{
	switch(currentState)
	{
		case FIRING:
			FireRound();
			break;
		case RELOADING:
			CancelReload();
			break;
		case IDLE:
			BringUpIronSights();
			break;
	}
}

This type of coding opens the door to you being able to branch your code in interesting and descriptive ways:

  • FIRING_MousePrimary
  • RELOADING_MousePrimary
  • IDLE_MousePrimary

Each of these is at least telling when it is relevant, although the names arent terribly descriptive, assuming the use above is intended.

Are you referring to state machines used in Animation blueprints?

I set an enum on the AnimBlueprint, which I then switched off of in the State Machine.

But, I think you want to poll for info, or set directly. I’m not sure how to do that.

Yes sorry I should have mentioned. I am referring to states in the State Machine in an Animation Blueprint.

Thanks for your detailed information. Appreciate your time :slight_smile: but actually there is a State Machine in UE4. Its used in Animation Blueprints to determine poses. I have made a minor edit in my question to clarify that.

State machines per se as you are describing actually are quite familiar to me. I have implemented them in various forms in some of my publications such as : http://www.cs.ubc.ca/~van/papers/2013-SCA-diverse/index.html But these were all done in my own character simulator coded from ground up by myself.

What I am trying to do now is to have similar sort of control over the UE4 state machine used in animation blueprints to produce procedural animations not entirely like in the link above but more so derived from a data driven approach.

So far I have found hacks to pass information to states through variables, sometimes by even abusing terminology used in programming the engine. For e.g. using BlueprintPure functions for causing state information change where I violate the contract of the pure functions ( I believe ). But that’s because I don’t have a real way of accessing the states in the animation blueprint directly in C++.

Yeah I am thinking along those lines too. Also ideally at every time step I want to check what state I am in and what the possible set of next states are. Then I can take actions depending on this information at every tick.

Orthogonally, as a possible direction I was thinking if it could be possible to subclass the current state classes to have more description for my problem domain and then use those instead of the current states in the state machine. That is because every time I want to make some change to my logic I don’t want to go and manually edit all my 100 states and transitions manually by dragging and dropping pins.

So I have a way of accessing the said time machine now. It is quite hacky and I am sure there are a million better ways to do this, however for the lack of time for engineering here goes :

I am using a subclass of AnimInstance to create my own class for Animation Blueprints. This subclass has access to a variable called RootNode which is declared to be of the type FAnimNode_Base. However on runtime this object happens to be of the type FAnimNode_Root which inherits from FAnimNode_Base. Now FAnimNode_Root has a variable of type FPoseLink called Result which links the poses generated from an input to the root of the graph. FPoseLink derives from a class called FPoseLinkBase which contains a variable struct FAnimNode_Base* LinkedNode; Now this is the node that is linked to the root of the AnimGraph ( root is the final output node ). So if we can access it, we can traverse the graph and reach our State Machine if it exists in this graph. Unfortunately this variable is private and cannot be accessed from AnimInstance class. So I had to go down and make a change to the source code. In the class FPoseLinkBase I added the following member function :

FanimNode_Base* GetLinkedNode() {return LinkedNode; }

Then I can access the stateMachine from my AnimInstance class by using the following call.

((FAnimNode_StateMachine*)(((FPoseLink)(((FAnimNode_Root*)RootNode)->Result)).GetLinkedNode()));

In my AnimGraph I have the Statemachine directly connected to the root of the graph.

Now once I have access to the Statemachine I can use similar procedure to get access to any of the skeletal controllers and sequence players I define inside each state of the state machine. Hence I can have full control over the State machine as I can call functions in my code at each Tick which now have access to internals of the State machine.

Hope someone finds this useful.

2 Likes