Trying to query area that overlaps my bullet kinematicbody2d so I can access script variables

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

Hi,
I’m using a kinematic2d bullet (has collision shape) and I’m trying to apply damage to any enemies group area2d nodes (they have collision shapes) all layers and masks are good for collision.

func _on_Bullet_area_shape_entered(area_rid, area, area_shape_index, local_shape_index):
var areabody = area.get_overlapping_bodies()

I’m trying to figure out how to address the area in question to trigger methods, get/set variables etc but i’m drawing a blank in my research on how to manipulate these via an area shape entered.

Could you not apply damage every time an enemy’s area2D enters the bullet’s hitbox?

godot_dev_ | 2022-07-20 19:06

in this case, the signal response is on the bullet, the bullet holds the damage value etc

func _on_Area2D_body_entered(body):
	print (str(body.bodyname))
	match body.bodyname:
		"player":
			pass
   		"enemy":
			# If nothing matches, run this block of code
			if (body.group):
				print(str(body.group))
				print(str(body.get_property_list()))
			if body.is_in_group("enemies"):
				body.receive_damage(damage)
				queue_free()

The issue here is body.bodyname returns “bullet”, is it colliding with itself??

xdfo | 2022-07-20 19:42

Oh wait, do I have it backwards? Should the signal be connected to the enemy and then it queries body.damage to get the bullet damage?

xdfo | 2022-07-20 19:44

How I would do it is have the player connect to the enemy hitbox Area2Ds, and call a signal handler function with a meaninful name like _on_hitbox_hit_player, and then you can avoid a lot of type checking in the signal handler function. Assuming only Area2D’s with a damage member property are used to connect to the player, then you just simply apply the damage. For example, assuming _on_hitbox_hit_player is implemented in the player script, the below function illustrate how simple it could be if you connected your signals to the Area2Ds properly.

func _on_hitbox_hit_player(hitbox):
    self.receive_damage(hitbox.damage)

I gave an example of how to connect signals in a similar fasion as this case in Getting the position of another scenes node - Archive - Godot Forum , where instead of ladders the player should connect to enemy hitboxes.

godot_dev_ | 2022-07-20 20:50

Interesting thanks I’ll try it out.

xdfo | 2022-07-20 21:06