cak3_lover
cak3_lover

Reputation: 1958

How to uniquely identify animations?

I'm trying to access Animation's but so far the only method I could think of was:

tool

...

    set_meta("animation_path",["NodePath/to/AnimationPlayer","Animation Name"])

    var ani_data=get_meta("animation_path")
    var animation=get_node(ani_data[0]).get_animation(ani_data[1])

but the problem is that if I change the Animation Name I'll have to reset the value, so is there no way to store a unique id for Animation resource?

I tried storing get_instance_id() & using instance_from_id() but that doesn't work when I restart the game engine

Upvotes: 0

Views: 601

Answers (1)

Theraot
Theraot

Reputation: 40295

If the Animation is saved

Given that Animation is a Resource, I believe you can use the resource_path (given the Animation is saved).

The resource_path represents where the Resource is saved, and thus it persists when you restart the game engine.

Also, it would be unique for each Resource (it is possible to have multiple Resources saved in the same file, but then the resource_path of each Resource points to a sub-resource of the file).

And yes, the resource_path does not change when you rename the Animation.


The other idea that comes to mind is that the Animation itself has metadata which you could use.


If you will not be using the animation name, you would have to iterate over the Animations of the AnimationPlayer to find the one you want.

I suppose that if you use the resource_path, you could load it and get the animation name form there. You could also be getting the same instance… That happens if you are getting it from cache.


If the Animation is not saved.

First of all, storing metadata on the Animation should still work. Except, of course, it still means to iterate over the Animations to find the correct one.

If the goal is to not do that, then you can hold a Dictionary with String for keys and Animations as values, and keep it somewhere you know there is only one instance. For example:

  • Store it in an EditorPlugin.
  • Store it in an Autoload.
  • Store it in a specific Resource which you preload.
  • Store it on a const. Godot will admit Dictionary and Array as consts. Making them consts means you cannot set them… But they are still mutable. Furthermore, consts are shared among instances.

Upvotes: 1

Related Questions