Script to compare coordinates between 2 events in MV

◆Script:if ($gameVariables.value(5) && $gameVariables.value(6) === $gameVariables.value(8) && $gameVariables.value(9))
{
$gameSwitches.setValue(3,true);
}

I am trying to get the XY coordinates of one event to compare to another, and if they are true have my 3rd switch turn on.
So far when the events match coordinates in game nothing happens. They just continue on the current path.
I have some coding experience but the engined is still new to me.

Edit: The goal is to coral an animal event into a stable. When it collides with the other event it is to walk into the stable.
I have it all working from common events. I'll post pics when my capture app is working.
What is the other event it's colliding with? Generally events won't go onto the same tile unless one of them is set to Through.

But if you just want to detect when it's on a particular tile on the map, or in a particular region, there's a better way.

Use a Region to paint the "trigger" tile, then use the following event commands (not scripts) to see when the animal moves into that position:

Code:
Control Variables [5] = Game Data > Character (animal)'s > Map X
Control Variables [6] = Game Data > Character (animal)'s > Map Y
Get Location Info (this is a command on tab 3) - set Variable [7] to the Region using variables 5 and 6 as coordinates
Conditional Branch: Variable [7] = <whatever region id you used for the stable>
  Control Switches [3] = ON
End

The script way to do it is (assuming you used region 25 and your animal is event 7):
Code:
$gameSwitches.setValue(3, $gameMap.event(7).regionId() === 25)
I think instead of the current setup which seems to be (x and y) === (x and y) you should try (x === x) and (y === y)

Edit:

after rechecking it seems like your current setup was actually

if ((x) and (y===x) and (y))

Which as @Zevia mentioned below will be mostly returning (true and false and true) which in turn will return false.
if ($gameVariables.value(5) && $gameVariables.value(6) === $gameVariables.value(8) && $gameVariables.value(9))

The problem here is that it's going to be evaluated like this:
1. gameVariables.value(5)
2. gameVariables.value(6) === gameVariables.value(8)
3. gameVariables.value(9)

Odds are pretty good that 1 and 3 will probably always evaluate to true once you've set them to something other than their default, as long as they don't equal 0.

2 will most likely evaluate to false the majority of the time, except in cases where the Y coordinate for the first event matches the X coordinate for the second event.

I think instead of the current setup which seems to be (x and y) === (x and y) you should try (x === x) and (y === y)

This is the better way to go about it. JavaScript isn't capable of handling (x and y) === (x and y), you have to evaluate each step of equality on its own like Engr. Adiktuzmiko's example.
Practically, your logic operation should be

Code:
if (($gameVariables.value(5)===$gameVariables.value(8)) && ($gameVariables.value(6)===$gameVariables.value(9)))

You check first if the two X are equal and then check if the two Y are equal.

Or if you want it to do a single check on the coordinate, you can input the X,Y combination inside an array and compare the arrays instead

Code:
if ([$gameVariables.value(5),$gameVariables.value(6)] === [$gameVariables.value(8),$gameVariables.value(9)])
For an "event enters building" situation I'd also recommend regions like Shaz suggests. 😂

In case it's handy for the future, I'll mention that there's a pos function explicitly for checking positions; e.g. to check whether event 5 is at event 6's position:
Code:
$gameMap.event(5).pos($gameMap.event(6).x, $gameMap.event(6).y);
Also may be worth noting that you can compare character coordinates using only one game variable and some event commands:
Spoiler
Code:
◆Control Variables:#0001 coord = Map X of Player
◆Control Variables:#0001 coord -= Map X of This Event
◆If:coord = 0
  ◆Comment:X coordinates are equal, now check Y
  ◆Control Variables:#0001 coord = Map Y of Player
  ◆Control Variables:#0001 coord -= Map Y of This Event
  ◆If:coord = 0
    ◆Comment:Y coordinates are also equal!
    ◆Text:None, Window, Bottom
    :    :You dig and discover some buried treasure!
    ◆Control Switches:#0001 Buried Treasure = ON
    ◆Change Gold:+ 5178
    ◆Exit Event Processing
    ◆
  :End
  ◆
:End
◆Text:None, Window, Bottom
:    :You dig and discover...nothing.

Or if you want it to do a single check on the coordinate, you can input the X,Y combination inside an array and compare the arrays instead

Code:
if ([$gameVariables.value(5),$gameVariables.value(6)] === [$gameVariables.value(8),$gameVariables.value(9)])
I think this will return true only if both sides reference the same object...which they don't? 😅
Could maybe use something like this if you want the convenience of arrays:
Code:
[[5, 8], [6, 9]].every(function(v) { return $gameVariables.value(v[0]) === $gameVariables.value(v[1]); });
I think this will return true only if both sides reference the same object...which they don't? 😅

Confirmed.
Code:
console.log([1, 2] === [1, 2]); // false

The .every call is creative - but I think comparing primitive values when possible is probably for the best. 🙂
I see, I havent tested the array check myself lolz. I actually had been thinking that it will not work. Thankz for confirming that it doesnt work.

Original thread: https://forums.rpgmakerweb.com/threads/script-to-compare-coordinates-between-2-events-in-mv.105861/