Mouse enter/exit event doesn't trigger when player moves

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

Hello I’m creating a farming system for my game. I have a StaticBody2D with a CollisionShape2D acting as the crop, it detects if the mouse is hovering it . When I hover my mouse over the node it fires the mouse entered/exited signal without issue, but lets say I keep my mouse still and move my player, the camera will shift with the player but if the mouse happens to hover over any another crop node after the camera shift it doesn’t fire the mouse enter/exited signal unless I move the mouse. Is there a simple way to fix this behavior or should I try to intersect the position of the mouse whenever the player gets close to the crop to verify it is hovering it?

Just for clarification: the mouse enter/exit signals work as intended. But when the player node is in that position, the signals don’t function as intended? So you’re trying to find a way to have the spot consume the mouse input without the player node consuming it?

Ertain | 2022-07-15 23:32

Yes the signals work as intended

Forgive me for the crude edits, the red dot would be the mouse
picture 1

if I move the mouse on top a the potato object the enter/exit signal works as intended
picture 2
if I leave the mouse on the same position and only move the player to the right side even though the mouse is on top of another potato that potato object doesn’t fire its enter/exit signal, if I stayed on the same position while only moving the mouse between both nodes, both would fire their signals.

picture 3

WannabeDev | 2022-07-16 00:25

I still don’t quite understand your problem. Do you want to move the player character over some object and have the mouse detect the object? The thing is, the StaticBody2D will detect the mouse cursor entering and exiting the collision. If the mouse cursor isn’t moved (i.e. no motion), no mouse-related signals are emitted. When the player character moves, any body collisions that are set up by the StaticBody2D can detect the character.

Ertain | 2022-07-16 05:04

What I want is to detect when a motionless mouse happens to hover a object as a result of a player movement.
I have a area2D to check if the player is within the interaction range I might just use that with the physics process to intersect the mouse position while the player is within that range.

WannabeDev | 2022-07-16 11:58

:bust_in_silhouette: Reply From: zenbobilly

Are you saying that as a result of moving your character, the view scrolls but the mouse position hasn’t changed and is now hovering over a different “crop” ? If this is the case, I have conducted a few tests and haven’t encountered your error. What type of collision shapes are you using for the StaticBody2D ? I tried my tests on a Windows system with a rectangle “collider” and positioned several “crops” in sequence and caused a scroll while keeping the mouse motionless and everything worked as expected.

Yes, this is the exact scenario. Moving my player scrolls the camera while keeping my mouse in the same position, the crop node doesn’t recognize the mouse being on top of it. I’m also using rectangle shapes for my collider.

enter image description here
The Hurtbox, PlayerDectectionZone and the StaticBody2D are all in different layers as well. The BuildingAreaBounds is only present while the player is trying to place the crop, afterwards it is removed.
All the signals are connected to the “Plant” Node2D

I also tested it with a StaticBody2D with a rectangle collision and pickable enable, I had the same results
enter image description here

WannabeDev | 2022-07-16 15:02

In my tests, I had the mouse “signals” tied to the StaticBody2D and it worked fine and not a Node2D. Are you sure the layer values you are using aren’t conflicting with something else on that layer(s) ?

zenbobilly | 2022-07-17 04:37

That’s my guess as well, it’s probably another area interfering with the mouse tracking. I did a workaround earlier. Made it so when the player enters the interaction area, I check if the mouse’s position in the TileMap matches the tile the crop is on and if yes I make my variable which tracks the mouse become “True”

WannabeDev | 2022-07-17 04:48

When I did my tests, I made sure that the StaticBody2D was on a completely separate layer to everything else and I ensured that the colliders for each weren’t overlapping each other or anything. The other possibility is that the signals attached to your “HurtBox” and “PlayerDetectionZone” are absorbing the mouse signals. An easy way to test this is to simply move your StaticBody2D node further up the tree (you can use CTRL+UP when the node is selected to do this) so that it receives signals before anything else in that tree.

zenbobilly | 2022-07-17 05:39

I turned off monitorable and monitoring for all the Area2D’s my player has along with disabling their collision shapes. I just attempted to do what you said and I still get the same result.

For good measure I made a entire new project, the player is on layer 1 and the object I want to detect my mouse is on layer 2. I added a simple movement script for the player and attached a camera so the view can scroll once the player moves. For the object I turned on pickable and gave the object a rectangle collision shape along with the godot icon as a sprite. (Red would be the object)
enter image description here
I still got the same results, I tried swapping the order of the Sprite and the CollisionShape2D just in case. Could this be some sort of hardware issue? My current machine is a laptop. I’m using a mouse but I couldn’t be bothered to turn off the touchpad, if it matters the screen of the laptop happens to have touchscreen too.

WannabeDev | 2022-07-17 13:04

It doesn’t matter about the order in the scene for your test project because only one thing receives signals (which should be the StaticBody2D). Mine is essentially the same setup with the script on the StaticBody2D as follows:

extends StaticBody2D

func _ready():
pass # Replace with function body.

func _on_StaticBody2D_mouse_entered():
OS.set_window_title ("Mouse Entered: " + name)

func _on_StaticBody2D_mouse_exited():
OS.set_window_title ("Mouse Exited: " + name)

Ensure that the StaticBody2D is the one receiving the mouse signal information and not some other node. Scroll around with the mouse positioned as required and you should see the window title text change as the mouse enters/exits the collision area as a result of scrolling.

zenbobilly | 2022-07-17 13:47

Sadly even following your instructions it still doesn’t work, I believe it might be some sort of hardware issue. I can verify that once I get back from holiday and run the project on my PC.

WannabeDev | 2022-07-17 15:18