Is It Possible To Select An Event Closest To Another Event?

Although I am using MV, I would think this is the same for all makers.

Example:
Let's say I set up a stationary event and name it Lightning Rod. Next I have several events slowly approaching this Lightning Rod. From there I set up a timer that plays an animation over whichever event is closest to the Lightning Rod, which would reset it back to it's starting position.

I already know how to do the reset, but I am unsure how to target the closest event each time. Suggestions?
there is only one way (and it doesn't matter which maker or which programming language, it's always the same principle and structure):

You need to go through the list of all events, compare the positions of the event to the key event position by mathematics, and on each comparison store the ID of the event that has the shortest distance.
After you're through with all events, the stored ID will be the ID of the event closes to the key position.

This loop can be done by pure eventing (just a lot of control variable commands with conditional branches and loops), but it's usually easier by scripting. But even in script you need to follow the same principle.

Moved to: MV Support
Tech-support is for getting the editors to run
You can use this script call to put the Game_Event object closest to the lightning rod in a Javascript variable called closestEvent:

Code:
var lightningRod = $gameMap.events().filter(function(event) { return event.event().name === "Lightning Rod"; })[0];
if (lightningRod) {
  var others = $gameMap.events().filter(function(event) { return event.event().name !== "Lightning Rod"; });
  var closestEvent = null;
  var closestDist = Number.MAX_SAFE_INTEGER;
  var x1 = lightningRod.x;
  var y1 = lightningRod.y;
  others.forEach(function(event) {
    var x2 = event.x;
    var y2 = event.y;
    var c = Math.hypot(x2 - x1, y2 - y1);
    if (c < closestDist) {
      closestDist = c;
      closestEvent = event;
    }
  });
}
@Trihan
your distance formula is not correct.
Just check it with an object where deltax=3 and deltay=3, and compare it with another one that has deltax=6 and deltay=0.

What you're calculating and compare is the number of steps to get to the position, but with a lightning rod (for striking something) the developer usually wants to target the true distance, and that means using Phytagoras as a formula for getting the distance.
@Andar is that better?
I didn't know hypot. That must be the same as
PHP:
distance = Math.sqrt(Math.pow(Math.abs(x1-x2),2) + Math.pow(Math.abs(y1-y2),2));
I learned something today.

Math.hypot is more elegant.
Yeah, it was added in ES5.
Thanks for the replies. How would I then go about calling that variable back up and targeting it?
You'd either have to do so in the script call itself, or you could do this:

Code:
var lightningRod = $gameMap.events().filter(function(event) { return event.event().name === "Lightning Rod"; })[0];
if (lightningRod) {
  var others = $gameMap.events().filter(function(event) { return event.event().name !== "Lightning Rod"; });
  var closestEvent = null;
  var closestDist = Number.MAX_SAFE_INTEGER;
  var x1 = lightningRod.x;
  var y1 = lightningRod.y;
  others.forEach(function(event) {
    var x2 = event.x;
    var y2 = event.y;
    var c = Math.hypot(x2 - x1, y2 - y1);
    if (c < closestDist) {
      closestDist = c;
      closestEvent = event;
    }
  });
}
$gameVariables.setValue(ID OF FREE VARIABLE, closestEvent.eventId());

That'll populate the variable with the chosen ID with the ID of the closest event, so you can then use it in later code or event commands.

Original thread: https://forums.rpgmakerweb.com/threads/is-it-possible-to-select-an-event-closest-to-another-event.76527/