How to access another Scenes node and player an animation

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

HI,

I’ve found this a bit confusing, as not all examples I’ve found are for playing an animation.

I have a Spike Scene, which has an Area2D as root. It detects if the Player enters it.

I have a Player Scene, which has is a KinematicBody2D and has an AnimationPlayer node with animation.

Both the Spikes and Player nodes are Instanced in a Main Scene/Level.

I just wish to know how to access the Player Scene with it’s AnimationPlayer node and play the animation.

I have seen the method frequently that I’m told isn’t ideal, using (…/) to move up the Scene Tree, which seemed quite easy. Other examples show a way to make the Player Scene available to other scripts, but I didn’t find an example that was to do with animation. It may be similar.

Could someone show me what to do in GDScript please?

var player

func _ready():
           # note = you need to know the player path
        player = get_tree().current_scene.get_node("Player")
        pass

func example():
    print(player.position.x) ## etc
    pass

ramazan | 2022-07-02 07:33

Thanks, but how do I access the Player’s Animation, how do I write the path?

Normally I’d write:

$AnimatedSprite.play("Player_Flash")

But that only accesses this node, not the player one

$AnimatedSprite.play......

Obviously I can’t write:

$AnimatedSprite.play.player.....

I’m a little unsure what to do here.

JayH | 2022-07-02 09:59

??? If I understood your question correctly
for example
player scene

  • Player
    –AnimatedSprite

##############

var player

func _ready():
    player = get_tree().current_scene.get_node("Player")
    pass

func example():
     var player_anim = player.get_node("AnimatedSprite")
     player_anim.play("Player_Flash")
pass

########## find Path
for example
player scene

func _ready():
      print(get_path())
    pass

ramazan | 2022-07-02 10:26

Hi,

Thanks for the help. I’m not getting errors, but the animation isn’t playing, perhaps I’m missing something.

JayH | 2022-07-02 10:51

this is where you want to connect to the “Player” scene.

example : add to enemy scene

var player

func _ready():
player = get_tree().current_scene.get_node("Player")
pass

func example():
 var player_anim = player.get_node("AnimatedSprite")
 player_anim.play("Player_Flash")
 pass

ramazan | 2022-07-02 14:00

That’s it! My bad, I was attempting to animate the Player’s AnimatedSprite node, I needed to animate the Player’s AnimationPlayer node. Didn’t realise how they are tied together.

JayH | 2022-07-03 10:24