How to check player proximity to event

Objective: to create a parallel event that makes a sound until the player comes to it, so then another action will be triggered.

For now I made it with two events: One parallel event creates sound and checking if my utility variable value = 146. Another event right next to it is an autorun event, but it has YEP proximity activate condition, so it activates only when player is in range of 5 tiles. All it does is sets utility variable value to 146 and deletes itself, so another event will react to it and launch the main event instead of looping sound.

But I don't think it's the best approach, and because it's the second time I have to make such event, I want to improve it. Is there a way to make everything in one event, using the conditional branches? I haven't found player proximity in branches' options, but there is option for scripts. Will it be hard to write such a script?
the key to this are mathematics and control variable.

you can use control variable to get the player coordinates into two variables, and then either coordinates or another event position into two more coordinates.

after that it is only https://en.wikipedia.org/wiki/Pythagorean_theorem to calculate the distance between the positions
Correct, but you need a direct solution for checking the distance between events (player pos too)
is method within MV:

$gameMap.distance()

example usage:
wwEuXdG.png
Might check YEP_EventChasePlayer. it has a distance option, and some activation options, i don't think it actually HAS to chase anyone.
Correct, but you need a direct solution for checking the distance between events (player pos too)
is method within MV:

$gameMap.distance()

example usage:
wwEuXdG.png
I honestly don't understand, is this script self-sufficient, or do I need to assign values to variables myself?

1650666263323.png

It doesn't really work just like that. Did I made a mistake?

Might check YEP_EventChasePlayer. it has a distance option, and some activation options, i don't think it actually HAS to chase anyone.
I checked it, it has the common event activation option. I can use common event to assign some variable value to specific number to activate the main event. If I won't be able to make it with scripts, I will use this method, thank you.
So @redmedved2 you want a system that tracks the distance between the player and an NPC event? Then this might work for you:

FhK7jE1.png


(Ignore the "R Button is pressed" part. Also the first two variables track the NPC's X and Y coordinates)

This is a system I cooked up in VX Ace that tracks how far away you are from an event in tiles, and it's extremely powerful since it even tracks diagonal distance. Just set it up as exactly as pictured and it should track how far away the player is from an event if that's what you need.

Just be careful: you have to count negatives as well. It would look like this in a conditional branch's script part -
$game_variables[902] == 1 || $game_variables[902] == -1
(This particular version tracks if you're directly adjacent to an event.)

Likewise, this version tracks if you're in distance of 5 tiles:
$game_variables[902] <= 5 || $game_variables[902] >= -5

This version tracks if you're over 5 tiles away:
$game_variables[902] >= 6 || $game_variables[902] <= -6

... Only trade in the script format for what MV uses ($gameVariables.value(variableId) / value);) and you should be good.
@redmedved2 It's self sufficient, just replace the eventId with actuall ID of event you want to check, I'm using exactly the same code as parallel event in escort quest in my game and it works. But I see I've made a mistake in value comparison, I used lower or equal <= but it should be greater or equal >=
@redmedved2 It's self sufficient, just replace the eventId with actuall ID of event you want to check, I'm using exactly the same code as parallel event in escort quest in my game and it works. But I see I've made a mistake in value comparison, I used lower or equal <= but it should be greater or equal >=
Hi there, I just tried this and am getting a SyntaxError: Missing ) after argument list. I went ahead and removed the conditional "if" section to figure out where it thinks it wants an end-parenthesis and it wants it somewhere in the initial script.

I get the error like this and with the full copy of what you did. Any ideas?
1664221127203.png
Hi there, I just tried this and am getting a SyntaxError: Missing ) after argument list. I went ahead and removed the conditional "if" section to figure out where it thinks it wants an end-parenthesis and it wants it somewhere in the initial script.

I get the error like this and with the full copy of what you did. Any ideas?
View attachment 241329


"$gameMap.event" is a function that looks for an event number as it's parameter.
So your code above should be something like...
JavaScript:
$gameVariables.setValue(10, $gameMap.event(91).x)
That would store event #91's x into variable 10

or...
JavaScript:
$gameVariables.setValue(10, $gameMap.event(this._eventId).x)
That would store the current event's x into variable 10

Alternatively if you really want to use the name to get the event... could be done like this:
JavaScript:
var id = $dataMap.events.filter(function(event){if (event) {return event.name === "Harpy 1"};})[0].id;
$gameVariables.setValue(10, $gameMap.event(id).x)
That would store the x location of the event named "Harpy 1" into variable 10

...
That said if you want I do have a plugin to do such things & more.
Originally created for another user just to check for the nearest event.. but has expanded into more now.

CTB_NearestEventMV Plugin: Click Here to Download

Sooo anyways with that plugin then to do what you have above could then be done like this:
Spoiler
1664236438067.png
or alternatively.. same thing..
Spoiler: more script based approach
1664236804418.png
...and to use my plugin to do this by the name of the event would be like this:
Spoiler
1664238309700.png
I really appreciate all the helpful tips!!

This is what I found that was simplest and accomplished exactly what I was looking to do:

1664341227681.png

Original thread: https://forums.rpgmakerweb.com/threads/how-to-check-player-proximity-to-event.146996/