Accessing Unique Names in a Singleton

Godot Version

4.2.2.stable

Question

Accessing Unique Names in a Singleton

Hello! I have some unique names in my scene tree, such as %Player, %GUI, and %LootGUI.

I want to access them from a GameManager script that will handle signals that each of these nodes emit.

However, the results from the GameManager are always null. I can try a direct path to each node such as “root/Farm/Player” etc, but these can change if I load into a new scene like “root/inside_house/Player” correct?

Ive tried just using get_node(“Player”) but it still returns null. Ive tried some other more convoluted workarounds like call_deferred or await, but I dont want to overengineer this if theres an easier way or some solution I’m overlooking.

The main issue that started me down this path was: I was trying to emit a signal from %Player that the %GUI and %LootGUI can both pick up on and run functions on their end. However the GUI was able to pick up the signal but the LootGUI wasnt, despite both connecting to the Player signal the same way:

%Player.connect("interact_area_leave", Callable(self, "_on_player_leave_interact_area"))

Then I figured that a GameManager script that picked up these signals and appropriately delegated the function calls out after would be more streamlined. But I still run into the same issue of not being able to pick up the %Player node.

Game Manager Singleton:

extends Node

var player : Player
var loot_gui : LootGUI

func _ready():
	if player:
		print(player)

SCs for context:

image

Any help, insight, or general feedback would be greatly appreciated! Thank you!

I am thinking that you are running up against the unique name limitation documented here.
I question why you need access to the node itself?
I assume you have the player as
class_name Player

Set this variable to your player instance and you get access to the players signals.
In the players _ready() function set the GameManager.player variable.

#Player.gd
func _ready():  
    GameManager.init_player(self)
#GameManager.gd
funct init_player(value): 
    player = value
    player.interact_area_leave.connect(_on_player_leave_interact_area) 

func _on_player_leave_interact_area(): 
    loot_gui._on_player_leave_interact_area() # or whatever function you want to fire in LooutGUI
1 Like

This worked like a charm! Thank you so much!

Still learning a lot about Godot. This also helps me see, some flaws in other parts of my code that I can refactor and clean up.

Really, really appreciate it! :smiley:

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