0 votes

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.

in Engine by (32 points)

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?

I'll check that soon

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

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.isactionpressed("LeftButton"):" and see if that works...

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.

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.

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.

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.

1 Answer

0 votes

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.

by (14 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.