making a monster follow player

im creating a game and I want the monster to follow the player if the player gets 5 steps away from him
I tried using this but the math does not add up
Try putting this in a Conditional Branch script box on the event, and make it parallel

Step 1
Replace 'id' with the number of your event (top left corner of event menu)
$gameMap.distance($gamePlayer.x, $gamePlayer.y, $gameMap.event(id).x, $gameMap.event(id).y) >= 5

Step 2
Inside of the conditional branch, use the event command 'Script'
And place the following code. Again, replace 'id' with the number of your monster event

$gameMap.event(id).moveTowardPlayer()


Alternative to step 2
Inside of the conditional branch, do a move route. Select 'This Event' as the target in the dropdown menu. Then, select the 'Script' button in the bottom right corner of the Move Route menu and type the following:

this.moveTowardPlayer()

I tested it, it works fine, let me know if it works the way you want it to.

EDIT: FYI the script call
$gameMap.distance(x1, y1, x2, y2) takes the x and y values for two different sets of coordinates and returns the shortest 'distance', or minimum amount of moves needed for one to reach the other.

You can use that script call to measure any sets of coordinates, but in this case it's for the Player's X and Y, and the Event's X and Y
OP you only have the positive outcome of the calculation in your conditional branches.
You forget the If variableX is -4 or less.....

You could also do:
Player Map X - Event Map X = Var 1
Player Map Y - Event Map Y = Var 2
If Var 1 is less than 0, if yes Var 1 multiply with -1 (To get a positive number in case its a negative one)
If Var 2 is less than 0, if yes Var 2 * -1
Var 1 + Var 2 = Var 3 (The distance to the Event)
Conditional Branch Variable 3 is 5 or more
Set movementroute Zombie, skip ,Wait. Move towards player a couple of times.

Edit: @standardplayer thank you for sharing the Script Call Lines, those are awesome,
i will definitelly make use of those. 🙏
Try putting this in a Conditional Branch script box on the event, and make it parallel

Step 1
Replace 'id' with the number of your event (top left corner of event menu)
$gameMap.distance($gamePlayer.x, $gamePlayer.y, $gameMap.event(id).x, $gameMap.event(id).y) >= 5

Step 2
Inside of the conditional branch, use the event command 'Script'
And place the following code. Again, replace 'id' with the number of your monster event

$gameMap.event(id).moveTowardPlayer()


Alternative to step 2
Inside of the conditional branch, do a move route. Select 'This Event' as the target in the dropdown menu. Then, select the 'Script' button in the bottom right corner of the Move Route menu and type the following:

this.moveTowardPlayer()

I tested it, it works fine, let me know if it works the way you want it to.

EDIT: FYI the script call
$gameMap.distance(x1, y1, x2, y2) takes the x and y values for two different sets of coordinates and returns the shortest 'distance', or minimum amount of moves needed for one to reach the other.

You can use that script call to measure any sets of coordinates, but in this case it's for the Player's X and Y, and the Event's X and Y
Thank you for your help!
OP you only have the positive outcome of the calculation in your conditional branches.
You forget the If variableX is -4 or less.....

You could also do:
Player Map X - Event Map X = Var 1
Player Map Y - Event Map Y = Var 2
If Var 1 is less than 0, if yes Var 1 multiply with -1 (To get a positive number in case its a negative one)
If Var 2 is less than 0, if yes Var 2 * -1
Var 1 + Var 2 = Var 3 (The distance to the Event)
Conditional Branch Variable 3 is 5 or more
Set movementroute Zombie, skip ,Wait. Move towards player a couple of times.

Edit: @standardplayer thank you for sharing the Script Call Lines, those are awesome,
i will definitelly make use of those. 🙏
Thank you!
In the end I used advice from both posts. I created a variable that kept track of the player's position of the map, and it included a variable for Map X and MapY for the player, I called these variable_y and variable_x. Than I created the zombie even and used this code.
upload_2019-7-14_9-11-12.png


after both conditions were met, switchs 0005 and 0007, I used a second page for the following code.
to turn towards player, and make a roaring noise as if it had just spotted the player
upload_2019-7-14_9-12-57.png

, than the 3rd page made the zombie run towards player.
upload_2019-7-14_9-17-10.png


I hope this post helps someone in the future. 🙂

Original thread: https://forums.rpgmakerweb.com/threads/making-a-monster-follow-player.110976/