I have an Area2D castle hitbox where each enemies entered causing overtime damage.
If there are 2 enemies inside, and I killed 1 enemy and removing the body using PhysicsServer2D.free_rid(enemy_RID), the Area2D on_body_shape_entered will be triggered again. So I have a duplicate of the other living enemy in my Array.
I can easily remove the duplicate, but I am worry if the removal causing the physic tick recalculate again causing unnecessary load. Is there any better way to remove a body using PhysicServer that is not re-triggering on_enter and even doing the whole physic recalculation?
I found the culprit.
It is not the removal process that causing the physic recalculation.
Upon the removal process, my code added replacement of enemy which trigger a creation of a new PhysicServer2D body.
I am re-using an instance of a “body_shape” variable to define the body.
Which is causing the unexpected behaviour (I’m not sure if it is another physics calculation or misbehavior of PhysicServer’s created bodies).
By creating a new shape for each new PhysicServer2D bodies, it fixes this issue.
I don’t understand why removing from the array effects the physics tick…
If you remove a body, it should not emit body_entered (I think), so I also don’t understand why it’s still added to that array..
You should have something like this:
func _on_body_entered(body: Node2D):
if not enemy_attacking_array.has(body): #to make sure you don't add the same enemy twice)
enemy_attacking_array.append(body)
func _on_body_exited(body: Node2D):
enemy_attacking_array.erase(body)