#Area2D not properly detecting mouse input.

31 messages · Page 1 of 1 (latest)

granite prairie
#

To be honest, the issue I have was already discussed on this reddit post, but he never seemed to have solved his problem. Here's a TL;DR for anyone who doesn't wanna go through:

I have a scene with an Area2D and a CollisionPolygon2D, and I want to detect mouse clicks in that area.
I connected the input event through the editor, and this is the function it's connected to:

func _on_input_event(viewport: Node, event: InputEvent, shape_idx: int):
    if Input.is_action_just_pressed('left_click'):
        print('clicked!')

of course, left_click is a valid action on the input map.
The problem is that it doesn't detect clicking of any kind. I've tried things like if event is InputEventMouseButton and it doesn't pass, except for under one odd condition.
The only time a mouse event (other than motion and scrolling) is detected properly, is the exact same frame that the mouse enters the Area2D.
Finally the real kicker; my code functions properly without any issues on a brand new project. Btw my original project is just a prototype, so it was also freshly created yesterday.

Something that wasn't mentioned on the reddit post but is something I noticed for my case, is that my .tscn file is "corrupting". Well, at least the editor doesn't want to open it because it thinks it's corrupted, but like I said the exact same file (copied over) works just fine on a brand new project.
I didn't mess with any settings on the original project either, so I don't understand why it's acting this way at all.

The prototype was going to be used in a bigger project that's been in development for months already, so I can't really bank on just creating a new project and transfering files; it'd be way too much work if I was required to do so for the big project as well. I need a proper solution, or at least proper understanding of the problem.

granite prairie
#

After trying to copy all files from my prototype instead of just the related scene, I realize it still doesn't work on a new project, so I think the problem might be something else.
Here are all the files I'm using, if anyone can find a hint to the problem I'd be grateful

umbral juniper
#

Input is a helper class that should only be used when there is no input event available in the current context, so if event is InputEventMouseButton is the only correct way to do it here

#

Otherwise detection will be buggy

#

You should also add a print statement above the if condition to check when any input event triggers

granite prairie
#

I've done that before, and like I said it only triggers when the mouse enters and exits, or if I scroll my mouse wheel
the event isn't triggered at all when I click, regardless of any if conditions

#

for example:

func _on_input_event(viewport: Node, event: InputEvent, shape_idx: int):
    print("test") <-- triggers on enter, exit and mouse scroll / but not triggered for left, right or middle mouse click.
    if event is InputEventMouseButton:
        print('clicked mouse') <-- triggers on mouse scroll
    if Input.is_action_just_pressed('left_click'):
        print('clicked!') <-- never triggered
umbral juniper
#

Test isn't printed while your mouse is changing its position inside the area?

granite prairie
#

nope

umbral juniper
#

Try to replace the polygon with a simple collision shape box or circle

#

See if that changes anything

granite prairie
#

it'd be a bit pointless cus I need it to be a polygon shape
and the polygon collision works on a blank project as well
but I can try just in case it gives a hint to the problem

#

nope, made no difference at all

umbral juniper
#

And this is an empty project? No other control nodes or areas that could catch that mouse click?

#

No UI at all?

granite prairie
granite prairie
# umbral juniper No UI at all?

I'm not using any conventional UI node, but I do have a scene called inventory that is filled with "slot" scenes, the slots having the area2d
that inventory scene is then placed in a level scene
I did try placing a slot scene directly in the level to see if that made a difference, but it did not

umbral juniper
#

Oh sorry I forget the beginning of the post.
While running, click the area and check the Debugger Misc tab in the editor

#

It should display what control node, if any, catches that click

granite prairie
#

hmm it does say the ColorRect is being clicked rather than the slots
but the ColorRect shouldn't be taking priority, so I'm confused
even if I switch their places around all that does is hide the slots visually and it still says I clicked ColorRect

#

can I make it so the color rect isn't interactable at all?

#

for now I don't need it to be

umbral juniper
#

There is a mouse_filter property in the inspector for control nodes

#

Set it to ignore

#

That should fix it

granite prairie
#

yup indeed
thanks a lot!

#

though I am a little uneasy about this fix because I feel like I might need to interact with the background at some point, but ig it's not an issue for now lol

umbral juniper
#

And remember only to use Input if there is no event: InputEvent as parameter in the current function. It will lead to strange bugs

granite prairie
#

good to know
I only used that because one of the replies on the reddit post suggested it

umbral juniper
#

It's a very common mistake

umbral juniper