Find Closest Tile to Mouse Location

Hi! I am wanting to make an event or common event happen at the tile where the player clicks. Currently, I am using the Variable Commands with script call TouchInput.x and TouchInput.y and setting them to variables. On mouse click, it gives me the correct variable of where the mouse is on screen.

The problem is that it gives me a pixel location, not a map tile location. How would I convert this information to make an event or common event happen at the mouse location? Thank you!
You can do Math.round($gameVariables.value(a))

Or Math.ceil or Math.floor if you want round up or down
You can do Math.round($gameVariables.value(a))

Or Math.ceil or Math.floor if you want round up or down
im not sure if thats what i need. the middle of the screen returns a X and Y of 300 and 400. How would I use this code to convert to tile and then make an event happen?

i am not well versed in javascript. Could you please use an example?

Currently I have:
What happens if you divide it by 48 (or your tile size), then round it?

set a new game variable to Math.round($gameVariables.value(10)/48)
Personally for relocating an event to the center of my 1920x1080 screen as an example, I used this.locate($gameMap.canvasToMapX(960), $gameMap.canvasToMapY(540))

$gameMap.canvasToMapX(variable) and $gameMap.canvasToMapY(variable) might be exactly what you need. It auto converts pixels to actual tiles.
Hi! I am wanting to make an event or common event happen at the tile where the player clicks. Currently, I am using the Variable Commands with script call TouchInput.x and TouchInput.y and setting them to variables. On mouse click, it gives me the correct variable of where the mouse is on screen.

The problem is that it gives me a pixel location, not a map tile location. How would I convert this information to make an event or common event happen at the mouse location? Thank you!
Try:

Math.floor(TouchInput.x/48) ;
Math.floor(TouchInput.y/48) ;

The 1st is the tile on the X axis
↔
The 2nd is the tile on the Y axis ↕


Oh... btw... That ONLY works if there are exactly enough tiles to fit the screen AND the screen is exactly the right format to hold tile tiles and not a pixel more or less.

If you have more, or less pixels or tiles, then the upper corner of your screen will not be exactly line up with the upper corner of tile 0,0 which makes fail.
Like SundialShark said:
I think these are the lines of code you're seeking~
Code:
$gameMap.canvasToMapX(TouchInput.x);
$gameMap.canvasToMapY(TouchInput.y);

Original thread: https://forums.rpgmakerweb.com/threads/find-closest-tile-to-mouse-location.167889/