Get x,y location of Terrain Tag

I spent a great deal of time trying to figure this out myself and searching the web for help, but I can't find anything that talks on what I want.

Simply put, is there a way to get the x,y coordinates for a specific terrain tag?

Just as example, I want to make a common event for the player to sit at a table, but the chair can be in any 1 of 4 spaces, see below. Each chair is marked with a terrain tag of 1.

Yhw4z7T
MPIVSf1.png


As it's meant to be a common event I can't just input coordinates, and I'm not sure of any other way of doing it aside from using an absolutely silly amount of variables, or creating a new event, coping the reliant code, & editing it to fit the situation perfectly every time, which kinda makes making it a common even useless. While I do think it could be done by using a silly amount of variables, each one being input by the event that will need to be situated on the table anyway, it would quickly become a mess of epic proportions even with proper commenting. Besides, if a simpler way using terrain tags is possible, why not use it.
It works the other way. You have the coordinates, and you use Get Location Info to return the terrain tag at that location.

So if your stool could be in one of 4 places, you will need to repeat the process up to 4 times until you get the correct terrain tag returned.
It works the other way.
So in short, No. I guess I'll be working on the variable route then.
It wouldn't take much to write a loop that would check every single tile on the map and stop when it finds one with the required terrain tag. Or if you could have multiple, it could build a list of coordinates that have that terrain tag and then you would have to figure out how to choose between them.
I hadn't thought of that. Think it could be simplified to "search" just the tiles adjacent to an event?
Yes. Use a common event to search around the specified event id, and use a map event to set a variable to the event id you're looking at.

So let's say event 8 is at the location of the table, we'll store the event id into variable 15, and we want the stool coordinates to be placed into variables 16 and 17. We're using terrain tag 2 for the stool location.

Prior to calling the common event, set variable 15 to 8 (the id of the event we want to search around).

In your common event, do the following script call:
Code:
var x1 = $gameMap.event($gameVariables.value(15)).x - 1;
var x2 = $gameMap.event($gameVariables.value(15)).x + 1;
var y1 = $gameMap.event($gameVariables.value(15)).y - 1;
var y2 = $gameMap.event($gameVariables.value(15)).y + 1;
for (var y = y1; y <= y2; y++) {
  for (var x = x1; x <= x2; x++) {
    if ($gameMap.terrainTag(x, y) === 2) {
      $gameVariables.setValue(16,x);
      $gameVariables.setValue(17,y);
    }
  }
}

After that common event runs, variables 16 and 17 will have the location of the last tile found with terrain tag 2.
This works on the assumption that there always will be at least one stool with terrain tag 2 around that event. If there isn't, you could end up with entirely different coordinates in those values, from the last time the common event was run successfully (which could be from a different map).
Thank you for the advice & assistance Shaz, I found it to be extremely helpful. Thanks to you I now have a fully functional, plug & play common event that finds the number of seats around a "table", notes the number of them & their locations, and then send the various party members to an assigned seat. (Still working on randomly chosen seats.) So once again, thank you.

-BloodMists

Original thread: https://forums.rpgmakerweb.com/threads/get-x-y-location-of-terrain-tag.94882/