0 votes

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?

Godot version 3.4.4
in Engine by (72 points)

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

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.

????????? 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

Hi,

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

enter image description here

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

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.

Please log in or register to answer this question.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.