UI element is not responding correctly to mouse_entered and mouse_exited, OP solution leads to unexpected results

Godot Version

4.3

Question

Hi! I have a really nice inventory that I created following a tutorial here. I wanted to connect the inventory to my game’s hotbar, so I created a new version of my hotbar (not instantiated just visually the same, because they need different scripts).

The hierarchy of the Hotbar is as follows:
Inventory Scene → Vboxcontainer → Grid Container → Inventory Slot(s) (packed scene). The inventory slot’s hierarchy is TextureRect → Area2D → ColisionShape2.

For some reason mouse entered and mouse exited signals are never called by the hotbar slot script. This probably has to do with an element of the inventory scene stopping the mouse, so I decided to try and detect the mouse position independent of mouse control using the following statement in the process function of a script attached to the root of the inventory slot.
if get_global_rect().has_point(get_global_mouse_position()): texture = hovered_slot else: texture = slot

For some reason that I don’t understand this has led to the slot being “hovered on” whenever the mouse is to a significant bit to the right of the slot. What is especially weird is that this is outside of the game window. I have checked the location of the collision shape and the area and they are perfectly aligned with the textureRect.

Here is a video of the issue:

Does anyone know how I could fix this? Any help would be much appreciated.

Could you post a MRE for us to test locally?

I’m so sorry that this reply took so long, I got busy with personal matters. I’ve never made one before, so if this is unsatisfactory I can try again. Godot doesn’t allow uploading zip files for whatever reason, so here’s a link to a drive with my MRE. Thanks! for-the-forums.zip - Google Drive

small update, I have confirmed that the area that it is tracking is a direct offset of the hotbar slots. moving the Hotbar moves the offset as well.

Somehow the engine is getting the correct location that I am looking for and changes the default cursor, but I can’t get my code to correspond to that area.

By switching the Hboxcontainer to a boxcontainer I was able to get the get_global_rect detection to not be off-centered; however, the area functions still do not work. This is bad, because I was hoping to detect if an item had entered the hotbar’s area on the screen.

1 Like

Hi! I will check out the project later today.

No need to apologize! You have no obligation to me, a random forum user :slight_smile: .

That is a textbook MRE. Good stuff.

Here’s some takeaways I had:

Inventory Hotbar Scene

  • The root VBoxContainer.
    • Is rotated 90 degrees.
      • It is documented that get_global_rect() does not account for rotation or skew in any ancestor node. That can explain why the get_global_rect().has_point(get_global_mouse_position()) is offset.
    • Is not being used as a VBoxContainer.
      • The VBoxContainer is meant to have multiple child control nodes that it stacks vertically.
      • In the MRE, you can get rid of the GridContainer node and add the inventory_hotbar_slots directly to the VBoxContainer.
        • Doing this will make it so the hotbar is vertical, too. No rotation needed.

Area2D Mouse Detection

The Area2D mouse entered and exited signals were working as expected for me. On mouse entered it would set the texture to hovered_slot and on mouse exit would set it to slot.

Just want to point out that Control has mouse_entered and mouse_exited signals.

Additionally there is the drag and drop api. If that suits your use case.


That’s all I got for now. Let me know if any of this helps!

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