Animation in C++

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Rem

Hello,

So I have created 1 animation named « DetachThisPart » with an animationPlayer. (Really easy).
Now I want to know how I can call this animation in C++.
The idea is to make an if :
if(ThisValue=1) {
playAnimation(DetachThisPart);
}
Thanks for your help.

Sincerely,
Rem

:bust_in_silhouette: Reply From: Gluon

It would be something like this

public class Character : KinematicBody2D
{
    private AnimatedSprite _animatedSprite;

    public override void _Ready()
    {
        _animatedSprite = GetNode<AnimatedSprite>("AnimatedSprite");
    }

    public override _Process(float _delta)
    {
        if (put your check here as you desire)
        {
            _animatedSprite.Play("DetachThisPart");
        }
        else
        {
            _animatedSprite.Stop();
        }
    }
}

Isn’t this C#? I got a little confused when I saw C++ in the title.

Juxxec | 2022-12-02 10:15

Firstly, thanks for your answer.
I dont’’t’ have a KinematicBody.
I made a Rocket as a « Spatial » in Godot.
I attach 3 « MeshInstance » to this spatial to have
different part of my rocket.
I attach an « AnimationPlayer » to this MeshInstance.
Then in my code in C++ (i’ m not using script in Godot)
i want to play the animation.

Rem | 2022-12-05 10:25

:bust_in_silhouette: Reply From: Ohan

Assuming the AnimationPlayer is a child node of the node this script is attached to, it should be something like this

get_node<godot::AnimationPlayer>("AnimationPlayer").play("DetachThisPart");

Thanks for your answer.
But I have a problem.
I made a Rocket as a « Spatial » in Godot.
I attach 3 « MeshInstance » to this spatial to have
different part of my rocket.
I attach an « AnimationPlayer » to this MeshInstance.
Then in my code in C++ (i’ m not using script in Godot)
i want to play the animation but what you write give me :
« get_node was not declared in this scope »
« AnimationPlayer is not a member of godot »
« Request for member ‘play’ in ‘(« AnimationPlayer »)’, which is of non-class type ‘const char[16]’

Rem | 2022-12-05 10:23