Connecting signal between scenes

Godot Version

v4.2.1

Question

Okay, what in the holy guacamole am I doing wrong. I have two scenes for my game. One scene updates the index of the saved pet when the UI arrows are pressed. Looks like this:

extends Node2D

# Current selected pet index
var selectedPetIndex: int = 0

func _on_ready():
	updatePetLabels(selectedPetIndex)

#FORWARD ARROW
func _on_forward_arrow_input_event(viewport, event, shape_idx):
	if event.is_action_pressed("LMB"):
		unset_active_option()
		selectedPetIndex = (selectedPetIndex + 1) % PetStorage.get_all_pets().size()
		GlobalSignalBus.selected_Pet_Index_Changed.emit("selectedPetIndexChanged", selectedPetIndex)
		set_active_option()
		updatePetLabels(selectedPetIndex)

#BACK ARROW
func _on_back_arrow_input_event(viewport, event, shape_idx):
	if event.is_action_pressed("LMB"):
		unset_active_option()
		selectedPetIndex = (selectedPetIndex - 1 + PetStorage.get_all_pets().size()) % PetStorage.get_all_pets().size()
		GlobalSignalBus.selected_Pet_Index_Changed.emit("selectedPetIndexChanged", selectedPetIndex)
		set_active_option()
		updatePetLabels(selectedPetIndex)

func unset_active_option():
	petNameLabel.visible = false
	petBirthdayLabel.visible = false
	
func set_active_option():
	petNameLabel.visible = true
	petBirthdayLabel.visible = true

func updatePetLabels(index: int):
	var pet = PetStorage.get_pet(index)
	if pet != null:
		petNameLabel.text = pet.name
		petBirthdayLabel.text = pet.birthday
		petNameLabel.visible = true
		petBirthdayLabel.visible = true
	else:
		petNameLabel.text = ""
		petBirthdayLabel.text = ""
		petNameLabel.visible = false
		petBirthdayLabel.visible = false

func _ready():
	petNameLabel.visible = false
	petBirthdayLabel.visible = false
	set_active_option()

I then have a simple autoloaded script:

#GlobalSignalBus.gd
extends Node
signal selected_Pet_Index_Changed(index: int)

And then I have another scene that should be listening to the input from the first and change accordingly.

extends Control

@onready var petNameLabel2 = $MainInfoPetName
@onready var petBirthdayLabel2 = $MainInfoPetBirthday

var selectedPetIndex: int = 0  # Declaration of selectedPetIndex

func _ready():
	GlobalSignalBus.selected_Pet_Index_Changed.connect(_on_selected_pet_index_changed)

func _on_selected_pet_index_changed(index: int):
	print("Selected pet index changed to:", index)  # Debugging print statement
	selectedPetIndex = index
	updatePetLabels(selectedPetIndex)

func updatePetLabels(index: int):
	print("Updating pet labels for index:", index)  # Debugging print statement
	var pet = PetStorage.get_pet(index)
	if pet != null:
		petNameLabel2.text = pet.name
		petBirthdayLabel2.text = pet.birthday
		petNameLabel2.visible = true
		petBirthdayLabel2.visible = true
	else:
		petNameLabel2.text = "no index"
		petBirthdayLabel2.text = "no index"
		petNameLabel2.visible = false
		petBirthdayLabel2.visible = false

Now, first of all, I’m getting an error and have no idea why:

res://main/Resources/main_middle_section.gd:19 - Compile Error: Identifier not found: GlobalSignalBus

Second of all the script that should be receiving the information from the signal is not doing so, because the labels don’t display anything, neither does anything get printed in my desperate attempt to debug the thing.

Any help would be greatly appreciated!

Change this

GlobalSignalBus.selected_Pet_Index_Changed.emit("selectedPetIndexChanged", selectedPetIndex)

to this

GlobalSignalBus.selected_Pet_Index_Changed.emit(selectedPetIndex)

Changed, still no reaction in the second script.

I think the issue is you need to emit the signal from the globalsignalbus autoload script.

I was trying to do something similar in C# and came up with this problem.

So you would create a function in the globalbus script that emits the signal and then from the first script call that function instead. Which even if it isnt necassarry is better OOD design as the globalbus is doing the work.

You could try removing GlobalSignalBus from autoloads in ProjectSettings and adding it again. I’ve had some weird issues with autoloads that I was able to fix by doing that.

It should work without all that though. I have a global signal bus and I emit signals like this:

SignalBus.my_signal.emit()

Works perfectly

Hmm, so basically emit GlobalSignalBus.selected_Pet_Index_Changed.emit(selectedPetIndex) from the singleton itself?

Fair enough, I couldn’t get it to work under C# like that, it was behaving the same was as the OP suggests.

I removed and re-added it into the autoload. Still nothing, unfortunately.

Unfortunately, I can’t think of anything else that could help. There is one (already closed, so it should be fixed) issue with similar error message but it’s related to tool scripts (Edit: and it’s very old issue anyway, didn’t check the date)

Alright, managed to figure it out thanks to an awesome person in one of the Discord communities. If anyone stumbles upon this post in the future - make sure that your script is actually attached to the tree. My scene was being accessed through StageSwitch.goto_scene(“res://Info/info.tscn”) instead of being directly in the tree. Putting it into the “main” scene resolved all issues.

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