Adventure Game Studio

AGS Support => Advanced Technical Forum => Topic started by: FanOfHumor on Sat 30/07/2022 15:52:25

Title: (solved)get room coordinate of where screen coordinate is over.
Post by: FanOfHumor on Sat 30/07/2022 15:52:25
I have a script that places tiles on the room at the mouse pos when you click but it gets off set because the screen coordinates are different than room coordinates.How would you get the xy position of the room underneath the cursor?
Code (ags) Select

function game_start()
{
tileguide=Overlay.CreateGraphical(player.x, player.y,3);
}
function repeatedly_execute_always()
{



if(beforetilenum<100000)
{
beforetilenum+=1;
}
if(beforetilenum==100000)
{
beforetilenum=0;
}
if(tile[beforetilenum]==null)
{
tilenum=beforetilenum;
}





if(tileguide.Y<mouse.y&ileguide.X<mouse.x)
  {
    //bottom right

    tileguide.X+=60;
    tileguide.Y+=30;
  } 
  if(tileguide.Y>mouse.y&ileguide.X>mouse.x)
  {
    //topleft
    tileguide.X-=60;
    tileguide.Y-=30;
  } 
 
  if(tileguide.Y<mouse.y&ileguide.X>mouse.x)
  {
    //bottomleft
    tileguide.X-=60;
    tileguide.Y+=30;

  }   

  if(tileguide.Y>mouse.y&ileguide.X<mouse.x)
  {
    //top right
    tileguide.X+=60;
    tileguide.Y-=30;

  } 
int dfg=3;
if(mouse.IsButtonDown(eMouseLeft))
{

tile[tilenum]=Overlay.CreateRoomGraphical(tileguide.X, tileguide.Y, dfg);
File* tilemap;
tilemap=File.Open("tilemap.txt", eFileAppend);
String s;
s=String.Format("tile[tilenum]=Overlay.CreateRoomGraphical(%d,%d,%d);",tileguide.X, tileguide.Y, dfg);
tilemap.WriteRawLine(s);
tilemap.Close();
}






if(IsKeyPressed(eKeyRightArrow))
  {
    Game.Camera.X+=13;
  } 
  if(IsKeyPressed(eKeyLeftArrow))
  {
    Game.Camera.X-=13;
  } 
  if(IsKeyPressed(eKeyUpArrow))
  {
    Game.Camera.Y-=13;
  } 
  if(IsKeyPressed(eKeyDownArrow))
  {
    Game.Camera.Y+=13;
  } 
}

The tileguide should be in the same place as the cursor but it should be able to send xy room position of tileguide to tile[tilenum].
Any thoughts on how to do this?
Title: Re: get room coordinate of where screen coordinate is over.
Post by: Matti on Sat 30/07/2022 16:39:39
In a scrolling room you need to add GetViewportX() / GetViewportY() to mouse.x and mouse.y to get the right room coordinates.
Title: Re: get room coordinate of where screen coordinate is over.
Post by: FanOfHumor on Sat 30/07/2022 17:24:01
Thankyou very much! :smiley:
Actually I just realized that theres Screen.screentoroompoint.
I don't know how I never saw that before.
Title: Re: get room coordinate of where screen coordinate is over.
Post by: Crimson Wizard on Sat 30/07/2022 17:43:41
Quote from: Matti on Sat 30/07/2022 16:39:39
In a scrolling room you need to add GetViewportX() / GetViewportY() to mouse.x and mouse.y to get the right room coordinates.

To clarify, the new way to do this since AGS 3.5.0 is: Game.Camera.X and Y.
Also, if you are using a more complex room viewport/camera setup (with viewport offset and camera zooming), then there's Screen.ScreenToRoomPoint and Screen.RoomToScreenPoint functions.
Title: Re: (solved)get room coordinate of where screen coordinate is over.
Post by: Matti on Sun 31/07/2022 18:59:40
Quote from: Crimson Wizard on Sat 30/07/2022 17:43:41
To clarify, the new way to do this since AGS 3.5.0 is: Game.Camera.X and Y.

Yes, of course, sorry. I still haven't used Game.Camera, so I forgot about it.

For some reason, I also didn't know about those Screen functions.