0 votes

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?

Godot version Godot 3.4
in Engine by (30 points)
edited by

1 Answer

0 votes

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.

by (236 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.