Global script isn't giving out updated variables

Godot Version

4.5.1.stable

Question

I made a script that makes an array of an X and a Y value. It prints just fine in the script itself, but when I try to print it out on other scripts, its blank.

Global script:

var cords : Array = []
var current_y: int = -1
var x: int = 0

func _ready():
	for child in get_children():
		if child is Button:
			child.mouse_entered.connect(_on_hovered.bind(child))

#get the Y cord
func _on_hovered(button: Button):
	var y = int(button.name)
	current_y = y

#get the X cord
func _process(_delta):
	if current_y != -1 and Input.get_last_mouse_velocity() != Vector2(0,0):
		var global_pos = get_viewport().get_mouse_position()
		var local_pos = global_pos - global_position
		x = round(local_pos.x / 85)
		cords = [x, current_y]

Other script:

func _process(_delta):
	print(ClickCords.cords) # always returns [] 

Any help is greatly appreciated :]

Add this to the top of your script:

class_name ClickCords

Then it should work fine.

Apologies, I forgot to mention that the global script is an autoload singleton. Adding the class_name throws an error that it hides the autoload.

Put prints(self, self.get_path()) into _on_hovered()

LaneContainer /root/main/Map/LaneContainer

To clarify: main is a Node2D, Map is a TextureRect and LaneContainer is a VBoxContainer. It’s descendants are 5 buttons for the Y-axis.

Where’s ClickCords in all this?

It’s in a seperate scripts folder:

res://scripts/click_cords.gd

I mean the autolad node, not the script.

Apologies haha, it’s in the LaneContainer.

What does that mean? Show your remote scene tree and your autoload setup.

Here you go, hope this can clear things up a little:

That’s local scene tree. We need to see the “Remote” one that exists at runtime.

I see. Sorry for not understanding your questions, I’m very new to all this

Is the first script you posted click_coords.gd? Is that script attached to LaneContainter?

Yes. The second script has a little more to it, but eitherway it’s located at S_Button (if that helps)

The first script is attached to LaneContainer.

Let’s try again. Put print_rich("[color=orange]", self.get_path()) into _ready()

This is what it prints out:

/root/ClickCords
/root/main/Map/LaneContainer

Why it’s not orange? :wink:

There’s your problem. You have two instances of the same script running on different nodes while you think you only have one.

1 Like

Oh, well it was orange, it just didnt carry over when I pasted it in the message

I know, I was just kidding.

1 Like