Get value from Variant in GDExtension

Godot Version

4.2.1

Question

I’m trying to get the animation state machine from the animation tree in GDExtension so I can control animations from C++. I have the following line of C++ code:

animation_state_machine = animation_tree->get("parameters/playback");

animation_tree->get() returns a variant, and the the animation_state_machine variable is the type AnimationNodeStateMachinePlayback*. How do I get the object out of the Variant so I can put it in a C++ type and call functions like travel() on it normally?

Variant’s are implicitly converted to Object’s.

In this case you could do:

AnimationNodeStateMachinePlayback* machine = Object::cast_to<AnimationNodeStateMachinePlayback>(animation_state_machine);

machine->travel("path");

And since AnimationNode... is a RefCounted object you wrap it with Ref<>:

Ref<AnimationNode...> machine = Ref<AnimationNode...>(Object::cast_to<AnimationNode...>(animation_state_machine));

machine->travel("path");

Let me know if that works.

1 Like

Thank you. This worked like a charm. I’ll also remember to wrap more stuff in Ref<>, since right now I’m using a lot of raw pointers.

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