Godot 3 code in Godot 4 : connect("area_entered", self, "_on_area_entered")

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

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)
:bust_in_silhouette: Reply From: spaceyjase
area_entered.connect(_on_area_entered)

As always, covered by the documentation: GDScript reference — Godot Engine (latest) documentation in English

:bust_in_silhouette: Reply From: Cyber-Kun

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")

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

malaska | 2023-04-26 20:57

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

Cyber-Kun | 2023-04-27 09:01

sorry wrong thread

Cyber-Kun | 2023-04-27 09:43