AnimationTree Advance Expressions and Enums/Constants in C#

Godot Version

4.2.1 Mono

Question

Hi.

I’m trying to build a basic animation state machine using AnimationTree and advance expression. So in the state machine script that is attached to the animation tree I have something like:

public enum AnimationState {IDLE, WALKING, JUMPING}

public AnimationState animationState;

Then in advance expression block I would put:

animationState == AnimationState.WALKING

However it just does not work in C#. I also tried constants but they also don’t work. The only thing that works is using the enum or constant value, as in:

animationState == 1

So I can’t seem to use either constant names or enum names, only numerical values. Which kind of defeats the reason for using expressions in the first place. RIght?

So, help? Please?

1 Like

For now I found a workaround by coding the state machine in GDScript and then calling it from C#. It’s a really ugly implementation for something as straight forward as using a constant or enumerator. Surely it can’t be that hard to fix the advance expression to recognize C# data structures?


public enum AnimationState {IDLE, WALKING, JUMPING}

public class MyClass
{
    public AnimationState animationState;
    public void Run()
    {
        animationState = (AnimationState)1;
        System.Console.WriteLine($"Animation state:{animationState}");
        animationState = AnimationState.JUMPING;
        System.Console.WriteLine($"Animation state:{animationState}");
    }
}

this how you use in C#?

        animationState = (AnimationState)1;
        System.Console.WriteLine($"Animation state is Jump?:{animationState == AnimationState.JUMPING}");
        System.Console.WriteLine($"Animation state is Walk?:{animationState == (AnimationState)1}");

        
        animationState = AnimationState.JUMPING;
        System.Console.WriteLine($"Animation state is Jump?:{animationState == AnimationState.JUMPING}");
        System.Console.WriteLine($"Animation state is Walk?:{animationState == (AnimationState)1}");

Sorry, I should have specified that I’m using the advance expression in the Godot editor, not in code. The parser for the editor does not play well with C#. I’ll just put everything in code and be done with it.

Okay, doing the state machine in code works fine so I just went with:

        switch(currentState)
        {
            case AnimationStates.IDLE:
                stateMachine.Travel("Idle");
                break;
            case AnimationStates.WALKING:
                stateMachine.Travel("Walk");
                break;
            default:
                stateMachine.Travel("Idle");
                break;  
        }

The reason I wanted to use the advance expression inside the Godot editor was to avoid using strings at all but alas, it is what it is. At least I don’t have to use ints so that’s something. I’ll put in a request on Github for the expression parser in the editor to be fixed.

1 Like

in .NET 6.x C# 10 you can use:

stateMachine.Travel(currentState switch
{
    AnimationStates.IDLE => "Idle",
    AnimationStates.WALKING => "Walk",
    _ => "Idle"
});

Thanks! That is definitely a step up from having to type out a bunch of case statements.