Help with handling mouse position

Godot Version

4.3.stable.mono

Question

get_global_mouse_position() causes _on_mouse_entered() and _on_mouse_exited() to misbehave

I need to check for mouse position in two places in two different ways (so far) and this causes an issue:

  1. A series of control nodes check for _on_mouse_entered() and _on_mouse_exited() (a series of Control nodes that perform a function on hover)
  2. Apart from that, in another (non-overlapping) part of the screen, I check if the mouse global position (get_global_mouse_position()) is within some bounds. (manually checking if the mouse is over an area, populated by a tilemap)

The two parts clash and I can’t seem to make them coexist properly: The 2 part works, but the 1 part breaks: when I hover over any of the Control nodes for part 1, _on_mouse_entered() is executed and then _on_mouse_exited() is executed immediately afterwards, without the mouse really exiting.
I tried getting the mouse position for 2 in _process() and in _unhandled_input(), with the same effect.

If I remove the get_global_mouse_position() functionality for part 2, the part 1 hover works perfectly.

I also tried setting the mouse filter property of the control nodes to ‘Pass’.

I presume the problem is that my mouse position is being “stolen” by the continuous checks of get_global_mouse_position(), but I have found no way to confirm or get around that. Ideally, I would like to be able to check the global mouse position regardless of any _on_mouse_entered / exited checks.

How is this done?


This is the main scene:

The supply node is a separate scene with the following structure:

Each ‘marker_’ in the Supply scene has the following structure:

This is the hover code for the markers (supply spots):
Screenshot 2024-12-26 175625

And this is the code in the script, attached to ‘Map’ in the main scene, that tries to do something with the mouse global position (currently the _process() attempts is commented out):

I don’t think the get_global_mouse_position is doing that, this only returns a vector and don’t touch in anything else. You need to provide more info, show the relevant code and show how your scene is organized.

You are right about the additional info, sorry. I’ve edited the quesntion to include structure and the relevant code.

What highlight_moves and remove_highlights do? You’re calling this in the signals and while moves the mouse so you probably getting the conflict because you’re calling the same thing from different points.

1 Like

Facepalm…

That’s it!

The code that relies on the mouse position cares about highlighting only 3 very particular spots, but if I don’t get the mouse where I want it, I indiscriminately remove all highlights! I should remove the highlights only on the nodes that the mouse movement would highlight. That’s what’s messing it up.

Thanks a lot! It takes a few hours of headbutting a wall and searching online, creating a new topic (and makingsure you’re not duplicating a question), only to get someone point out that the problem is srating you in the face :smiley:

P.S. For completion:

highlight_moved(name) sets the ‘highlight’ Sprite2D nodes of a number of supply_spots visible (the set depends on the passed name).

remove_highlights() makes all ‘highlight’ nodes invisible.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.