I am making a custom plugin that has a RegionObject script which extends Spatial; I put class_name RegionObject at the top of the script file.

What this script is supposed to do is create two Area nodes with their own accompanying CollisionShape nodes and SphereShapes (The CollisionShape nodes are children to the two Area nodes, one to each). The first Area/CollisionShape/SphereShape are the largest, and look out for the second, smaller Area/CollisionShape/SphereShape of other instances of the script. In the script's _init function, I connect the first Area's signals area_entered(area) and area_exited(area) to separate functions at the bottom of the script file. The enter signal and exit signal functions have print("Area entered: ", area) and print("Area exited: ", area), respectively.

During testing, I created two instances of this script and added them to the scene tree. When moving one of them around, only the second instance, the one added last, has its signal functions called, despite both having their functions connected in the _init function.

Here is the RegionObject _init function code:

 func _init(_object : Spatial = null) -> void:
 	name = "RegionObject"
 	
 	add_to_group("RegionObjects", true)
 	
 	object = _object
 	
 	detector_area.name = "DetectorArea"
 	detector_area_col_shape.shape = detector_shape
 	detector_area_col_shape.name = "DetectorShape"
 	detector_shape.radius = detector_shape_radius
 	detector_area.add_child(detector_area_col_shape)
 	
 	add_child(detector_area)
 	
 	print("Connecting RegionObject signals")
 	detector_area.connect("area_entered",
 							self,
 							"_on_detector_area_entered")
 	detector_area.connect("area_exited",
 							self,
 							"_on_detector_area_exited")
 	
 	detection_area.name = "DetectionArea"
 	detection_area_col_shape.shape = detection_shape
 	detection_area_col_shape.name = "DetectionShape"
 	detection_shape.radius = detection_shape_radius
 	detection_area.add_child(detection_area_col_shape)
 	
 	add_child(detection_area)

If anyone has any idea why this is happening, and possibly have a way of fixing this issue, please let me know. Thank you!

I tested the situation you mentioned, and I feel that the problem you mentioned did not occur. Can you provide more information, such as a sample program?

@vmjcv

I tested the situation you mentioned, and I feel that the problem you mentioned did not occur. Can you provide more information, such as a sample program?

I've attached a zip file with a test project in it. There is a base player that is moved with the Arrow Keys. (Press escape to free the cursor.) I also added two simple RigidBodies with box shapes. As you move away from the boxes, both should disappear, with the Output log displaying the lines: Area in: [Area:1226] Area in: [Area:1226] Area out: [Area:1226] (That is what happens when I do a basic run.) What should be happening is all mesh instances (the two boxes and the player) should all disappear (go invisible) when they get too far apart. Turning on "View CollisionShapes" helps in showing the area they look. What happens when I run the test is that only one box turns invisible, as it should, while the other box and the player remain visible despite getting too far away from one another.

I hope I am making sense in all of this.

Okay, I figured out my problem. Unlike what I had thought, the problem was not with my _init function, but with the actual function called by the signal (_on_detector_area_entered, in this case).

In the function, I had the the if statement if not (area.get_parent() == self) and area.get_parent().name == "RegionObject" and not area.name == "DetectorArea":. Because Godot automatically modifies the name of objects added to the scene tree if the name is already taken (I had two-plus RegionObjects added to my scene tree, The first RegionObject was named just that. The second would have a name like @RegionObject@4 or whatnot (I do not fully understand where those numbers come from; point is they are added onto the name).

Anyone who comes across this wondering why signals across duplicates of a scene or custom node, make sure you are not checking against the a node/PackedScene with a specific name, especially if the node/PackedScene is added multiple times to the scene tree through code.

2 years later