How to get a reference for $AnimatedSprite node from another scene

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

Hi,

I’m looking for a way to get a reference for an $AnimatedSprite that is in another Scene so I can play it’s animation.

I have several $AnimatedSprites that will be in the Level/Scene Tree that I need to play their animations.

I’ve been using Signals up to this point to communicate with other Scenes and I’m wondering if I can do the same for this, but I’m just a little uncertain of how I get the reference exactly.

Thanks.

:bust_in_silhouette: Reply From: godot_dev_

You could have a parent node (let’s name it sprites) that hosts all the AnimatedSprite children iterate over all its children for c in self.get_children():, and when the child is an AnimatedSprite if c is AnimatedSprite:, then you could add the AnimatedSprite to a dictionary or array (e.g. spritesArray.append(c).

Then, you could have a child (or a node somewhere else in the scene) signal the request to play an animation, along with an index to fetch the AnimatedSprite from the sprites node by connection the sprites node to a node that emits signals to request the play. Your sprites node could have a script with the following function to play one of the animated sprite

func _on_request_animated_sprite_play(animeSpriteId, animation):
     var spriteAnime = spritesArray[animeSpriteId] #fetch the sprite to animate
     spriteAnime.play(animation)# play a requested animation

All that’s remaining is to connect _on_request_animated_sprite_play to the appropriate signals and have nodes that control playing animation manage the animation name and spriteanimation id