0 votes

Hey guys, I've been trying to create a hitbox and a hurtbox program.

My design logic is that every hitbox is a combination of one or more than one Area2D node. Also, the Area2D node can be freely added.
This is my Hitbox code:

extends Node

onready var hurtboxes: Array = []

#the message to send to hurtbox
var message: Dictionary = {}

func _ready() -> void:
    add_to_group("hitbox")
    #looping through all the Area2D(hitboxes) node it possesses
    for node in self.get_children():
        if node is Area2D:
            var hb: Area2D = node
            hurtboxes.append(hb)
            hb.monitoring = true
            #connecting body_entered signal
            hb.connect("body_entered", self, "_on_hurtbox_entered")


func _process(delta: float) -> void:
    pass


func _on_hurtbox_entered(body: Area2D) -> void:
    #this function wasn't triggered as it should
    if body.get_parent().is_in_group("hurtbox"):
        body.connect("hit", self, "get_hit")


func get_hit(dict: Dictionary) -> void:
    #hit logic
    pass

and this is the Hurtbox code:

extends Node

onready var hurtboxes: Array = []

#storing the message from the hitbox
var recieved_message: Array = []

func _ready() -> void:
    add_to_group("hurtbox")
    #looping through all the Area2D(hurtboxes) node it possesses
    for node in self.get_children():
        if node is Area2D:
            var hb: Area2D = node
            hurtboxes.append(hb)
            hb.monitoring = true
            #connecting the hurtbox signal
            hb.connect("body_entered", self, "_on_hitbox_entered")


func _process(delta: float) -> void:
    pass


func _on_hitbox_entered(body: Area2D) -> void:
    #getting hit
    if body.get_parent().is_in_group("hitbox"):
        body.connect("hit", self, "get_hit")



func get_hit(dict: Dictionary) -> void:
        recieved_message.append(dict)

So if any of the hitbox's Area2D child node detects a hutbox's Area2D child node, it should trigger the onhitbox_entered() function. But the function was never triggered when I tested it.

Any help would be appreciated!!!

Godot version v3.3
in Engine by (18 points)

I suppose you tested(with some print messages for example), and the actions in "gethid" not triggered. I think you need to define the signal "hit" which you use to connect the "gethit" method. Also if the problem is in connect you' ll have some error messages in "debug" tab on editor when you run your project, which may can help to understand what is wrong.

I've tried printing some message in the onhurtbox_entered() function and it didn't print, so obviously, the function was not triggered.

So the "on_entered()" functions not triggered. Do you have any other control nodes in the scene? Then maybe some other control node steal the focus. Try(if this is the case) to set the Filter in Mouse property of that node(or nodes) to "Ignore".

My Hitbox scene is a Position2D node attached with a script and also the same as the hurtbox, also, I got a Player scene with a Hitbox node in it and I also added an Area2D node a child of my Player's Hitbox node. I do this because that I can modify the shape of my hitbox determine on the attack I use. I'm new to Godot, so I don't know if my description makes sense or not but for me I see that I got 3 scenes: Level scene(contains a Player), Player scene(contains Hitbox node), and Hitbox(or hurtbox) scene. And may I ask that what does it have to do with Mouse Property and what is Mouse Property? Pls forgive me for asking this stupid question.

From your description, seems that you haven't any control node. Control nodes(button, textureButton, textureRect for example) have a mouse property.
https://docs.godotengine.org/en/stable/classes/class_control.html

So, now I've changed my Level scene from a Node2D to a Control type and I also set the mouse property to ignore in the inspector tab. The thing is that the event is still not getting triggered.

No, you misunderstood. You don't need to change to a control node. I mentioned control nodes because they can cause area2d events to not triggered if exist in the scene, so you need to filter the mouse property. If you haven't any, then the problem caused by something else.
Did you check the debugger' s errors tab when you run the project? Maybe there are some errors there which can help. And also there is "pickable" property in area2d which needs to be enabled.

I just managed to trigger the event by adding a bullet to test. By the way, thanks for taking your time to answer me and so far I've learned some new things.

1 Answer

+1 vote

I just managed to trigger the event after creating another bullet class with hitbox node in it. Although I still don't understand why it didn't work when I test it with the player but now it works.

by (18 points)
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 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 webmaster@godotengine.org with your username.