I need to detect the area that get entered

Godot Version 4.3

Hello !! So i have centralized a mechanic inside a single script which isn’t inside any area. And i add to many area body_entered event to detect when my character enter in them. The code work , but the problem is that godot can’t give me the area that get entered. What would be the best workaround for performance ? Thank you

func _ready():
	var elastics = get_tree().get_nodes_in_group("Elastics")
	for elasticlist in elastics:
		for rope in elasticlist.get_children():
			var touch_part = Area3D.new()
			touch_part.connect("body_entered", Callable.create(self,"_on_body_entered"))

func _on_body_entered(body):
	print("Ok but which area has been entered ???")

You could bind the area to the callable, make sure to use 4.x connection and callable style. Hopefully you are also adding the Area3Ds as children of something with a collision shape or they won’t do anything anyways.

func _ready():
	var elastics = get_tree().get_nodes_in_group("Elastics")
	for elasticlist in elastics:
		for rope in elasticlist.get_children():
			var touch_part := Area3D.new()
			touch_part.body_entered.connect(_on_body_entered.bind(touch_part))


func _on_body_entered(body, area):
	print("Ok but which area has been entered ???")
2 Likes

Genius… thank you very much your your help !!