Best practices for static typed GDScript Signals and interfaces/abstract classes

Godot Version



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:

signal thing_happened(triggered_by: Node, message: String);

EventBusAutoload.thing_happened.emit(self, "yo!");

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:

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!

At least reading the source there is abstracted machinery to setup the signals on a class. In some sense I think it could auto complete for you, but it probably isn’t implemented to do so.

I know for a fact that when you use the editor to connect a signal it knows the argument names of the function signature. So at least some API is available to help the auto complete.

When a signal is added it is done via the ClassDB class. The types of the parameters are stored. From there it’s not clear how the machine works, (I would need to read the src more). I suppose there is a table of callables for each signal and it’s connections in the end.

1 Like

Hmm that’s interesting. I haven’t delved into Godot’s source yet (and to be honest, I think it will be some time until I do) but it sounds like when I’ve got a bit further along it might be worth doing. Maybe then, if no one beats me to it, I can look into implementing it myself and make a pull request. Again though, that’ll be some time, I’ve only been here a day. Haha.

Given that you started digging around in the engine code for a solution (thanks by the way!) I’m guessing there aren’t any commonly known work arounds?