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 (144 points)
selected by

Sorry, but not work. This time I am getting a new error. == Invalid get index "health" (on base: "Nil")

New sword script:
onready var player=$player
var health=player.health

func _ready():
print(health)

After the ‘$’ sign, the word need the be the name of the node you want to reference, is the node called ‘player’ exactly like that? (No capitals etc)

Yes , it is. .

Do you want a screenshot?

So it looks as though the sword and the player are not a child of each-other. You’ll need to access the tree as a whole and then your player

Try using get_tree() and then attaining a child in the tree

A screenshot would be helpful

Yes. THe Sword and the player not a child of each-other. How can I do what you're saying

Make the sword a child of the player and try the original plan, the sword is a part of the player I assume so really it should be a child of it

main scene == WORLD (Node 2d)
WORLD's childs == player , sword
player's childs== Sprite , CollisionBox2d
sword's childs== Sprite, CollisionBOx2d

The player and the sword are child of world.
if i can "Make the sword a child of the player" This time sword and player move the together.

Do they need to be separate?

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