Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: Cmdr on Mon 11/11/2024 21:35:00

Title: Mouse Coordinates in a scrolling room - AGS 3.6.1
Post by: Cmdr on Mon 11/11/2024 21:35:00
I have the exact same problem as this guy:
https://www.adventuregamestudio.co.uk/forums/beginners-technical-questions/problem-with-mouse-coordinates-in-a-scrolling-room/

I need to get my mouse coordinates, but not of the current viewport, but the global coordinates of the scrolling room. The given solution is simple and can be found on many threads in this forum.

But the funny thing is...
It doesn't work with AGS 3.6.1 anymore  :grin:

So what is the current equivalent of GetViewportX()?
I tried Screen.Viewport.X, but this seems to be always 0, no matter how much the screen scrolled over the room.
Title: Re: Mouse Coordinates in a scrolling room - AGS 3.6.1
Post by: Crimson Wizard on Mon 11/11/2024 21:39:45
There's the list of obsolete functions and their replacements in the manual:
https://adventuregamestudio.github.io/ags-manual/ObsoleteScriptAPI.html

Even if you find GetViewportX, it will mention the replacement:
https://adventuregamestudio.github.io/ags-manual/Globalfunctions_Room.html#getviewportx
Title: Re: Mouse Coordinates in a scrolling room - AGS 3.6.1
Post by: Cmdr on Mon 11/11/2024 21:51:12
Thank you for your reply.
I found a solution with Viewport.ScreenToRoomPoint:
QuotePoint* pt = Screen.Viewport.ScreenToRoomPoint(mouse.x, mouse.y, true);
    if (pt != null) {
       DoThings(pt.x, pt.y);
    }
Title: Re: Mouse Coordinates in a scrolling room - AGS 3.6.1
Post by: eri0o on Mon 11/11/2024 21:55:22
https://adventuregamestudio.github.io/ags-manual/Screen.html#screenscreentoroompoint

Screen directly has a ScreenToRoomPoint which you should use. Note AGS supports multiple cameras and multiple viewports - for something like split screen and similar.

I think your approach only works if the viewport is only one and it covers the entire screen.
Title: Re: Mouse Coordinates in a scrolling room - AGS 3.6.1
Post by: Crimson Wizard on Mon 11/11/2024 22:00:13
Quote from: Cmdr on Mon 11/11/2024 21:51:12I found a solution with Viewport.ScreenToRoomPoint:

Oh right, that's a more generic way of doing this.

You can also keep the old way by adding Game.Camera.X (instead of GetViewportX).
ScreenToRoomPoint has a benefit that it also handles scaling (if you zoom camera in or out).

Please note that if you do not pass "true" to the last argument (restrictToViewport or clipViewport, depending on a function), then you dont have to test the return value for null, as if the coordinates are never "clipped by viewport", and returned Point is always valid.