0 votes

My tree:
-world
--player
---sprite
--sword
---sprite

Hi. I want to access sibling node(player -- sword)
player script:

var health=100

sword script:

onready var variable1=getnode("/root/world/player")
var player
health=variable1.health
func ready():
print(player
health)

i am getting a mistake== Node not found: /root/world/player.

Please help. Thanks

in Engine by (21 points)

1 Answer

+1 vote
Best answer

A better way to access the node would be by executing:

onready var player = $Player

such that you can call:

player_health = player.health

Doing it this way is more standard and can give you access to any of the children of the script holding node.

by (138 points)
selected by

Unfortunately yes

Can you put your code on google drive (or other so I can have a look please)?

Thanks

I have got it working! :)

So in your sword script you want something like this...

onready var player = get_parent().get_node("player")

func _ready():
    print(player.health)

and then clearly in you player script you need var health = 100 somewhere.

Let me know if this works!

WOOOOOWWWW! You are awesome. It's work. Thanks for everything.

No problem!

What might be good to read for the future when accessing other nodes is this page:
https://kidscancode.org/godot_recipes/basics/node_communication

Learning about signals will really open up your options

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 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 webmaster@godotengine.org with your username.