Why it couldn't find Globals?

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

I was working on the networking on the UI

func _on_player_connected(id):
	var Globals.player2ID= id
	var setup = preload("res://screens/Setup.tscn")
	get_tree().get_root().add_child(setup)
	hide()

and i keep getting this error:

Parse Error: The identifier “Globals” isn’t declared in the current scope.

I just set up Globals.gd before I made the menu

var player2ID= 1

Can it connect to Globals?

:bust_in_silhouette: Reply From: dewcked

In case when player2ID is constant (not variable)

If you haven’t autoloaded Globals.gd, autoload it. After autoloaded it, set class name on the top of the script like this.

extends ~~~
class_name Globals

const player2ID = 1

Now you can access that as global scope variable.

In case player2ID is variable…

It’s possible if only node that containing Globals.gd script and node that containing script that has _on_player_connected(id) are instanced in the Scene.get reference from one node to another node and you can set/get the value.