How you use AnimationTree/StateMachine, Condition and Expresion?

Godot Version

4.3

Question

Hi, how you actually use those as i find it really complex/confusing ?

image

I am trying to switch between two states in AnimationNodeStateMachine in the AnimationTree.

I can’t get it to work and honestly i am kinda confused on how to use Condition and Expression properties in the transition settings.

The only way i got it working is by having two bools: walk and fly and using them like this:

// I am setting the two variables to play the fly anim:
anim_tree.set("parameters/conditions/fly", true)
anim_tree.set("parameters/conditions/walk", false)

image

// And later i switch the bools to play the walk animation:
anim_tree.set("parameters/conditions/fly", false)
anim_tree.set("parameters/conditions/walk", true)

image

When i try to use the inverted value of “fly” instead of the second “walk” parameter like so, it doesn’t work:
image

What should i use in the Condition actually? The docs are not really clear to me.

Also for the Expression, why this is not working:
image

How i suppose to use Expression ? The docs are not even mentioning this.

If you have any idea, please share a quick tip or an example.

To use expressions it requires the expression node to be set.

Personally I like to make it the root of the scene. And basically it allows you to write expressions ( and even call functions) as if you were writing script on the expression node itself.

Example:

Extends CharacterBody3D

enum {
NORMAL,
FALLING
}
var state = NORMAL
func _phisics_process(delta):
   if velocity.y < -100:
      state = FALLING

then if you have the character node as the expression node you can make an expression like this in the transition of the state machine.

state == FALLING

As for the condition and expression as you are trying to utilize. I would try making the animation tree the expression node.(This could be the default)

But it may be:

conditions.flying == false

As conditions is a dictionary that holds your custom variable flying.

It could also be

parameters.conditions.flying == false

The expression field can be crazy handy for making very minimal code animation transitions. I think it’s a very underused feature. I made a video explaining it which might help demystify some of its usage.

But essentially, you can type any condition in there to be evaluated either by a node you choose, or in a script you set up, and if that condition is true, it transitions automatically.