Godot Version
4.4.1
Question
I have set up a scene to detect mouse hoowering on various type of objects. I use composition. So I put this scene under any node I want to be able to detect hoowering, set up the collision area size, and that’s it.
The scene is an Area2D with a CollisionShape2D. Code is as follows. “g.” refers to “global.gd”, a singleton. That singleton is responsible to get the signal and send it to whoever is interested
extends Area2D
class_name mouse_detector
@export var area_size: int = 16
signal mouse_in
func _ready() → void:
$CollisionShape2D.shape = RectangleShape2D.new()
$CollisionShape2D.shape.size = Vector2(area_size, area_size)
$CollisionShape2D.position = Vector2(area_size/2, -area_size/2)
mouse_in.connect(g._on_mouse_detector_mouse_entered)
mouse_entered.connect(_on_mouse_entered)
mouse_exited.connect(g._on_mouse_detector_mouse_exited)
func _on_mouse_entered():
mouse_in.emit(get_parent())
And here is what you find in global.gd:
signal mouse_in
signal mouse_out
func _on_mouse_detector_mouse_entered(o) → void:
mouse_in.emit(o)
func _on_mouse_detector_mouse_exited() → void:
mouse_out.emit()
Piece of cake, right?
Problem is, mouse is randomly not detected.
Sometime it is, in a position DIFFERENT from where it should.
Sometimes, triggers/detriggers if I zoom in or out the camera (which I can do using the mouse wheel btw).
Often, triggers in the right position if I click the mouse left button, but it should not. As you see above nothing is related to mouse click. Yes there is a control node somewhere on the tree that listens for clicks, its code is as follows:
extends Control
class_name in_game_control
@export var parent: Node
signal left_mouse_click
func _ready() → void:
pass # Replace with function body.
func _input(event):
# Mouse in viewport coordinates.
if event is InputEventMouseButton:
if event.button_index == MOUSE_BUTTON_LEFT and event.is_pressed():
accept_event()
left_mouse_click.emit()
I am enabling display of collision shapes in editor, they are where they should, b4 you ask.
OF COURSE all in any control node is fine for mouse input: everything is set to “pass”.
Odd?
I did not tell you the whole story tough.
I have two viewports. And I suspect that THERE is the problem, somehow.
In fact, on the right viewport everything is working fine as it should.
It’s only in the left one that things go crazy.
I suspect everything is related to that. Also the only viewport that has zooming is the left one, the right one has not, scale = 1 and no camera zoom ever.
But even if my suspect is correct… well now what?!
I’m depressed. There is ALWAYS an issue with signal for mouse position. I am really pissed off, Godot somehow always makes some specific thing more difficult than they should (control nodes, anyone?). I will end up putting a get_global_mouse_position in a process, then check for various rectangles if the mouse position is inside them, and that’s it. Maybe.
But maybe someone here understands my problem and has a solution? I would love so.