Functionality scoping concern

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By nightrobin

For example:
I have an ENEMY entity and BULLET entity. The bullet hit the enemy. Both of them have collision.
The enemy’s health will be reduced when hit.

1st approach:
The enemy have a function that accepts the bullet that enters its collision area, it takes the bullet damage value. Then it will compute for the damage and apply it to the enemy.

2nd approach:
The bullet have a function that accepts the enemy it collides to. Then it will compute for the damage to be applied to the enemy.

3rd approach:
There is a central manager/script that handles have list of enemies and bullets that collided. It will take the hit bullet’s damage and apply it to the affected enemy.

What is the better approach? Or other suggestions that is much better than these?

:bust_in_silhouette: Reply From: godot_dev_

I would go with approach 3. It seems more flexible since it will support separating the modularity of dealing damage vs. collision detection. That is, your central manager script could handle all the collisions at once, which will give more flexibility to add more logic in the future (some enemies might be immune to certain types of projectiles, so the collisions can be ignored and those invincible enemies don’t need any logic to deal with that). Your central manager script could then apply the damage after resolving all the legal collisions. This also will let you properly order how and when damage is applied. For example, if some bullets can be deflected, approach 1 and 2 may have trouble dealing with the case where a bullet damaged someone the same processing frame that it was deflected, but with a central management script you can add priority and what not.

In my opinion:

Handling a list of collisions gives you more flexibility of controlling when the result of a collision is handled, while handling a collision immediately after a signal was emitted makes it more difficult when handling many collisions at the same time and may lead to different results depending on which signal was handled first.

:bust_in_silhouette: Reply From: magicalogic

I would definitely go with approach 1. It’s more logical to me and more straight forward to implement than approach 3.