Animation with script only plays for the last instance in a scene. Why is this?

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

I have a scene called MovingPlatform with an AnimationPlayer. I want to add an animation via script and export variables to change the position.x and time for each instance. When I use animation.track_set_path(track_index, "../Platform:position:x") all my instances are animated in another scene, but if I change for example anim_time for one in the Inspector tab, both are animated the same when playing the scene. And if I use for example this line of code instead animation.track_set_path(track_index, str(platform)+"/Platform:position:x") (see the comments down there) then only the latest instance is animated and the other ones aren’t moving. I hope it’s understandable. I’ll attach some pictures.

So my question is, how can I add an individual animation to each MovingPlatform, that I can use multiple times in many other scenes and change variables?

extends Node2D

@export var anim_pos_x = 100
@export var anim_time: float = 2.0

@onready var platform_node = $Platform

func _ready():
	var animation_player = $AnimationPlayer
	
	var animation = Animation.new()
	var track_index = animation.add_track(Animation.TYPE_VALUE)
#	var platform = get_path_to(platform_node)
#	var platform = platform_node.get_path()
	var platform = self.get_path()
#	animation.track_set_path(track_index, "/root/MovingPlatform/Platform:position:x")
#	animation.track_set_path(track_index, str(platform)+"/Platform:position:x")
	animation.track_set_path(track_index, "../Platform:position:x")
	animation.track_insert_key(track_index, 0.0, 0)
	animation.track_insert_key(track_index, anim_time / 2.0, anim_pos_x)
	animation.track_insert_key(track_index, anim_time, 0)
	animation.set_loop_mode(1)
	animation.set_length(anim_time)
	
	var animation_library = animation_player.get_animation_library("")
	animation_library.add_animation("MoveAnimation", animation)
	animation_player.play("MoveAnimation")

enter image description here

enter image description here

I think the correct path would be Platform:position:x. It gets a child node named “Platform”.

../Platform will get a sibling node and /Platform gets a node via it’s absolute path. See more in NodePath

umaruru | 2023-05-29 20:15

I get the errors get_node: Node not found: "Platform:position:x" (relative to "/root/MovingPlatform/Platform"). and _ensure_node_caches: On Animation: 'MoveAnimation', couldn't resolve track: 'Platform:position:x'. when writing Platform:position:x.

Nottblod | 2023-05-30 09:47

There’s the root_node property in the AnimationPlayer. The paths on the animation tracks should be relative to that. If Platform isn’t being found but ../Platform was, it suggests that the root node is not the parent (the default .. of the property), but the AnimationPlayer itself or one of its siblings. Other than that I don’t know what could be the cause of this problem

umaruru | 2023-05-30 17:33

:bust_in_silhouette: Reply From: Nottblod

I found another way using get_tree().create_tween() and doing it with tween_property. That’s working fine! But I’d still like to know if it’s possible with Animation like above.