An event touches another event and triggers something

How can I make something trigger when an event touches another event? I would be happy if someone has a script for this.

I only have one script that allows one event to react to another when they overlap:

$gameMap.event(1).x == $gameMap.event(2).x && $gameMap.event(1).y == $gameMap.event(2).y;
There is probably a quicker way, but this should work:

JavaScript:
var x = $gameMap.event(1).x - $gameMap.event(2).x ;
var y = $gameMap.event(1).y - $gameMap.event(2).y;
$gameSwitches.setValue(123, false);
if (x ==  1 && y ==  0) {$gameSwitches.setValue(123, true)};
if (x == -1 && y ==  0) {$gameSwitches.setValue(123, true)};
if (x ==  0 && y ==  1) {$gameSwitches.setValue(123, true)};
if (x ==  0 && y == -1) {$gameSwitches.setValue(123, true)};
This should turn switch 123 on when event 1 and 2 are touching, and off when they are not touching.

Hope that helps.

Edit: Oh, you probably want it all in one line to use in a conditional branch, right? Lets try it like this:
JavaScript:
var x = $gameMap.event(1).x - $gameMap.event(2).x; var y = $gameMap.event(1).y - $gameMap.event(2).y; x ==  1 && y ==  0 || x == -1 && y ==  0 || x ==  0 && y ==  1 || x ==  0 && y == -1;
How can I make something trigger when an event touches another event? I would be happy if someone has a script for this.

I only have one script that allows one event to react to another when they overlap:

$gameMap.event(1).x == $gameMap.event(2).x && $gameMap.event(1).y == $gameMap.event(2).y;
Your script is already fine, so what's the issue?
Your script is already fine, so what's the issue?
That script shows when both events are on the same tile. They want the events right next to each other.
Thanks in advance!Thanks in advance!

Now the script is such that one event has to be next to another. Is it also possible that one event has to touch the other? That would be easier for me. If it doesn't work, it's also okey, because your script works too.
Thanks in advance!Thanks in advance!

Now the script is such that one event has to be next to another. Is it also possible that one event has to touch the other? That would be easier for me. If it doesn't work, it's also okey, because your script works too.
You could have the system check with the .canPass() function. You are welcome to test this in a parallel event - I am not at the computer to test unfortunately.

At this point, you probably don't need the .canPass() since it will already look like it is colliding (also assuming that you don't want them to go through each other either).

You can also check for distance (cartesian/hypotenuse) using Math.hypot() to deal with the events' distance.

It will be all on one line since the script box for the conditional branch only allows for that:
JavaScript:
const m = $gameMap, e = m.events(), d = Math.hypot(m.deltaX(e[1].x, e[2].x), m.deltaY(e[1].y, e[2].y)), p = e[1].canPass(e[1].x, e[1].y, e[1].direction()); d <= 1 && p

Edit: I would eventually just write this into a function and call it to make it easier after testing. So something like this:
JavaScript:
function checkDistPass(e1, e2, dist) {
    const m = $gameMap,
          d = Math.hypot(m.deltaX(e1.x, e2.x), m.deltaY(e1.y, e2.y)),
          p = e1.canPass(e1.x, e1.y, e1.direction());
    return (d <= dist || 1) && p;
}

And then in your if conditional branch, just do:
JavaScript:
checkDistPass($gameMap.event(1), $gameMap.event(2), 1)
There is probably a quicker way, but this should work:

JavaScript:
var x = $gameMap.event(1).x - $gameMap.event(2).x ;
var y = $gameMap.event(1).y - $gameMap.event(2).y;
$gameSwitches.setValue(123, false);
if (x == 1 && y == 0) {$gameSwitches.setValue(123, true)};
if (x == -1 && y == 0) {$gameSwitches.setValue(123, true)};
if (x == 0 && y == 1) {$gameSwitches.setValue(123, true)};
if (x == 0 && y == -1) {$gameSwitches.setValue(123, true)};
this could be cleaner as well, specially if you got multiple or want it simplified.
JavaScript:
let x = $gameMap.event(1).x - $gameMap.event(2).x ;
let y = $gameMap.event(1).y - $gameMap.event(2).y;
if (x ==  1 && y ==  0 || x == -1 && y ==  0 || x ==  0 && y ==  1 || x ==  0 && y == -1 ) {
$gameSwitches.setValue(123, true) } else { $gameSwitches.setValue(123, false)};

to use it cleaner and multiple places (if switch included)

JavaScript:
Function EventCheck(ev1, ev2, swId)
let x = $gameMap.event(ev1).x - $gameMap.event(ev2).x ;
let y = $gameMap.event(ev1).y - $gameMap.event(ev2).y;
if (x ==  1 && y ==  0 || x == -1 && y ==  0 || x ==  0 && y ==  1 || x ==  0 && y == -1 ) {
$gameSwitches.setValue(swId, true) } else { $gameSwitches.setValue(swId, false)};

// without switch

Function EventCollide(ev1, ev2)
return $gameMap.event(ev1).x == $gameMap.event(ev2).x && $gameMap.event(ev1).y == $gameMap.event(ev2).y

but there are more methodes, but the bottom 1 as scriptcall does the trick too if you use it loads ^^
The problem with all of this is that, presuming you're using autonomous move routes, an event will never try to move into a space with another event. It will see that direction as impassible and never go there.

So the code above from ShadowDragon, checking for them to have the same coordinates, can only happen if your event has Through on and you have manually directed it via move route.
Thank you all. I use the script from JohnDoeNews and have shortened it a bit. I also managed with little tricks that everything turned out the way I want it to be.

By the way, this is the script I'm using:

var x = Math.abs($gameMap.event(1).x - $gameMap.event(2).x), y = Math.abs($gameMap.event(1).y - $gameMap.event(2).y); (x == 1 && y == 0) || (x == 0 && y == 1);
Thank you all. I use the script from JohnDoeNews and have shortened it a bit. I also managed with little tricks that everything turned out the way I want it to be.

By the way, this is the script I'm using:

var x = Math.abs($gameMap.event(1).x - $gameMap.event(2).x), y = Math.abs($gameMap.event(1).y - $gameMap.event(2).y); (x == 1 && y == 0) || (x == 0 && y == 1);
Okay. I was going to say you need them again with -1, but then I saw a Math extension I don't know yet. I googled it to see if this would always show the difference as positive, and it does. 🙂 This is very useful to know.

So I guess we learned from each other at the same time.

I like how my original code gets shorter and shorter, btw. First there is an idea, than there is the logic, than there is the finishing touch. 🙂

However, I see a tiny mistake in the code. "), y = Math" must be "); var y = Math"

Edit, okay, I learned 2 things today. 😛 After I wrote the reply, I thought: "Or would that work too?", and it did! Seriously, I feel like such a noob today. 😛

Original thread: https://forums.rpgmakerweb.com/threads/an-event-touches-another-event-and-triggers-something.171345/