Urgent: How do I call events separately for each instance of my Area2D node?

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

I have 2 scenes: the main scene and an Area2D scene. In my main scene I’ve created instances of the Area2D as shown below.

func _ready():
var k = area2D.instance()
for i in range (10):
var s = k.duplicate()
add_child(s)
list_players.push_back(s)
list_players[i].set_position(Vector2((150 * i),357))

func _input(event):

if list_players[0]._input(event):
	$Label.text = "one"
	print("one")
	
elif list_players[1]._input(event):
	print("two")
	$Label.text = "two"

… and so on

My Area2D has the following code:


func _input(event):
if event is InputEventMouseButton:
return true

I’ve been stuck with this for a long time now, I’ve done a lot of googling but I’m still really confused. I’ve tried making script, sub-resources unique, didn’t work.Every time I run the game, ‘one’ gets printed out 4 times (even though I’ve made 6 instances) no matter what I click on. I want the Area2D event to be run only when a certain event is clicked and the label to display corresponding text. What can I do? Please reply asap, this is urgent

Not sure what you’re trying to achieve. Do you have two different Area2Ds and want to respond to being clicked on? If so, it’s simple:

extends Area2D

func _ready():
	connect("input_event", self, "_on_input_event")

func _on_input_event(viewport, event, shape_idx):
	if event is InputEventMouseButton and event.pressed:
		print("Hello from " + name)

Add two area 2d in your scene, each with this script attach. Give them different names. You’ll see that clicking on one of them doesn’t trigger the other (unless they’re overlapping).

Bernard Cloutier | 2020-10-11 04:30