How to make unique Animation node in Inherited scenes

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

Hi,
each of my Inherited scenes randomize its position in _ready() function. After this it changes first point of Sprite position [Animation] track to the same value

var anim = $Animation.get_animation("move_to_pos_zero")      
var track_pos =anim.find_track("Sprite:position") 
anim.track_set_key_value(track_pos,0, $Sprite.position)

The problem is with the reference of [Animation] node. Each of my “child” scenes inherit the same [Animation] node, so each new inherited node override animation position-track value of other nodes.

screen

For example: I create three of Inherited scenes. Each scene randomize its position on init and set the same value to position-track(id 0) of [Animation] node. After this each node plays “move_to_pos_zero” and animates its position to (0,0). Unfortunly first and second initialized nodes teleportate to position of last created node and do the same animation.

The question is how to make [Animation] unique?

Sorry for my English. I hope you understood what I meant.

:bust_in_silhouette: Reply From: Reisflocke

An Animation is inherted from Resource, so you can use all methods from Resource on your Animation.
This let’s you use duplicate().

So you could do:

var anim = $Animation.get_animation("move_to_pos_zero") .duplicate()

But then you would have an entire new Resource, so you would have to set this new Resource as the Resource in use for you AnimationPlayer:

$Animation.add_animation("my_animation_name", anim)

(don’t forget, that you old animation “move_to_pos_zero” is still in the list of the AnimationPlayer, so you can’t use that name for you new animation)

But to achieve your goal I would rather use Tweens, as you can give them a start and an endpoint and they whil interpolate from start to end over a give period of time. (Look into the docs for more information)