MV Detecting distance from region id?

Hello,

Is there a script or way to check how many tiles or pixels the player is away from a region ID?

For example I'm trying to check if the player is within 3 tiles of region ID 10, (specifically on the X axis)
Theoretically, you could loop through every tile on the map, store the coordinates of any tile of region ID X into an array, then calculate the range of tiles that are within 3 tiles of the player, put them in a second array and see if there are any matches.
Interesting, that's sounds like it could work. Definately beyond my current skill level though lol, where would I start?
This should do the first part, I think? Push all coordinates of a region into an array. Change 4 to the region you want to check

JavaScript:
$gameSystem.regionXY = [];
for (var x = $gameMap.width(); x--;) for (var y = $gameMap.height(); y--;)
if ($gameMap.regionId(x, y) === 4){$gameSystem.regionXY.push([x, y]);}
Interesting, that's sounds like it could work.
As said above - theoretically yes, practically most likely not

the problem is that the loop over every map tile and then the distance check over every result will require more processing the larger the map gets.
And even then, processing that after every position change will cause lag extremely fast.

A better solution is to use two different region IDs to only pretend to check everything.
1) place the region ID you want as normal
2) select a second region ID that is unused
3) place the second region ID on every tile that is within the distance of the original ID
4) check if the player stands on the second ID. If yes you'll know he is in range because that is where you placed the second ID.
Aren't you asking two different things? First you ask 'how many tiles the player is away from a region ID', but then in your example 'if the player is within 3 tiles of region ID 10 (X axis)'.

Your example situation you can use event commands to check the 7 tiles around player, like this:

Spoiler
Control variables X = X-coordinate of player
Control variables Y = Y-coordinate of player
Control variables X -= 3

LOOP:
Get Location Info: RegID(X, Y)
If RegID = Z
->DO STUFF
->break loop
Control Variables X += 1
Control Variables LOOP += 1
If LOOP >= 7
->break loop (+ set the loop variable to 0)
As said above - theoretically yes, practically most likely not

the problem is that the loop over every map tile and then the distance check over every result will require more processing the larger the map gets.
And even then, processing that after every position change will cause lag extremely fast.
You'd only need to loop through the map once to get the coordinates or every tile of id region x. Or if the map is unchanging, you don't even need to run it during the game, just do it during playtest and store the results in an array.
The grid of tiles around the player would be dynamic though, and the more often you check the more processing power it would take.

A better solution is to use two different region IDs to only pretend to check everything.
1) place the region ID you want as normal
2) select a second region ID that is unused
3) place the second region ID on every tile that is within the distance of the original ID
4) check if the player stands on the second ID. If yes you'll know he is in range because that is where you placed the second ID.
Would work if the OP is always checking for "within 3 tiles" and that distance isn't a variable.
You'd only need to loop through the map once to get the coordinates or every tile of id region x.
yes, but checking the player position against every element of the array would have to be done every player move.
and checking every array element in two coordinates against the player position requires more processing as the map loop as well.
@twosnakes It would probably help if you clarified your requirements a bit:
- Do you actually need the straight-line distance, or only the distance along a single axis?
- Do you always need to check the same length (e.g. 3 tiles), or can that change during the game?
- Will the regions change during the game, or are they static?
yes, but checking the player position against every element of the array would have to be done every player move.
and checking every array element in two coordinates against the player position requires more processing as the map loop as well.
That shouldn't be a problem. I'm programming a fog of war right now, which is pretty close to OP's request.

I suggest making a plugin with this code. The plugin's file can have any name.

Code:
Game_Map.prototype.isRegionIdInRange = function(regionId, range) {
  for (let x = 0; x < $gameMap.width(); x++) {
    for (let y = 0; y < $gameMap.height(); y++) {
      if ($gameMap.regionId(x, y) == regionId && $gameMap.distance($player.x, $player.y, x, y) <= range) {
        return true;
      }
    }
  }
  return false;
}

This code will also work with looping maps (e.g., Worldmap).

You can now query whether your player is x tiles away from RegionId R by using this in an IF branch:
$gameMap.isRegionIdInRange(R, x)

As Andar mentioned, you should not forget about the performance. The most elegant way would be to use Variables and track whether the player took a step in some direction, but I think a simple wait command with ~10 frames should be good enough and much simpler to do.
Thank you everyone for the help so far!
@twosnakes It would probably help if you clarified your requirements a bit:
- Do you actually need the straight-line distance, or only the distance along a single axis?
- Do you always need to check the same length (e.g. 3 tiles), or can that change during the game?
- Will the regions change during the game, or are they static?
- just the distance along a single axis
- length can change
- regions are static
That shouldn't be a problem. I'm programming a fog of war right now, which is pretty close to OP's request.

I suggest making a plugin with this code. The plugin's file can have any name.

Code:
Game_Map.prototype.isRegionIdInRange = function(regionId, range) {
  for (let x = 0; x < $gameMap.width(); x++) {
    for (let y = 0; y < $gameMap.height(); y++) {
      if ($gameMap.regionId(x, y) == regionId && $gameMap.distance($player.x, $player.y, x, y) <= range) {
        return true;
      }
    }
  }
  return false;
}

This code will also work with looping maps (e.g., Worldmap).

You can now query whether your player is x tiles away from RegionId R by using this in an IF branch:
$gameMap.isRegionIdInRange(R, x)

As Andar mentioned, you should not forget about the performance. The most elegant way would be to use Variables and track whether the player took a step in some direction, but I think a simple wait command with ~10 frames should be good enough and much simpler to do.
This plugin works nicely so far 🙂. I just corrected $player to $gamePlayer to get it to work. So far there is no performance hit with a wait command and only checking when the player moves.

Is it possible to check if the player is x tiles away on only one axis at a time?
Is it possible to check if the player is x tiles away on only one axis at a time?
rpgLord gave a demonstration of doing that in post #6.
I just corrected $player to $gamePlayer to get it to work.
Whoopsie doopsie xD
Thanks for the help everyone, I probably should have explained my plan to begin with (why didn't I?!). I'm using community lightning and don't like how the flashlight goes through walls. Other lightning plugins with ray tracing haven't been able to provide a lag free experience (for MV), so I was trying to incase rooms with region ids and change the size of the flashlight as the player approached the walls. Shortening the light beam as you approach the wall so it doesn't project through.

This thread helped, but I think my idea and execution/approach is too sloppy/inelegant looking. For now I'll stick to slightly large hallways and map design to accommodate the flashlight beam, unless anyone has any other creative ideas lol?
yes, but checking the player position against every element of the array would have to be done every player move.
and checking every array element in two coordinates against the player position requires more processing as the map loop as well.
Won't be more efficient to run sections of the map per time? A error margin being acceptable of course
Won't be more efficient to run sections of the map per time? A error margin being acceptable of course
If you're going to do that you might as well just check a 3x3 edit: 7x7, 3 tiled in each direction (or a x a) box around the player for a region id as the OP originally requested.

Original thread: https://forums.rpgmakerweb.com/threads/detecting-distance-from-region-id.161134/