0 votes

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 onBulletareashapeentered(arearid, area, areashapeindex, localshapeindex):
var areabody = area.getoverlappingbodies()

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.

Godot version 3.5 rc3
in Engine by (33 points)

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

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??

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?

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 https://godotengine.org/qa/134862/getting-the-position-of-another-scenes-node , where instead of ladders the player should connect to enemy hitboxes.

Interesting thanks I'll try it out.

Please log in or register to answer this question.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.