How to get variable from other node in scene

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

In the main scene, I have a node (player) that is a scene. I also have a label in the main scene. Every frame I want the label to show a variable inside the player script.

My tree in the main scene
Here is the thing in the main scene, I am trying to use the script in “label” to access a variable in “player”

:bust_in_silhouette: Reply From: Ertain

Since we don’t have your code, it’s difficult to help with your situation. This is one example of how things may be written.

# Our player scene which has a script attached to it. The script takes the base properties of the player scene and does something more with it, i.e. they "extend" the node. Our variable, "some_variable", has a value we've made. This property can be accessed through the "." operator. The value of the property can then be set (that is assigned) to the "text" property in the Label.
some_label.text = the_player.some_variable

I’m trying to find a way to locate the player node. I tried directly locating it but it won’t let me.

PizzaMaker | 2022-10-29 21:20

The node is located relative to its place in the scene tree. If the player node is a child of the scene’s top-most node, then the script can reference it by using the “$” operator:

some_label.text = $player.some_variable

Ertain | 2022-10-29 22:03

:bust_in_silhouette: Reply From: PizzaMaker

I finally figured it out after a long day of pondering. I instead added a script to the main scene node and I can use that to access everything in the scene.

Please tell me if this is good practice