Godot Version
4.2.2
Question
Hi everyone,
I’m very new to Godot but have a background in software development. I’m trying to design the architecture for my first Godot game using GDScript. I really like the rapid iteration capabilities of GDScript and how well it ties into the editor, so I’d prefer to use it over C#, but having read the best practices documentation and enabled type hints I’m still having some challenges around static typing.
Specifically, when I connect up signals:
# event_bus_autoload.gd
...
signal thing_happened(triggered_by: Node, message: String);
# broadcaster.gd
...
EventBusAutoload.thing_happened.emit(self, "yo!");
# listener.gd
...
EventBusAutoload.thing_happened.connect(_on_thing_happened);
...
func _on_thing_happened(triggered_by: Node, message: String) -> void:
#do something
I get no type hints/intellisense from the editor when passing arguments to .emit() or .connect(). Worse than that, I get no compilation error if I pass incorrectly typed arguments to .emit() or attempt to pass a method which has the wrong parameters to .connect(). Having looked around the forums, I think this might be expected behaviour, but if anyone can give me a workaround I would really appreciate it. So far the best I’ve come up with is this:
# event_bus_autoload.gs
signal _thing_happened(triggered_by: Node, message: String);
func thing_happened(triggered_by: Node, message: String) -> void:
_thing_happened.emit(triggered_by, message);
and then call the public func instead of emitting the “private” signal. This does give me type safety when emitting, but unfortunately not when connecting. I can’t pull the same trick when connecting because (as far as I have seen) you can’t specify types for a Callable’s parameters (is this true?).
Related to this - I’m also very interested to know if there’s a way to make _private_members actually private, and in any workarounds/editor plugins that tackle the lack of interfaces and abstract classes. I’d love to find a way to cause compile time errors when, for example, accidentally assigning an “abstract” base class to something instead of a derived class. I appreciate the composition over inheritance ideology reduces the need for interfaces and abstracts, but I would still like to be able to use eg. the strategy pattern and unfortunately, I’ve not found a type safe way to this so far in Godot.
These may just be limitations I will have to get used to, but I thought it was worth asking first.
Thanks for reading!