Help with CTB_NearestEventMV Plugin

Hello everyone!

I'm currently trying to figure out a script that will determine the event ID that I'm looking for and store the ID in variable 27 using CTB's NearestEventMV plugin. This way I can call the event in another script call in order to change the event priority when needed. I won't always know what the event ID is going to be, which is why I need to store it in a variable.

If it was just one event at a time, that would be fine. My issue is that there are situations where multiple events could be an equal distance away from the player. I need it to be the event that's equal to $gamePlayer.x && $gamePlayer.y-1

I did find some good help on this forum post and based my current script call off of that. However, it doesn't appear to be changing the variable like I thought it was going to.

Here's my script I was attempting to run:
JavaScript:
var events = $gameMap.eventsByDistance('DekuNut');
events[0]._eventId // first closest
events[1]._eventId // second closest
events[2]._eventId // third closest
events[3]._eventId // fourth closest
if (events[0].x === $gamePlayer.x && events[0].y === $gamePlayer.y-1) {
$gameVariables.setValue(27, (events[0]._eventId))
}
else if (events[1].x === $gamePlayer.x && events[1].y === $gamePlayer.y-1) {
$gameVariables.setValue(27, (events[1]._eventId))
}
else if (events[2].x === $gamePlayer.x && events[2].y === $gamePlayer.y-1) {
$gameVariables.setValue(27, (events[2]._eventId))
}
else (events[3].x === $gamePlayer.x && events[3].y === $gamePlayer.y-1) {
$gameVariables.setValue(27, (events[3]._eventId));

Clearly this isn't correct, but I'm not sure why. I've been racking my brain most of the day trying to come up with a solution and I feel like the answer is right in front of my face, but I can't see it. Any help would very much so be appreciated!

Thank you! 🙂
Try:
JavaScript:
const p  = $gamePlayer;
const id = $gameMap.eventIdsByDistance('DekuNut')
                   .find(e => e.pos(p.x, p.y - 1));
$gameVariables.setValue(27, id);
Also, check the spelling of DekuNut and make sure it isn't case sensitive. Hope this works.
Try:
JavaScript:
const p  = $gamePlayer;
const id = $gameMap.eventIdsByDistance('DekuNut')
                   .find(e => e.pos(p.x, p.y - 1));
$gameVariables.setValue(27, id);
Also, check the spelling of DekuNut and make sure it isn't case sensitive. Hope this works.
I will definitely try this when I get a chance. It definitely looks like a much cleaner solution that mine haha.

Also, I'm not sure I 100% understand what the 'e' does, even after googling it. Though I do understand the idea of what's happening. Do you have a cliff notes explanation of what that does?
I will definitely try this when I get a chance. It definitely looks like a much cleaner solution that mine haha.

Also, I'm not sure I 100% understand what the 'e' does, even after googling it. Though I do understand the idea of what's happening. Do you have a cliff notes explanation of what that does?
Sure thing!

.find() is a method for arrays that allow you to find the first match. The parameter 'e' is for the element being referenced - an element is required when using this method. So inside an array that is [1,2,3], the parameter e will use each of those values as a reference.

Example:
JavaScript:
const array = [1,2,3];
const id = array.find(e=>e > 1);
console.log(id);
// Console will show '2'

In the case of what we are doing, we are using CTB's event id by distance function (which I am assuming is giving you an array, I could be wrong, in which I will make changes as needed).

The .find() function will search through that list, find the first value that is equal to the position of the game player's x and y value minus 1, and return that id to you.

From there, you just set the variable to the value and you are done.

More info on .find():
MDC: Array.prototype.find() Method
I've put in the script call but it didn't seem to adjust the variable either :/ Maybe I'm not understanding how that specific plugin script works? I thought I did based on his explaining in the link of the OP. Maybe not though.

Here are some screenshots if that helps, though I doubt it. I was just running the script by itself to see if it adjusted the variable and it didn't.

Spoiler: Images
467461336_1165160835031993_5085144153066064924_n.png
467467902_584804801082443_337503882496514181_n.png
Moved to Plugin Support.
I've put in the script call but it didn't seem to adjust the variable either :/ Maybe I'm not understanding how that specific plugin script works? I thought I did based on his explaining in the link of the OP. Maybe not though.

Here are some screenshots if that helps, though I doubt it. I was just running the script by itself to see if it adjusted the variable and it didn't.

That was my mistake (I wasn't able to test so I was only going off of whatever notes I had regarding this plugin - I was also thinking of the other function but changed it to id midway and forgot to update lol):
You will need to do use this one instead and set it up like this (I forgot one like of code - also made some changes):
JavaScript:
const p  = $gamePlayer;
const ev = $gameMap._events;
const id = $gameMap.eventIdsByDistance('DekuNut')
                   .find(e => ev[e].pos(p.x, p.y - 1));
$gameVariables.setValue(27, id);

Here is the result:
s1.png

s2.png


Don't forget to make sure that the event that needs to be referenced has the proper comment note tag - in this case, you need to use a Comment Command and type in <DekuNut: true/> for it to work.

And finally, do make sure you have at least up to 27 variables open in the editor in order for it to work. Click on the Variable in the Conditions part of the page and set the range to at least 30. Otherwise, the variable is undefined and you will get nothing.
Hey! That did the trick! Thank you so much, I really appreciate it 🙂

Question of curiosity though: why the switch from event notetags to comment tags? My understanding was that both were possible. Did the comments work better than the notetags for this? Or is this simply the route you decided to take?

Side note: I can generally pick up on basic Javascript as I need it, but I'm starting to think more and more that I need to take a Javascript class haha
Question of curiosity though: why the switch from event notetags to comment tags? My understanding was that both were possible. Did the comments work better than the notetags for this? Or is this simply the route you decided to take?
Just the route I decided to take since you can make variations for each page if needed.

If you were to use the event's note tag at the top, you would still do <DekuNut> or <DekuNut: true/> or whatever, assuming it does work, and it will persist for that event regardless of the page comments. Try them both out and see which one works best for you.
Just the route I decided to take since you can make variations for each page if needed.

If you were to use the event's note tag at the top, you would still do <DekuNut> or <DekuNut: true/> or whatever, assuming it does work, and it will persist for that event regardless of the page comments. Try them both out and see which one works best for you.
Yeah I thought about it after I posted and I think I do actually like the comment tags for that specific reason.

Thank you very much for all of your help!

I'm going to report this as resolved 🙂

This thread is being closed, due to being solved. If for some reason you would like this thread re-opened, please report this post and leave a message why. Thank you.

Original thread: https://forums.rpgmakerweb.com/threads/help-with-ctb_nearesteventmv-plugin.174248/