Script to compare coordinates between 2 events in MV

CreativeVulpine

Villager
Member
Joined
Jan 1, 2019
Messages
13
Reaction score
14
First Language
English
Primarily Uses
RMMV
◆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.
 
Last edited:

Shaz

Veteran
Veteran
Joined
Mar 2, 2012
Messages
39,672
Reaction score
13,282
First Language
English
Primarily Uses
RMMV
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)
 

Engr. Adiktuzmiko

Chemical Engineer, Game Developer, Using BlinkBoy'
Veteran
Joined
May 15, 2012
Messages
14,696
Reaction score
3,008
First Language
Tagalog
Primarily Uses
RMVXA
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.
 
Last edited:

Zevia

Veteran
Veteran
Joined
Aug 4, 2012
Messages
637
Reaction score
346
First Language
English
Primarily Uses
RMMV
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.
 

Engr. Adiktuzmiko

Chemical Engineer, Game Developer, Using BlinkBoy'
Veteran
Joined
May 15, 2012
Messages
14,696
Reaction score
3,008
First Language
Tagalog
Primarily Uses
RMVXA
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)])
 

caethyril

^_^
Veteran
Joined
Feb 21, 2018
Messages
1,714
Reaction score
1,165
First Language
EN
Primarily Uses
RMMV
For an "event enters building" situation I'd also recommend regions like Shaz suggests. :kaojoy:

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:
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? :kaoswt2:
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]); });
 

Zevia

Veteran
Veteran
Joined
Aug 4, 2012
Messages
637
Reaction score
346
First Language
English
Primarily Uses
RMMV
I think this will return true only if both sides reference the same object...which they don't? :kaoswt2:
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. :smile:
 

Engr. Adiktuzmiko

Chemical Engineer, Game Developer, Using BlinkBoy'
Veteran
Joined
May 15, 2012
Messages
14,696
Reaction score
3,008
First Language
Tagalog
Primarily Uses
RMVXA
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.
 

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

Latest Threads

Latest Profile Posts

And back. Trying to work online with no internet for 1.5 days was...interesting. I felt like a character in a old Sierra game trying to find internet due to all the hoops I had to jump through just to get online for work.
grief... if MV didn't have certain plugins already, I think I'd upgrade to MZ. seeing like 10 MV plugins in 1 MZ one is hilariously convenient lol.
Have you already checkes out my mapping tutorial on the blog? I would love to hear your feedback and things you'd like to see in the future!

one of character for my upcoming game idk how to design o_O

Designing patches that can 'augmented' to clothing based armor. Such good fun. A bit time consuming due to the need to create image displays, but the patches are limited so it's not too tiring.
Note: lol, posting this made me find a couple mistakes.

Forum statistics

Threads
100,737
Messages
978,948
Members
132,359
Latest member
PhilKeepItReal
Top