godotengine / godot-proposals

Godot Improvement Proposals (GIPs)
MIT License
1.14k stars 93 forks source link

Static signals #10680

Closed AdriaandeJongh closed 1 month ago

AdriaandeJongh commented 1 month ago

Describe the project you are working on

A tower defense game called Rift Riff and soon a sequel to my indie hit game Hidden Folks.

Describe the problem or limitation you are having in your project

I use signals on singletons quite a lot:

GameManager.instance.some_signal

The (albeit minor) downside of this approach is that this requires the singleton object to be initialized and assigned. I rarely have issues with this particular pattern, but when I do it's usually when I run into tricky initialization order issues.

Describe the feature / enhancement and how it helps to overcome the problem or limitation

It would be awesome if I could define static signals like so:

class_name GameManager
extends Node

static signal some_signal

func _ready():
    some_signal.emit()

... allowing this signal to be accessible from anywhere like so – even from _init() functions in other classes before any singletons associated with the class in question have been initialized and assigned:

GameManager.some_signal.connect(my_function)

Describe how your proposal will work, with code, pseudo-code, mock-ups, and/or diagrams

As per usual in my proposals (sorry, I'm 'just' a game dev!) I don't know enough about the engine's inner workings to say anything meaningful here.

If this enhancement will not be used often, can it be worked around with a few lines of script?

I could simply continue to use the signal through my singletons.

Is there a reason why this should be core and not an add-on in the asset library?

Not sure if it's core or GDScript, but this is part of either of those.

tetrapod00 commented 1 month ago

I wonder if this would be helpful for implementing the common events bus pattern (https://www.gdquest.com/tutorial/godot/design-patterns/event-bus-singleton/)?

passivestar commented 1 month ago

Duplicate of https://github.com/godotengine/godot-proposals/issues/6851

dalexeev commented 1 month ago