Godot best practices for signals?

Godot Version

4.2.2

Question

I’m currently going through a tutorial on Godot. As I’m working through signals, I’m not exactly happy with how signals are set up. Namely, I’m not happy with the multiple methods of setup and the way it’s set up in code using strings.

I wanted to get a sense of how others used signals.

  1. I’m never a fan of strings. It’s easy for typos, makes refactoring more difficult, and more. Does anyone do anything to aid with this, something like:
# constants.gd

var SIGNAL_NAME = "signal_name"
signal signal_name
    
# object.gd
    
emit_signal(SIGNAL_NAME)

Even this feels weird, but it keeps the refactoring/possible mistake spot close together. Or is this a YAGNI thing and folks typically stick with passing a string name around?

  1. Do you prefer to keep your signal setup in code or use the interface (storing in the scene file)? I’m leaning towards instantiate all in code so I know where to find them. However, maybe folks find it easy to just find across both the scene and code files.

Maybe this way?

#Constants:
signal my_signal
signal my_signal_with_params(a: int, b: int)

#Object:
Constants.my_signal.emit()
Constants.my_signal_with_params.emit(1,5)

Edit: Works only if Constants is an autoload or are reference to an instance. Not sure if you wanted to emit signal from something that is not in the scene tree.

1 Like

Thank you, I kind of like that solution. I do see what you mean, and do see some problems with a constants file approach. It’d mean all the contents would get loaded per script, possibly solvable by breaking apart into object1_constants, object2_constants, etc. Which then feels like it could go into YAGNI territory…

I think I’ll play with doing it the way you’re suggesting and see how it feels.