Identifier "global" not declared in the current scope

Godot Version

4.2 1

Question

Hello, I'm very new at programing and I'm making a simple RPG game following a few tutorials. I currently have some problems with the code because I'm trying to call a script that I called "global" to another script and it just keeps telling me that "global" isn't declared in the current scope and I don't know what to do because in the tutorial this problem doesn't happen, can someone please help me?

Here’s the code I used (the script that I’m trying to call is named “global.gd”):

extends Node2D

func _ready():
pass # Replace with function body.

func _process(delta):
change_scene()

func _on_bar_transition_point_body_entered(body):
if body.has_method (“LeazarSprings”):
global.transition_scene = true

func _on_bar_transition_point_body_exited(body):
if body.has_method (“LeazarSprings”):
global.transition_scene = false

func change_scene():
if global.transition_scene == true:
if global.current_scene == “Mundo 1”:
get_tree().change_scene_to_file(“res://bar.tscn”)
global.finish_changescenes()

And here’s the global script:

extends Node

var current_scene = “Mundo 1” #Mundo 1 bar
var transition_scene = false

var player_exit_bar_posx = 0
var player_exit_bar_posy = 0
var player_start_posx = 0
var player_start_posy = 0

func finish_changescenes():
if transition_scene == true:
transition_scene = false
if current_scene == “Mundo 1”:
current_scene = “bar”
else:
current_scene = “Mundo 1”

if you want global to be accessable from anywhere in the project then you have to add it as an autoload: project → project settings → autoload tab

3 Likes

Thank you for your response. I did add it as an autoload in the beggining and I tried to re-do it because it wasn’t working. I still don’t know what’s wrong.

Did you name your autoload “global”? It’s the name that you give the autoload is what you have to call in your code, not the name of the script file.

You can print you autoloads like this:

for autoload in get_tree().root.get_children():
		if autoload != get_tree().get_current_scene():
			print(autoload)

If it’s ok, yours should print something like
global:<Node#73635202458>

1 Like

Thank you so much for your help, the problem is solved. :slight_smile:

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.