How to add event for dynamic node?

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

how to add event for dynamic object?

func _ready():
    var csg = CSGBox.new()
    ... ?
    add_child(csg)

func _on_Area_input_event(camera, event, position, normal, shape_idx):

Thanks
But if there are many objects, how to find out which one was clicked ?

var spheres = [
	{"x": 1,"y": 1,"z": 1},
	{"x":-1,"y": 1,"z": 1},
	{"x": 1,"y":-1,"z": 1},
]

func _ready():
	for s in spheres:
		addCSG(s)

func addCSG(s):
	var cs = CollisionShape.new()
	cs.shape = SphereShape.new()
	
	var a = Area.new()
	a.add_child(cs)
	a.connect("input_event", self, "_on_Area_input_event")

	var csg = CSGSphere.new()
	csg.transform.origin = Vector3(s.x, s.y, s.z)
	csg.add_child(a)

	add_child(csg)

func _on_Area_input_event(camera, event, position, normal, shape_idx):
	if event is InputEventMouseButton:
		if event.button_index == BUTTON_LEFT and event.pressed == true:
			print(position, shape_idx)
  • connect doesn’t pass custom parameters
  • emit_signal - how to bind to onclick ?

ravend | 2022-09-06 14:08

:bust_in_silhouette: Reply From: Ev1lbl0w

When connecting in code, you can pass additional parameters. In this case, you can pass the object itself:

func addCSG(s):
    ...
    a.connect("input_event", self, "_on_Area_input_event", [a])

func _on_Area_input_event(camera, event, position, normal, shape_idx, area):
    # Now you have access to area as well