Connection doesn't work on an instanced scene

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By mmarramarco
:warning: Old Version Published before Godot 3 was released.

I am trying to detect any event (in this case, a mouse click) on my tile. They look like this :

Tree :
Area2D

  • Sprite
  • CollisionPolygon2D
  • Label

And here is the script, attached to the Area2D :

extends Area2D

func _on_Area2D_input_event( viewport, event, shape_idx ):
	if event.type == InputEvent.MOUSE_BUTTON and event.button_index == BUTTON_LEFT and event.pressed:
		var label = get_node("Label")
		label.set_text("Clicked")

(This is correctly connected, if you wonder)
According to internet… this should work. But this is a tile of a board game, and I instance these tiles by doing :

var tile_scene = load("res://Scenes/hextile.tscn") #tile scene
... 
var tile = tile_scene.instance()
... 
add_child(tile)

And it seems that instancing these doesn’t allow me to change the label of my tiles. I also tested with other similar event, such as mouseenter() and mouseexit(), but with the same result : nothing change.

It works fine for me. I’ve done nearly exactly the same thing a few times in one of my games, and I just tried your code just to be sure and no problems. Are you sure the Area2D input signal is connected, and that you haven’t changed the size of the collision box?

Michael Paul | 2017-12-05 07:55

I’ll check that soon

mmarramarco | 2017-12-05 15:14

My function is connected to the signal “input event”, under the CollisionObject2D part. Is it the correct connection or did i miss something?

mmarramarco | 2017-12-05 16:22

That’s the correct connection… Do you get an error, or just no response from the click?
You can try mapping the left mouse button in the input map to a reference like “LeftButton” and change the first line in the function to “if Input.is_action_pressed(“LeftButton”):” and see if that works…

Michael Paul | 2017-12-05 23:13

Sorry for the late answer, I had a busy week. No, I don’t get any error. I just tried to remap left mouse as you proposed, without any amelioration.

mmarramarco | 2017-12-09 11:54

finally found something that work, I change my area to a kinematic body 2D and pickable to on. don’t know why only this one work.

mmarramarco | 2017-12-09 12:36

Hey, glad you found a workaround. If the kinematic worked where Area2D wasn’t, it’s almost certainly an issue with the CollisionPolygon2D which provides the actual “pickable” area. If you need to go back to an Area2D model, make sure the polygon is drawn properly. Cheers.

Michael Paul | 2017-12-09 16:21

Found something even funnier (and got back to the Area2D model with this) You just need the polygon to be… the first element of your subtree. For no reason.

So my tree is :

Area2D

  • CollisionPolygon2D
  • Sprite
  • Label

And now it works fine. Thanks for the help through the week.

mmarramarco | 2017-12-10 12:28

:bust_in_silhouette: Reply From: allanmakhoul

I just had a similar problem where none of this worked, but I solved it.

My problem was that I had a ColorRect as a simple background. I had to change the ColorRect setting Mouse → Filter to Ignore. Solved it for me.