I'm not sure if it's a bug in Godot, a fault on my part, or a misunderstanding on how exactly signals work. So I decided to post my problem here.
I have the following situation:
I have an 'enemy' scene which consists of a parent node (type: KinematicBody
) to which an Area
is attached as a child. In the script of the Area
I connect both body_shape_entered
and body_shape_exited
signals to appropriate receiver methods. whenever somethind enters or leaves the area, it's been kept track of in an _area_cintents
array. Once the damage area's big moment has come, the AI calls a do_damage()
method which inflicts damage upon all nodes within the area.
Here's the simplified code:
var _area_contents : Array
func _ready():
_area_contents = []
connect("body_shape_entered", self, "_on_body_shape_entered")
connect("body_shape_exited", self, "_on_body_shape_exited")
func _on_body_shape_entered(_body_id, _body, _body_shape, _area_shape):
_area_contents.push_back(_body)
func _on_body_shape_exited(_body_id, _body, _body_shape, _area_shape):
var index = _area_contents.find(_body)
if index >= 0:
_area_contents.remove(index)
func do_damage():
for target in _area_contents:
if target is Damageable:
target.hurt()
The enemy is controlled by a small state machine which calls do_damage()
from the outside if the curcumstances are right.
This works pretty well as long as there is only one instance of the 'enemy' scene in my level. Once I spawn multiple instances, I observed the following behaviour once I digged deeper using the debugger:
The functions _on_body_shape_entered
and _on_body_shape_exited
are called properly and within their scope _area_contents
contains the expected nodes.
However, whendo_damage()
is called on the same entity _area_contents
is shown to be empty in the debugger and the function has no effect. _area_contents
isn't accessed anywhere else, only in those three functions in the Area
node script.
I'm currently using Godot 3.2.2 stable.
Is this intended behaviour and I'm missing something? Or could this be a problem in Godot? Any hint on what the cause of this might be would be deeply appreciated.