Identify which node entered the Area2D

Godot Version

4.2.1

Question

In my game, the enemies can be damaged by 2 different types of nodes.

  1. Player bullet
  2. Supermove projectile

Both types of nodes are Node2D and have a CollisionShape2D.

The “Enemy” node has an Area2D, which uses the “on_area_entered” signal to determine whether the enemy is damaged.

My requirement is to differentiate the damage values based on which player projectile type (Player bullet or Supermove projectile) enters the Area2D of the enemy node.

Ex: The Supermove projectile should do more damage to the enemy than a player bullet.

Below is the base code of the enemy.

extends CharacterBody2D

class_name EnemyBase

enum FACING {LEFT = -1, RIGHT = 1}

const OFF_SCREEN_KILL_ME = 1000
@onready var animation_player = $AnimationPlayer

@export var default_facing: FACING = FACING.LEFT
@export var points: int = 1
@export var speed: float = 30
@export var enemy_health: float = 2

var _gravity: float = ProjectSettings.get_setting("physics/2d/default_gravity")
var _facing: FACING = default_facing
var _player_ref: Player
var _dying: bool = false



# Called when the node enters the scene tree for the first time.
func _ready():
	_player_ref = get_tree().get_nodes_in_group(Gamemanager.GROUP_PLAYER)[0]


# Called every frame. 'delta' is the elapsed time since the previous frame.
func _physics_process(delta):
	fallen_off()

func fallen_off():
	if global_position.y > OFF_SCREEN_KILL_ME:
		queue_free()
		
func get_damaged():
	
	enemy_health -= 1
	
	if enemy_health <= 0:
		die()
	else:
		return
	
func die():
	if _dying == true:
		return
	_dying = true
	Signalmanager.on_enemy_hit.emit(points, global_position)
	ObjectMaker.create_explosion(global_position)
	set_physics_process(false)
	hide()
	queue_free()


func _on_visible_on_screen_notifier_2d_screen_entered():
	pass # Replace with function body.


func _on_visible_on_screen_notifier_2d_screen_exited():
	pass # Replace with function body.


func _on_hitbox_area_entered(area):
	get_damaged()
	animation_player.play("damaged")

What would be the best solution for this query?
Thank you in advance.

You use the area parameter of the signal, that’s what was entered, and use it to check what you should do, if it’s the player or what ever

For example add a get_damage method to it and let it tell you how much damage to do

Can you please help with how to write the comparison?
Having a bit of a difficult time understanding the “area” parameter type in the “on_area_entered” method. And how to compare it with the object I need.

func _on_hitbox_area_entered(area):
	if area == player_bullet:
		...
	get_damaged()
	animation_player.play("damaged")

Or just:

area.get_damage()

And add a method to each bullet area

Thank you for replying to this thread.
I’ve managed to get this done with the below code in the “on_area_entered” signal method.

I’ve added the “player bullet” scene and “super move projectile” scene into separate groups and used those to identify which object entered the Area2D of the enemy character node.

func _on_hitbox_area_entered(area):
	#animation_player.play("damaged")
	if area.is_in_group("player_bullet"):
		get_damaged_by_bullet()
	elif area.is_in_group("supermove_projectile"):
		get_damaged_by_super()
1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.