Assign animation to a custom resource

Godot Version

4.2.2

Question

Hello, I’ve made a custom enemy resource, and I want to assign each enemy their unique animations. How can I do that without loading all animations as separate Sprite2D nodes in the same generic enemy scene I assign the enemy resource to?
image


I tried to save animations I created in a different scene, and then assign to the enemy resource, but when I tried to use them in the generic enemy scene, the key frames were empty (assuming its because the animation.res, animation.tres and animation.anim did not save the Sprite2D keyframes, only the keyframe positions).
image

Animation resources save the path to the node for each track starting from the assigned AnimationMixer.root_node in the AnimationPlayer node.

For example:

  • Node2D
    • Sprite2D
    • AnimationPlayer (root_node = "..")

Path to Node2D/Sprite2D.frame saved = Sprite2D.frame because the root_node points to Node2D

If you try to load that Animation resource in a scene with a different structure like:

  • Node2D
    • Node2D
      • Sprite2D
    • AnimationPlayer (root_node = "..")

It won’t work because it won’t be able to find the Sprite2D (The path Sprite2D.frame starting from the root_node ".." would be ../Sprite2D.frame = Node2D/Sprite2D.frame which does not exist in this scene and won’t work)

Make sure that the scene where you create the Animation and the scene where you apply it have the same structure or that the AnimationPlayer root_node points to a node where the Animation path will be able to be found.

1 Like

For anyone else experiencing the same issue, my exact problem was that the Sprite2D name in the scene where I created the animation was different from the one I was importing it to, so really look through your scene paths thoroughly!

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