Player/Event Distance conditional script help

MV 1.6.1

I need an event to be moving towards player (from any direction) and then change speeds based on distance.

ex.

-Event if more then 5 tiles away from player has a speed of 5,
-Event if is more than 2 but less than 5 tiles away from player has a speed of 4,
-Event if equal to 2 stops.
-then reverses the process as player moves away.

plugins help get the event to stop, but i cant seem to figure out a way to reverse the process with just plugins. ive done yanfly/mog/various other sensorswitches.

I would love to be able to run a conditional script based on distance!
so far some lunatic server help was given in a few different forms:

Math.pow((Math.pow(($gamePlayer.screenX() - $dataMap.events(e).screenX()), 2) + Math.pow(($gamePlayer.screenY() - $dataMap.events(e).screenY(), 2))),0.5) > (48 * 5)


Math.pow(Math.pow($gamePlayer.x - $gameMap.events()[e].x, 2) + Math.pow($gamePlayer.y - $gameMap.events()[e].y, 2),0.5) > 5


I've tried both these methods, without any luck. The Bottom script turns on the events self switch no matter what distance, so i believe it its in the right direction.

Any ideas?
Well, what a messy code.
conditional branch: script:
Code:
Math.abs($gameMap.event(1).x - $gamePlayer.x) <= 2 && Math.abs ($gameMap.event(1).y - $gamePlayer.y) <= 2
chamge event speed to 0.
else:
Code:
conditional branch: script:

Math.abs($gameMap.event(1).x - $gamePlayer.x < 5) && Math.abs ($gameMap.event(1).y - $gamePlayer.y) < 5
chage event speed to 4

else change event speed to 5
@bazrat
One question: Do you want your limit in real distance or in grid distance?

The default movement has no diagonal steps, which is why the squareroot formula for distance might be unneccassarily complex and poryg gave you the simpler grid-based formula. However if both XY have a difference of 2 that would still count as in range of 2 for the grid distance despite the fact that the real distance is almost 3 for those coordinates.
Well, what a messy code.
conditional branch: script:
Code:
Math.abs($gameMap.event(1).x - $gamePlayer.x) <= 2 && Math.abs ($gameMap.event(1).y - $gamePlayer.y) <= 2
chamge event speed to 0.
else:
Code:
conditional branch: script:

Math.abs($gameMap.event(1).x - $gamePlayer.x < 5) && Math.abs ($gameMap.event(1).y - $gamePlayer.y) < 5
chage event speed to 4

else change event speed to 5


is there a way to change the event speed to 0? seems like i can only choose slow, 1
@bazrat
One question: Do you want your limit in real distance or in grid distance?

The default movement has no diagonal steps, which is why the squareroot formula for distance might be unneccassarily complex and poryg gave you the simpler grid-based formula. However if both XY have a difference of 2 that would still count as in range of 2 for the grid distance despite the fact that the real distance is almost 3 for those coordinates.

i am using diagonal movements in the game, but i dont really care if the event stops directly diagnol to the player i just want the event to have a 1 tile space gap on the left/right up/down of player.

and then as player moves away from event and gets like 3-5 tiles away it starts walking toward you, then when the player gets 5 spaces away event it starts running toward you. and then vice versa as player stops moving. i hope i explain this right..
$gameMap.event(1)._speed = 0 if I remember.
[CODE said:
conditional branch: script:

Math.abs($gameMap.event(1).x - $gamePlayer.x < 5) && Math.abs ($gameMap.event(1).y - $gamePlayer.y) < 5[/CODE]
chage event speed to 4

else change event speed to 5

Poryg that worked so well, thank you!

I was having trouble getting the < 5 part to work but found that i had to edit this last part of your code. the ) wasen't behind that first player x

Code:
quick fix:

Math.abs($gameMap.event(1).x - $gamePlayer.x) < 5 && Math.abs ($gameMap.event(1).y - $gamePlayer.y) < 5

thank you so much works like a charm!
Yeah, I typed it on the mobile, so had no way to check 😀
Yeah, I typed it on the mobile, so had no way to check 😀

btw what is the difference between Math.pow and Math.abs?
They are different things, pow is power and abs is absolute value.
Math.pow
Math.abs

There's no reason to use Math.pow unless you're using an old version of MV. You can just use the exponent operator, i.e.
Code:
let result = 3 ** 4 // 81

Original thread: https://forums.rpgmakerweb.com/threads/player-event-distance-conditional-script-help.104281/