|
|
|
|
Reply From: |
njamster |
The answer depends a great deal on where exactly the script snippets you provided are located exactly. I’ll assume the following tree for my examples:
Stage1 (or Stage2 later on)
- Player
- HUD
- Score
- HP
Now if you’re running a script attached to the Player-node and want to get the scorelabel no matter if Stage1 or Stage2 is active, you could do:
get_parent().get_node("HUD/Score")
Analogous if you attach a script to the HP-node and want to get the player’s hp regardless of the currently active stage and its name, you would do:
get_parent().get_parent().get_node("Player").hp
or (the same thing but shorter):
get_node("../../Player").hp
This works around your problem by providing a relative path (starting in the node the script is attached to) instead of an absolute path (starting in the root-node). However, it will still fail if you change your tree without changing the pathes, e.g.:
Stage1 (or Stage2 later on)
- Node2D
- Player
- HUD
- Score
- HP
In order to make your scenes completely independent of their exact position in the tree, you would simply emit signals on certain events (scoring a point, getting hurt, etc.) that other nodes interested in that information can connect to.
I used your way for Score and It’s fine but in Life Bar Script of player I keep getting error that No “Player” node found.
func _physics_process(delta):
value=get_tree().root.get_node("Stage1").get_node("Player").playerhp
This works fine but just for stage 1.
This is Image of My Stage 1 Scene
This is Image of my Player Scene
I have no idea how to get value stored in playerhp in the Life Bar script without using the previous statement containing Stage 1.
Scavex | 2020-04-18 13:30
Assuming your “Life Bar Script” is attached to the node PlayerProgressBar it’s:
func _physics_process(delta):
value = get_parent().get_parent().playerhp
# or value = get_node("../..").playerhp for short
njamster | 2020-04-18 13:46
You are a true champion !
Scavex | 2020-04-18 13:51