Question on how to reference certain variables globally

Godot Version

4.6

Question

I have a variable I tried to make global (I am aware of the Globals tab in settings, I did that first) , as I wanted to use in a different area, two actually, which I’m unsure how to reference in other scripts globally as I need the info in these variables to instance other objects in the same spot. One of them is in a custom function, so its not accessible by normal means, and I need the info of the variable to be accessed after the specific object is instanced to spawn other objects in that same position.

I have an example here of another variable that is not within a function though that i also am unable to access globally, and I’m unsure why its returning a null value:

#spawn_mesh is the variable I want to reference elsewhere, unsure how to make it so that I can do all of that outside of a function.

func spawn_enemy_instance():
	#var shuffle : Array = [point_1,point_2,point_3]
	
	for i in range(0,3):
		var spawn_mesh : Node = mesh_scene.instantiate()
		add_child(spawn_mesh)
		spawn_mesh.position = SpawnmeshGlobal.shuffle.pick_random()
		
		var spawn : Node = target_scene.instantiate()
			#if get_tree().paused:
		add_child(spawn)
		spawn.position = spawn_mesh.position

And here, is the variable I made to get 3 positions in 3d to instance spawn_mesh at. However, I wanted to make the shuffle variable a global one so I could use it elsewhere, but upon doing so I was given null value.


@onready var point_1: MeshInstance3D = %Point1
@onready var point_2: MeshInstance3D = %Point2
@onready var point_3: MeshInstance3D = %Point3

var shuffle: Array = [point_1,point_2,point_3]

#point_1,point_2 and point_3 are placeholder meshes in 3d space to mark positions to instance the spawn_mesh at

Basically I would like to know a.) How can/how should I reference arrays containing positions globally? and b.) How can I reference variables within specific functions elsewhere, and if not, how should I go about making the variable global so that I can get attributes from it?(such as position)

Thank you for reading.

Put variables you want to be global into a dedicated script and set this script up as an autoloaded node. Alternatively you can make those variables static and keep it in any named script. This will make them globally accessible via the script name.

I am aware of the first solution. I did that with the shuffle variable, I’m just a bit confused by why I got an error when referencing it in the same spot where the non-globally accessible version used to be. Nothing about the value has changed besides the fact I had to put shuffle outside of a script.

image

Post the code that causes that error.

I’m afraid that is the code that I already posted.

Which line is causing the error?

spawn_mesh.position = SpawnmeshGlobal.shuffle.pick_random()

I believe. This is where the error sends me.

print SpawnmeshGlobal.shuffle before that line to see if it’s initialized as you expect it to be.

It’s probably returning a value, (probably null based one the screenshot) that doesn’t match what property you’re assigning it to. Once it’s returning a Vector3 hopefully it will work right away.

Trouble shooting like normalized said is a great way to narrow the root cause down.

ah yes, it is initializing, but every value is null.. hm

Let’s see the code that initializes it.

If it’s the second snippet you posted then shuffle should also be declared @onready. Otherwise it’ll execute before the initialization of @onready variable it depends on, and values of those variables will be null.

Alright, I did that. oddly, it is still null.

What gets printed when you print the shuffle array?

[<Object#null>, <Object#null>, <Object#null>]

This means that your paths to point nodes are not valid. Can you post your main scene tree?

Try using regular paths to fetch those nodes. Scene unique nodes work only when accessed from within the same scene, and autoloaded node is not in any scene.

So: $"../Node3D/Point1

That said, this is a bit dodgy way to go about initializing it. Better to assign those node references to that global array from some code within your scene.