[Solved] How do I get Mouse X and Y coordinates relative to the game window instead of gdevelop grid

Short version:
I need to change my cursor icon to a pointer when I hover over a button. Something like {myPointer.X() = Mouse.X()} works just fine until my games moves drastically to the right (eg 2000px). Now I have to use something like {myPoiner.X() = Mouse.X() - offsetOfHowMuchGameHasMove.X()}. But that’s very inelegant and not very accurate. Is there a way to get the window mouse.X() even when its outside of the initial game width and height?

Long version:
I have a game that’s pretty much a board game. You get questions right, roll dice, move spaces to the right, first one to the end wins. Because I wanted the camera to smoothly follow the characters, I decided it was best to create one super long scene as the game board. (out to 15,000+px). The problem with this method is that is seems the mouse.X() and mouse.Y() numbers return based on the x & y of the game grid. So mouse.X() see the position of 5,000 and moves myPointer.X() to somewhere way off the screen to the right because myPointer is be placed off the game window.

So I’m asking how do I get the mouse.X() and mouse.Y() coordinate according to the window so that {myPointer.X() = mouse.X()} will work?

in javascript its “runtimeScene.getGame()._inputManager._mouseX”. How do I get this value without having to use javascript?

You’re experiencing this behavior as you’re missing parameters on your expressions. MouseX() and MouseY() should never really be used without expressions.

If you use the expression builder, it’ll show you what should be populated, but basically you want to point it at whatever layer the button is on.

Camera will always be 0 (there is only 1 camera per layer, the field is a legacy thing from GD4). The Layer field should be whatever layer your items are on.

So as an example, if your buttons are on a layer named “GUI”, you’d do MouseX(“GUI”,0)/MouseY(“GUI”,0).

1 Like

I knew it had something to do with the layers. And i was using mouse.X() instead of MouseX(). Thanks! This worked.

1 Like