Print variable from another script

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

My game is made of 2 scenes: STAGE and PLAYER.
A have a variable called SCORE in the player script of the PLAYER scene.

I have instanced PLAYER into STAGE and I can play just fine and I can print the variable to the console and it updates perfectly.

The problem comes when I try to print the variable to a RichTextLabel in the STAGE scene so it is visible in the main game.

How can I access the variable SCORE from the RichTextLabel script?

Thank you so much!

:bust_in_silhouette: Reply From: Schweini

You have to use a singletons.
Singletons (AutoLoad) — Godot Engine (3.1) documentation in English When you have more questions ask me

:bust_in_silhouette: Reply From: Bert

One way to do this is by getting the PLAYER node in the RichTextLabel script:

var score = get_node("../Player").score

note that this only works if your node tree looks like this:

- Stage
  - Player
  - RichTextLabel

The “…” in the node path basically refers to the parent node.