How to access top level variables from child node scripts?

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

Hello all. I made a button that lets players mute the sound effects in my game. The variable “FXon” is in the top level Node/Panel’s GDscript. Two layers below that, I have a child node with its own GDscript like this:

Node/Panel (a) -> Node/Control (b) -> Node/Control (c)

I tried this:

onready var globalVars = get_node("/root")
if globalVars.FXon == 1: do_stuff

Every time it got to that line of code, the game hung. So how can I read a’s FXon value from c’s GDscript? Thanks.

:bust_in_silhouette: Reply From: njamster

With get_node("/root") you will get the root-node of the tree which is a Viewport-node. When you run a scene, that scene will be added as a child of root. So if (a) is called “main”, the correct path would be get_node("/root/main").