The Godot Q&A is currently undergoing maintenance!

Your ability to ask and answer questions is temporarily disabled. You can browse existing threads in read-only mode.

We are working on bringing this community platform back to its full functionality, stay tuned for updates. | Twitter

0 votes

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?

in Engine by (40 points)

2 Answers

0 votes

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.

by (1,434 points)
0 votes

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

by (2,018 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.