Area2D collision get node of other collision object

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


I’ve got a bullet that’s an Area2D that collides with a KinematicBody2D and I can get the node it collides and call a function in that node to set the damage.

I want to do the same thing with the bullet colliding with another Area2D node.
I’ve set the Area2D node name the same name as the root node of the object, so the code below works.

var name = area.get_name()  
var mynode = get_tree().get_root().find_node(name,true,false)  

but I don’t like it and feel that the search could be slow at times (maybe from what I’ve read).

If there’s no other way, then I was going to get the other object to check what it’s been hit by and react to what ever a global bullet damage value may be.

Is there a better way to do this?



:bust_in_silhouette: Reply From: deaton64

So I went with plan B. If anyone is interested, this is what I’ve done.
I have a global variable called Laser1_damage, which I set to the damage I want that laser to inflict. As the game progresses, I can make the laser more powerful by changing that value.
The laser has an Area2D node called Laser1.

Then in the enemy node, the Area2D node has a signal for area_entered, which has the following code:

func _on_Area2D_area_entered(area: Area2D) -> void:
        take_damage(Global.get(area.get_name() + "_damage"))

The laser dies when it hits something and the enemy takes the damage.

Seems to work OK.