How can I pass values between scripts with signals

I have main script attached to the root node Inv_UI of the scene. In this script I have an array that consist of 2 other arrays and is prefixed with @onready. My original goal that I still want to achieve is to be able to access this declared variable slots from another scene and since I really do not want to do it through instancing the whole Inv_UI scene I tried to do it with an autoloader but that broke something and caused an error that I believe would force me to not use the @onready prefix which is needed. That is why I thought I could add a node with a script that would get the slots variable with a signal that is emitted at the time when slots has all the necessary values and then use that as an Autoloader but I am very new to godot and cant seem to understand how to make the signals work.

Here is the code of the root node Inv_UI:

extends Control

@onready var slots: Array =$Hotbar/GridContainer.get_children() + $Inventory/GridContainer.get_children()

signal SlotsReady (slots: Array)

func _ready():
     emit_signal("SlotsReady", slots)

And heres the code for the listening script which I plan to use as an autoloader for slots:

extends Node

@onready var SlotsArrayNode = $".."

func _ready():
	SlotsArrayNode.connect("SlotsReady", self, "_on_slots_ready")

func _on_slots_ready(slots: Array):
	print(slots)

Might help to use 4.x signal/connect format

extends Control

@onready var slots: Array =$Hotbar/GridContainer.get_children() + $Inventory/GridContainer.get_children()

signal SlotsReady (slots: Array)

func _ready():
     SlotsReady.emit(slots)
extends Node

@onready var SlotsArrayNode = $".."

func _ready():
	SlotsArrayNode.SlotsReady.connect(_on_slots_ready)

func _on_slots_ready(slots: Array):
	print(slots)

I believe this line is what fails to work.

SlotsArrayNode.connect("SlotsReady", self, "_on_slots_ready")

in 4.0 connect requires a “Callable” so the fix would be this, or again using the improved 4.x signal/connection syntax.

SlotsArrayNode.connect("SlotsReady", Callable(self, "_on_slots_ready"))
1 Like

Your singleton is initialize first and cannot access your slotsarraynode. What you can do instead is call the function of the auto load from your control ready function and pass the slots array as argumen.

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