Node Communication

Godot Version

4.3.stable.mono

Question

Hey folks, I’ve got a question about node communication.

Currently my player is set up with multiple nodes as children to control different aspects of the player. One such child is an ability loader/assigner which loads the ability and assigns it a button. It receives information from the UI about which ability has been selected and which button it should be assigned to.

My UI has an abilities section, and then has children representing each category of ability. I would like to have the ability loader listening for a signal from the ability categories so that when a new ability is selected the ability loader knows to switch ability assignments.

Currently the ability loader has a reference to the player which has a reference to the UI and that’s how it is getting its information. But I think its messy and I would like to clean it but by listening for a signal. The problem that I have is connecting the signal from the ability category to the ability loader. Should I pass the ability loader a UI reference on _Ready() from the player and then connect it that way?

If anyone has a good idea or best practice on how to do this let me know!

If the scene is static use the editor to connect signals.

If the scene is dynamic i would designate a manager who can connect signals when new scenes are instanced.

Another approach for dynamic scenes is to have an autoload with an API for Singleton like classes to register a global signal and for observers to connect to a global signal. (With appropriate error handling of an observer can’t get the signal they want )

I should have mentioned that I am avoiding autoloading, because there may be multiple instances of the UI. Although I did have that implemented before and it worked well for its use case.

As for connecting signal via the editor that’s what I do when using a built in signal, but for this use I need custom signals to tell when the player has switched abilities in the UI. I think what I am going to do is have a method in my ability handler that subscribes to the signals it needs from the UI.

The player has a reference to the UI so it can pass that reference via that method on ready and then in that method the ability handler will subscribe to the signals it needs. That way the only thing that the ability handler can do with the UI is listen to it, and it will get the updates and information it needs to change the ability assignments for the player.

Thanks for the reply!