How to read all available states / input in a Transition from code?

Godot Version

4.6.1

Question

Hey there maybe I did not read carefully enough or “skipped” the parts in the tutorials / docs. But how the heck do I get a list of available states / inputs of a Transition (Node, not the things which connects them) inside an AnimationTree?

Once I know the name of a state I can just call:

_parent_animation_tree["parameters/idle_standing/transition_idle_standing/transition_request"] = "state_2"

and it will do the transition to the new idle state.

But what if I don’t know all available inputs / states in advance? Like allowing mods to add new character models which might have more or less idle states than my “default” characters? Or simply not all my NPCs having the same amount of idles themselves. Which already is the case.

My goal is just to read the states and pick one at random every 5 up to 10 seconds so the NPC do not simply loop all the same idles all over again.

I did this before without an AnimationTree but that process was tedious by reading all available animations there are in the AnimationPlayer and do all the queueing / playing and blending in code myself. I want to make my life easier before it is too late and just use AnimationTrees instead and simply control the respective Transition Node in the Tree.

My Tree Root Node is a AnimationNodeStateMachine itself.

Since the Editor knows the States and can display them in a drop down there has to be an easy way to read them right?

Something like:

_parent_animation_tree["parameters/idle_standing/transition_idle_standing/???"]

does not exist to get a list of states?

Greeting, NC

Always the same with me. Just after I posted my question I found the solution …

var node: AnimationNodeStateMachine = _parent_animation_tree.tree_root # Tree Root, top of AnimationTree
var sub_node = node.get_node("idle_standing") # Idel Standing Sub Node (see image above)
var transition_idle: AnimationNodeTransition = sub_node.get_node("transition_idle_standing") # The idles TransitionNode in question
var input_count = transition_idle.input_count # Number of states
var idle_idx = transition_idle.get_input_name(0) # Actual name of first state

Therefore, just a simple randi_range(0, count - 1) fetching the name and go for it. :sweat_smile:

1 Like