Trying to find a distance between Player and Event

PKirkby

Veteran
Veteran
Joined
Oct 26, 2019
Messages
154
Reaction score
14
First Language
english
Primarily Uses
RMMV
Hey everyone, I'm trying to find a simple way to get distance between a player and an event. I am using a proximity event plugin, but it doesn't allow anything for if that the player leaves that proximity. I know how to load play and event map/screen locations in variables, but I can't figure out how to use them, so I think seeing it done would help alot.

So the just of this is I need an event to run some code (stop playing music, go back to event location) when I get so far away from it. I'm ok with scripts, plugins or even just eventing. Thanks in advance!
 

Kes

Global Moderators
Global Mod
Joined
Aug 3, 2012
Messages
21,398
Reaction score
10,830
First Language
English
Primarily Uses
RMVXA
Tutorials is where people who have made tutorials can post them

I've moved this thread to RPGMaker MV. Please be sure to post your threads in the correct forum next time. Thank you.

 
Joined
Jun 30, 2017
Messages
30
Reaction score
24
First Language
spanish
Primarily Uses
RMMV
Probably there is a plugin to do that, but you can use this script call to store the distance between player(x,y) and event(x,y) into a variable:

Code:
var player = {
 'x':$gamePlayer.x,
 'y':$gamePlayer.y,
}; var event = {
 'x':$gameMap.event(1).x,
 'y':$gameMap.event(1).y
};
var dist_y = player.y - event.y;
var dist_x = player.x - event.x;
var dist = Math.sqrt(dist_x*dist_x + dist_y*dist_y);
$gameVariables.setValue(15,dist);
$gamemap.event(1) -> 1 is the event ID
$gameVariables.setValue(15,dist); -> 15 is the variable that stores the distance.

You can use it on a parallel evento to constantly check the distance.
Distance is returned as a number with decimals, diagonal distances are sometimes returned as numbers like 2,83748374

Example parallel event to switch to night screen tint when player is more than 6 cells away from event 1.

event.png
 
  • Like
Reactions: Bex

Andar

Veteran
Veteran
Joined
Mar 5, 2013
Messages
28,969
Reaction score
6,667
First Language
German
Primarily Uses
RMMV
you don't even need a script, all that data is directly available through control variable - game data (and then the map coordinates for event or player)
 
Joined
Jun 30, 2017
Messages
30
Reaction score
24
First Language
spanish
Primarily Uses
RMMV
you don't even need a script, all that data is directly available through control variable - game data (and then the map coordinates for event or player)
Andar, yes but I think sqrt is not avaiable as a math operation on variables,
you need at least a "set variable = Script" to do that.
 

PKirkby

Veteran
Veteran
Joined
Oct 26, 2019
Messages
154
Reaction score
14
First Language
english
Primarily Uses
RMMV
Tutorials is where people who have made tutorials can post them

[move]RPGMaker MV[/move]
Thanks, I didn't actually know where this should belong.
 

Bex

Veteran
Veteran
Joined
Aug 2, 2013
Messages
1,306
Reaction score
313
First Language
German
Primarily Uses
RMMV
Raw example:

Var1 = Player map x
Var1 - Event map x
Conditional Branch Var 1 less than 0 ? if yes Var1 * (-1)

Var2 = Player map y
Var2 - Event map y
Conditional Branch Var 2 less than 0 ? if yes Var2 * (-1)

Var1 + Var2 (= Distance of Event to Player, Diamond Shape )

Edit: Here an usage example, its just one possible Methode, but there are different ones, depending on the wanted outcome.

If Var1 is equal or more than 0
If Var1 is less than 3
If Var3 is not equal to 1 (With this Condition you execute the following Code just one time when needed. if its permanently executed, that could cause Performance Problems or the Music would start over and over all the time)
Yes: Set Var3 = 1
Play BGM River Max Volume

If Var1 is equal or more than 4
If Var1 is less than 6
If Var3 is not equal to 2
Yes: Set Var3 = 2

Play BGM River Medium Volume

If Var1 is equal or more than 7
If Var3 is not equal to 3
Yes: Set Var3 = 3
Stop BGM River

The Closer you come towards the Event, the louder the River Music or Sound would Play. The further away you get, the more it would fade off.​
 
Last edited:

PKirkby

Veteran
Veteran
Joined
Oct 26, 2019
Messages
154
Reaction score
14
First Language
english
Primarily Uses
RMMV
Raw example:

Var1 = Player map x
Var1 - Event map x
Conditional Branch Var 1 less than 0 ? if yes Var1 * (-1)

Var2 = Player map y
Var2 - Event map y
Conditional Branch Var 2 less than 0 ? if yes Var2 * (-1)

Var1 + Var2 (= Distance of Event to Player, Diamond Shape )

Edit: Here an usage example, its just one possible Methode, but there are different ones, depending on the wanted outcome.

If Var1 is equal or more than 0
If Var1 is less than 3
If Var3 is not equal to 1 (With this Condition you execute the following Code just one time when needed. if its permanently executed, that could cause Performance Problems or the Music would start over and over all the time)
Yes: Set Var3 = 1
Play BGM River Max Volume

If Var1 is equal or more than 4
If Var1 is less than 6
If Var3 is not equal to 2
Yes: Set Var3 = 2

Play BGM River Medium Volume

If Var1 is equal or more than 7
If Var3 is not equal to 3
Yes: Set Var3 = 3
Stop BGM River

The Closer you come towards the Event, the louder the River Music or Sound would Play. The further away you get, the more it would fade off.​
Great, I'm going to fool around with this and get back to you. Appreciate the response.
 

Bex

Veteran
Veteran
Joined
Aug 2, 2013
Messages
1,306
Reaction score
313
First Language
German
Primarily Uses
RMMV
I made a mistake in the hurry.

If Var1 is less than 3
it Needs to be
If var 1 is equal or less than 3.

same for the block with the number 6.

Edit:
If you want to use the Scriptline in Event Commmands, there is a 3rd possible solution.
I remember @caethyril mentioning it in the past.

$gameMap.distance(x1, y1, x2, y2)
and the Code to use would look like this for variable1:

$gameVariables.setValue(1, $gameMap.distance($gamePlayer.x, $gamePlayer.y, $gameMap.event(1).x, $gameMap.event(1).y) )

or this with direct coordinates:

$gameVariables.setValue(1, $gameMap.distance($gamePlayer.x, $gamePlayer.y, 8, 15) )
 
Last edited:

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

Latest Threads

Latest Posts

Latest Profile Posts

A wave of cold air has hit Canada... I know it's "always cold", but this one is especially cold... I'm freezing to death lol.
Have fun with English: a "fire grenade" is not portable napalm but a fire extinguisher tool.
2 days ago someone tried to convert me to Islam. Today youtube tried to convert me to Catholicism. The only thing left would be to meet sone Jehova's witnesses.
I just successfully tested changing music by region, and changing it between continents on the world map after getting on/off the airship. I had to override the default audio transitions with eventing to make it change smoothly but it worked.
Stream will be going live shortly with some more Witcher 3, then we will switch to another game! Feel free to drop by~

Forum statistics

Threads
93,419
Messages
912,206
Members
122,926
Latest member
JoetSmith
Top