• TutorialsGodot 4.X
  • This is how to use AnimationTree State Machine transitions with expressions

So, this will be a quick tutorial on how to use the Expression field in State Machine transitions. It is an incredibly powerful tool, but the knowledge on how to use it is missing in the docs at the moment. It is a feature that was added to Godot 4, so you probably won't find it in godot 3.
I recommend using these when doing State Machine transitions, as it is better than the other, more well known methods.

First thing we need is an AnimationTree node, this uses an AnimationPlayer node with animations.

1 - In the animation tree, select a node with script in Advance Expression Base Node. Advance->Expression will take ANY variables from this script, even if they are export or not, including things like velocity, which are part of the node itself.

2 - For the next step, we need a State Machine, this can be anywhere in the AnimationTree. In my case, it's inside a blendTree:

3 - Inside the State Machine, we select a transition:

4 - In the transition there are many options. Make sure our Advance->Mode is set to Auto. Finally, we can type an expression in the field below Expression:

What's an expression?
An expression could be anything you put after an if, it should return a boolean, a value that can be true or false. The docs explain it in more technical detail: https://docs.godotengine.org/en/stable/tutorials/scripting/gdscript/gdscript_basics.html

Expressions¶

Expressions are sequences of operators and their operands in orderly fashion. An expression by itself can be a statement too, though only calls are reasonable to use as statements since other expressions don't have side effects.

Expressions return values that can be assigned to valid targets. Operands to some operator can be another expression. An assignment is not an expression and thus does not return any value.

Here are some examples of expressions:

2 + 2 # Binary operation.
-5 # Unary operation.
"okay" if x > 4 else "not okay" # Ternary operation.
x # Identifier representing variable or constant.
x.a # Attribute access.
x[4] # Subscript access.
x > 2 or x < 5 # Comparisons and logic operators.
x == y + 2 # Equality test.
do_something() # Function call.
[1, 2, 3] # Array definition.
{A = 1, B = 2} # Dictionary definition.
preload("res://icon.png") # Preload builtin function.
self # Reference to current instance.

Identifiers, attributes, and subscripts are valid assignment targets. Other expressions cannot be on the left side of an assignment.

5 - In the script of the node we selected, we can create variables and use them in the Expression, we can also use variables that belong to the node, such as velocity.

6 - In Expression, type an expression using our variable, like:
isAttacking
not isAttacking
velocity.y < 0.5
a > 0.5
a == 0.5

And That's it!, when the variable is changed the State Machine will change according to the expression, this allows us to use the variable for different transitions and gives us complete control over the states of the animation.
Hope this helps someone.

a year later

Can we use multiple conditions in the expression?
Like this one:
velocity.length() == 0 and is_on_floor()