Home > Enterprise >  how to store meta data in animation keys?
how to store meta data in animation keys?

Time:10-07

I have an animation track and I'm trying to store some associated meta data in keys

enter image description here

Now I could store it in the values of the keys like this:

track_get_key_value(track_idx,key_idx).set_meta("data","value")

but this only works for object type values like texture but not for other basic data types (like Rect2)
so is there any way to achieve what I'm trying to do?

why am I doing this?

I have some custom nodes with some custom properties & I don't want to animate them,
so I'm trying to store their values in the meta data of inbuilt properties of the nodes I extended from,
that way when I remove my custom nodes everything would still work

CodePudding user response:

I'm going to suggest a different direction: You can have a node that manipulates the AnimationPlayer.

For reference, see how you would use an AnimationTree, it has a NodePath called anim_player that you set so it know what AnimationPlayer it would work with.

Same idea. Make a custom Node that exports a NodePath to an AnimationPlayer, then it can manipulate the AnimationPlayer and monitor its state.

In particular, it can set property playback_process_mode of the AnimationPlayer to ANIMATION_PROCESS_MANUAL and then advance it with seek and advance from _process. So that your custom Node is in control of the position of the AnimationPlayer and can do other things in sync with it.

Some examples of things you can do with this approach:

  • Stop the animations at a particular time you want (no more using a timer to stop the animation early, which is not precise).
  • Loop a section of the animation.
  • Have a second hidden AnimationPlayer with extra animation data and play them both in sync.
  • Compute the value for other properties based on the current state of the animation.
  • Enforce preconditions to play animations.

We are, of course, assuming that no other code using seek and advance on the AnimationPlayer.

The rest of the code could interface with the AnimationPlayer as usual, or it could interface with your custom node.


I have a custom Node that does something similar to what I describe. It sets playback_process_mode to ANIMATION_PROCESS_MANUAL, however I still call play and stop normally. I can use seek to set the start time of the animation right after calling play...

The custom Node will read current_animation and current_animation_position to know where on the animation it is. And on _process it will do this:

    var current_time := _animation_player.current_animation_position
    next_time = current_time   delta
    _animation_player.advance(delta)
    _animation_player.seek(next_time, true)

I use both advance and seek as workaround for this issue: AnimationPlayer.advance() doesn't advance audio #48982.


Now given what you have described in other questions… I'm going to suggest that the custom Node could:

  • Extend AnimationPlayer (so it does not need a NodePath, since the AnimationPlayer is itself).
  • Create a second hidden AnimationPlayer.
  • Store extra information in the Animations of the hidden AnimationPlayer
  • And play the hidden AnimationPlayer in sync with it self by using seek and advance while playback_process_mode is set to ANIMATION_PROCESS_MANUAL.
  • Related