State Tree Logic and Animations Issues

Hello !

I’m learning how to make an AI and I’m struggling to understand how to setup a communication between the State-Tree, Tasks, AI controller and Animation Blueprint…

My current system does the following :

My AI controller detect the player and set some basic informations as variables, such as, Player Location, Player Last Seen location, Player lost and so on…
It uses an ENUM to set the different logic states : Idle, Search, Attack…

My ABP for my NPC uses a state machine and retrieve the variables to enter the correct animation states.

My state tree get the logic states information
and start Tasks to do some actions.

example : If LogicState = Chase, then start Task “go to last seen player location”
The task will have the AI move to location system.

My Issue is for the shooting system :

If i make a task “Shoot” it would be practical to retrieve my NPC variables and do the shoot logic in the task directly, but the issue is that i think its Read only, and i cant set variables of the owner NPC from the task, which makes it annoying to start the proper animation in my “ABP State Machine”.

I would need the task to do a part of the shooting logic, and still have setting variables inside my AI Controller, which splits the logic and make it super confusing to debug.

This makes the State machine useless in this case.

When i use the “State Tree” Tasks to move my character its fine, because the “ai move to” sends info to the NPC to move, and the ABP retrieve the velocity and direction to start the proper animation.

But when i need a shooting animation, i need a “Is Shooting” bool to be set to TRUE in the AI Controller to start the proper animation.

How would you set a proper communication between State Tree Tasks and the ABP ?

Okay, so if anyone get theses kind of issues in the future i pretty much got it figured out. (i think, feel free to correct me if im doing it wrong)

Basically, it is a very bad idea to drive an Animation “State Machine” with a “State Tree

It works on specific actions, like walking or running, because inside a Task, you can do AI Move to and that will force the AI Controller to move around, then you can use the change in velocity to trigger some walking animations in a “State Machine” in a Walk Blend Space for example.

For other actions like ; Shooting, Throwing a Grenade, Jumping and so on, it is just better to Play a Montage inside your task.


The logic i Have is the following ;

Perception system in the AI Controller :

This is a bit personalized because i use the plugin ; Advanced Sight.
https://www.fab.com/listings/d341ebda-400f-46fa-8b54-4046d77c142e

But basically, depending on my perception information, i change an Enum value that represent the Alert Status it contains states like Search Player, Attack Player, Aim at Player, Idle.

The Enum have a Onrep Notify system that triggers an event and trigger a State Tree event with tags matching the current Enum state

For example, on Begin Play, My Controller switch the Enum to Idle.
When the player is detected, Enum goes to Attack Player, and when attacking, if the vision of the player is lost, the enum goes to Search Player.


State Tree :

The State Tree Start by Root, and then goes to Idle.
When the Idle State transitions receive the State Tree Event with the Tag Attack Player It instantly stops the Idle State and switch to the Attack State

That allows me for force stop any Active state.

Same logic for the Search Player State, when Attack Player state is active → transition rule ; On Event Tag : Search Player to the go to the Search Player State.


Tasks :

When the Idle State is running in the State Tree, it fires the related Task.
This is a custom Task called Task Idle.

In the Task, On Event Enter State, it will start a random Ai Move To, to create some kind of patrol, then it goes to finish task.

On event Exit State, i use Stop Movement on the AI controller, so if the Alert Status change to Attack Player, the State Tree Event with Tag : Attack Player is recieved by the State Tree and the State Idle transition to the State Attack, just like mentionned above, but it also stop the movement when the Idle Task is interrupted.

For the Shooting Task ;
On Event Enter State : Play montage Shooting in a loop (for automatic fire)
On event Exit State : Stop Montage

This allows for montages to play when the state is active and stop when the state is interrupted by a logic change in the AI.


Animation Blueprint :

The ABP State Machine is only used for walking animations inside of a Blend Space using a Cast to the NPC character Movement Component to retrieve its ground velocity and direction.


Conclusion :

The State Tree or the related Tasks Can’t set variables, for the NPC or for the ABP.
It is used to receive variables and Special State Tree Events with tags used to force interrupt a state into another one.
The Tasks handle the logic for actions and play montages to animate theses actions.
The variables needed for specific Tasks are created in each Task and set as Instance Editable and then Set to the appropriate Character variable via a link inside the State Tree.

I hope this could eventually help someone.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.