This site is currently in read-only mode during migration to a new platform.
You cannot post questions, answers or comments, as they would be lost during the migration otherwise.
0 votes

I am trying to set up a default hitbox and hurtbox template as in a youtube guide.
The guide uses godot 3 and I´m using godot 4. So i need some help how to do this in Godot 4.
The main issue are with how connect() work in godot 4. I have tried some variants but not found a working solution so far.
I get 3 error codes, 2 of the saying that i cannot use string in connect() and the last is when having self inside the connect()

The code so far are:

class_name MyHurtBox
extends Area2D


func _init() -> void:
    collision_layer = 0
    collision_mask = 2


func _ready() -> void:
    connect("area_entered", self, "_on_area_entered")

func _on_area_entered(hitbox: MyHitBox) -> void:
    if hitbox == null:
        return

    if owner.has_method("take_damage"):
        owner.take_damage(hitbox.damage)
Godot version Godot 4.02
in Engine by (12 points)

2 Answers

+2 votes
area_entered.connect(_on_area_entered)

As always, covered by the documentation: https://docs.godotengine.org/en/latest/tutorials/scripting/gdscript/gdscript_basics.html#signals

by (1,406 points)
+1 vote

reference the object wich the signal is coming from. say you had a button and you were hooking that up

func _ready():
$Button.button_pressed.connect(function_to_perform)

func function_to_perform():
print("button_pressed")
by (110 points)

I prefer the syntax of this answer (with correct indentation of course) - Works great!

thank you you may select it as the best answer. also I'm sorry but I wrote the code in the browser and tab indenting doesn't work well like that

sorry wrong thread

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.