MZ Script call that includes all game map events

Hello,
I'm trying to create a situation where any enemy (an event with move route) that touches a city (a static event) triggers an event.

I've achieved this by creating a conditional branch in the city event with the script:

Code:
$gameMap.event(9).x == $gameMap.event(5).x && $gameMap.event(9).y == $gameMap.event(5).y;

(City is event #9 and enemy event #5)

In order for my system to work with all enemies, I'd need to create a new branch for each enemy event Id.

For the sake of efficiency and sanity, is there a way of making this script look for any event that touches $gameMap.event(9)?

In other words, can I replace $gameMap.event(5) with something that would include all events and not only event 5? That would save me a whole lot of work!
Thanks
On MV, $gameMap.events() returns an array that contains every single event in the current map.
Maybe that could work on MZ. From there, you could check if an event is touching the event you want.
Example:
JavaScript:
IDevents = [];
for(i = 0; i < $gameMap.events().length; i++) {
    sameX = $gameMap.event(9).x == $gameMap.events()[i].x;
    sameY = $gameMap.event(9).y == $gameMap.events()[i].y;
    notTheSame = $gameMap.event(9) !== $gameMap.events()[i];
    if(sameX && sameY && notTheSame) {
        IDevents.push($gameMap.events()[i].eventId);
    }
}

This makes it so that if an event in the map that's not event number 9 has the same X and Y coordinates as event number 9, its event ID is added to the IDevents array.
I'm not on my computer right now, so forgive me if I'm wrong.
JavaScript:
$gameMap.events().forEach(function(ev) {
   if(ev.eventId() != 9){
      if($gameMap.event(9).x == ev.x && $gameMap.event(9).y == ev.y){
         console.log("Event "+ev.eventId()+" collided!");
      }
   }
});

I'm not a Scripter / Programmer, I usually use Events to create most of my systems like that, I don't think it's too much work.

But if you are going to create multiple events on the map, it can be very tiring.

Good luck
On MV, $gameMap.events() returns an array that contains every single event in the current map.
Maybe that could work on MZ. From there, you could check if an event is touching the event you want.
Example:
JavaScript:
IDevents = [];
for(i = 0; i < $gameMap.events().length; i++) {
    sameX = $gameMap.event(9).x == $gameMap.events()[i].x;
    sameY = $gameMap.event(9).y == $gameMap.events()[i].y;
    notTheSame = $gameMap.event(9) !== $gameMap.events()[i];
    if(sameX && sameY && notTheSame) {
        IDevents.push($gameMap.events()[i].eventId);
    }
}

This makes it so that if an event in the map that's not event number 9 has the same X and Y coordinates as event number 9, its event ID is added to the IDevents array.
That solution sounds promising.
However, since I'm very new to scripting, I'm not quite sure how to implement this. Where exactly should I place this script and what exactly should I add to the conditional branch?

Thanks!
That solution sounds promising.
However, since I'm very new to scripting, I'm not quite sure how to implement this. Where exactly should I place this script and what exactly should I add to the conditional branch?

Thanks!
What's next is what you want to do with the events that were identified as in the same location as event 9.
The "IF" inside the "For" is the "Conditional Branch"
The "IF" inside the "For" is the "Conditional Branch"
Sort of, however it doesn't let OP switch back to eventing, which is a better option when you're not really proficient in JS.
I was going to suggest that he study this:
RMMV Script Calls
What's next is what you want to do with the events that were identified as in the same location as event 9.

OK, I tried to make this event work but I'm not quite sure how to insert the array. This is what I tried doing (the city is now event iD4:
2021-04-01 15_22_22-ID_004 - Event Editor.png


When I launch the game I now get:

TypeError
Cannot read property 'x' of undefined

Sorry if I sound like a total newb...

I was going to suggest that he study this:
RMMV Script Calls

I actually have this file and went through it a few times. Its a great reference document but it doesn't answer all of my questions right now...
But it would be just simple commands, like using a Switch to modify an event or more, it doesn't have to be all within the Script.

"X" is undefined because the algorithm is trying to read an event that does not exist.
You may have created events 1, 2, 3, 4, 5, and 6.
But if you delete 3 for example, it will not decrease the number, it will create a gap:
1,2,4,5,6
Did you try my example?
But it would be just simple commands, like using a Switch to modify an event or more, it doesn't have to be all within the Script.

"X" is undefined because the algorithm is trying to read an event that does not exist.
You may have created events 1, 2, 3, 4, 5, and 6.
But if you delete 3 for example, it will not decrease the number, it will create a gap:
1,2,4,5,6
Did you try my example?
I tried your example but I'm not sure how to make it work. Do I need to insert a script call inside your code? If so, where exactly? If you could provide an example of how to make this work, I'd be very grateful. Thanks!
To simulate the system, I created 8 events with monster graphics that randomly rotate around the map, none is restricted to other events or map tiles.
In the center I put an event in a parallel process, I put the Script in it, this being 0009.
I tested it and it worked as you can see on the game console (F12).

I'm sorry if something looks confusing, if it doesn't succeed I'll send you a copy of the test map.

Collision-Events_rez.png
Your code works for me too (I see collided! in the console). But I don't know how to trigger an event (show text, turn switch on, etc) when it happens. If you could send me a test map with an even being triggered, that would be great.
Code:
ev9 = $gameMap.event(9);
$gameMap.eventsXY(ev9.x, ev9.y).length > 1;

2 instructions, no loop.
"take event 9, find anyone sharing the same coordinates"
resulting array will be at least 1 event large: event 9, the one you're measuring.
anyone else? then they're both sharing the coordinates.
But I don't know how to trigger an event (show text, turn switch on, etc)
The commands for this are in the "RMMV Calls Script":
I was going to suggest that he study this:
RMMV Script Calls
Show Text - Line 5
Control Switch - Line 25
Control Self Switch - Line 28

Good Luck
The commands for this are in the "RMMV Calls Script":

Show Text - Line 5
Control Switch - Line 25
Control Self Switch - Line 28

Good Luck
It seems to be working now! I knew which script calls to use but I had trouble finding where to place them inside your code (total newb with Javascript). After some trial and error, I believe I got it

$gameMap.events().forEach(function(ev) { if(ev.eventId() != 4){ if($gameMap.event(4).x == ev.x && $gameMap.event(4).y == ev.y){ console.log("Event "+ev.eventId()+" collided!"); $gameMessage.setFaceImage('Actor1',0); $gameMessage.setBackground(1); $gameMessage.setPositionType(1); $gameMessage.add("test"); } } });

Since I shouldn't be dabbling too much with Javascript, I guess I could use your code to trigger switches and go back to eventing afterward.

Anyways, thanks for your help!
You don't have to "like" my answer, which is the solution, but use BBCODE to post scripts.

Here's an example (Remove *):
Code:
[*CODE]$gameMessage.setFaceImage('Actor1', 0);
$gameMessage.setBackground(1);
$gameMessage.setPositionType(1);
$gameMessage.add("test");[*/CODE]

Result:
Code:
$gameMessage.setFaceImage('Actor1', 0);
$gameMessage.setBackground(1);
$gameMessage.setPositionType(1);
$gameMessage.add("test");

Original thread: https://forums.rpgmakerweb.com/threads/script-call-that-includes-all-game-map-events.135007/