PlayerGui GetCurrentGuiObject(), CurrentGuiObjectChanged

Whenever my mouse was over an interactable NPC/container, I wanted to change the mouse cursor to an interact icon. The caveat being I wanted the cursor to revert to the default whenever it was on top of a GUI because I don’t want this happening:

Let’s observe what my options are for determining if my mouse is over a GUI, and what that GUI is:

  • Recurse through all GUI objects in the PlayerGui and manually check if the mouse is inside with position checks. This would not work well being run every time the mouse was moved or on heartbeat.
  • Hook up every GUI ever made a descendant of the PlayerGui to MouseEnter/MouseLeave and use that to track the GUI object the cursor is over. That’s a lot of events, and pretty messy.
  • Use something like quad trees to split the GUIs into manageable chunks that I can loop through on mouse move or heartbeat, which is arguably over-complicated for the purposes of something that you’d expect to be simple

While I was looking for something in the ROBLOX API to help me, I couldn’t really find anything. I did notice that whenever the mouse is over a GUI object with the active property enabled though, UIS processed = true. It looks like ROBLOX already keeps track of whether or not your mouse is over a GUI, and all that’s left is to expose that information. Right now I’ve made all GUIs’ lowest backgrounds active and added an additional check to my code to revert the cursor to default when processed = true, but being able to get the current GUI object the mouse was over, and having an event that fires when that object changes, would make accomplishing this a much, much cleaner process.

3 Likes

MouseEnter and MouseLeave is probably the ‘easiest’ way to do this, to be honest. However, from what I recall, they’re a little broken so with complex UI you’ll have a bunch of issues.

However, though, using quadtrees is a very nice solution and definitely worth the effort if you have a complex UI - you can more than likely use it for a lot of other things as well as mouse cursors.

Checking if UIS processed = true was easiest for me

Not really when the only thing I’d ever use it for is this. There is nothing else I’d ever need to iterate through all of the GUIs in the PlayerGui for.

I don’t think we should be solving this ourselves with a quadtree or whatever for such a simple task that PlayerGui/UserInputService could already keep track of. Doing it in internal code would be much quicker since you don’t waste as much time reading out properties of objects, and internally they already have some kind of sorted list of UI elements for rendering.

1 Like