Godot Version
4.3.stable
Question
I am trying to figure out the best architecture when using signals for collision.
There are two ways I can think of handling collisions:
- Game Objects detect the player and emit a signal
- Player detects everything and emits signals
Assume that the Player has an area2d for detecting objects. Assume the objects all have area2d.
Option 1 makes logical sense because each object would be responsible for sending out its own signal. This keeps everything nicely organized. The problem is that every new object that gets made needs to have its signal connected (to the level or to a GameManager script). So if I want to add 100 coins and 25 spikes, I’d have to manually connect 125 signals. This is tedious and prone to error if I forget to connect a signal or accidentally disconnect one.
Option 2 has the player handle all collisions so I only have to connect the player’s area_entered signal one time and it handles everything. The downside of this method is that as more types of objects are added to the game, the bigger the player’s on_area_entered function gets. It also makes less logical and organizational sense to me.
Example:
func _on_area_entered(area: Area2D):
if (area.is_in_group("coin")):
coin_collected.emit()
if (area.is_in_group("diamond")):
diamond_collected.emit()
if (area.is_in_group("spike")):
spike_hit.emit()
if (area.is_in_group("acid")):
acid_hit.emit()
...
I’m wondering if there is some other model or architecture that I’m not aware of that I should be using instead of either of these methods. If not, I’d like to know which of these methods is considered superior.