RPG Maker VX Ace

RPG Maker VX Ace

View Stats:
make NPC or Enemy Approach player when within a few squares of event?
RPG Maker VX ACE

I'm trying build an enemy system where enemies (or Npc's if needed) randomly walk around an
area, but when the Player moves into range the enemy changes to intercept you (thus starting a battle), I understand fine how to allow sprites to randomly move or to approach but how do I get the sprite to begin on random movement and then change to approach (but only if the player is nearby? Similar systems are like Earthbound back on Snes. If anyone has any ideas that would be great.
< >
Showing 1-14 of 14 comments
Karanum Jul 5, 2014 @ 10:47am 
You can get the X and Y positions of both the player and the monster, store them in variables and calculate the distance to the player. If that distance is smaller than the detection range of the monster, you flip a self switch and change the movement of the new page to follow the player.

If you're not familiar with variables or run into some other problem, I can write something out in more detail if you want.
RosstheBoss0 Jul 5, 2014 @ 12:21pm 
Ohh cool, I'm pretty familiar with switches and such but I'm not too familiar on how to calculate and store the coordinates of the player and the monsters, that would be great to know if it's explainable :)
Karanum Jul 5, 2014 @ 1:04pm 
I'm going to apologize in advance in case this is really messy and hard to follow.

You can either do this completely through variable operations and such, or with a little bit of scripting. The latter method only requires 1 variable and this is how you should set up your pages:

Page 1[i.imgur.com]
Page 2[i.imgur.com]

If you want to do this with event commands only, reserve about half a dozen variables:

Make a new event command, on the first page there's Control Variables... -> Set -> Game Data... -> Character [Player]'s [Map X] and store that in an empty variable. Do the same for [Player]'s [Map Y], [This event]'s [Map X] and [This event]'s [Map Y].

Now take the variable containing the Player's X, and subtract the This event's X from it. Do the same for the Player's Y and the This event's Y. Make a Conditional Branch to check if either of them is negative, and if so, multiply that variable by -1 to make it positive.

If both the X-difference and Y-difference are positive, just add them together and you have the distance (in tiles) from the monster to the player. Then you can just do another Conditional Branch to see if it's close enough for the monster to start following.
Hajami Jul 5, 2014 @ 2:16pm 
I have one Solution.
Define a range of which event ids should be checked.
Determine a Radius. If the Event is inside it, the Self Switch A of that Event will turn on.
On that new Eventpage you could make a new moveroute where the event chases the player.


1. Create a Common Event with Trigger None.
2. Use the Eventcommand "Script" on the 3rd page of Event Commands.
And paste the following Code:
#----------------------------------------------
#Hit_Box
#----------------------------------------------
for a in (1..50) #<-Range of Enemy_Ids#
if $game_map.events[a] != nil
if Math.hypot($game_map.events[a].screen_x -
$game_player.screen_x , $game_map.events[a].screen_y -
$game_player.screen_y) <= 32 #<--Pixel Range Radius#
$game_self_switches[[$game_map.map_id, a, 'A']] = true
end ; end
end

3.
Make one Event on your Map that is Paralell processing. (You will have 1 Event that can handle hundrets thats better than having too many parallel events at the same time).
4.
Insert the Eventcommand: Call Common Event (Choose the Common Event which include our above Code)
5.
Insert the Eventcommand: Wait 60Frames (Thatway its only asking once per seccond on that map)
Last edited by Hajami; Jul 5, 2014 @ 3:06pm
RosstheBoss0 Jul 5, 2014 @ 3:00pm 
Wow, thanks for the help guys, hopefully I can get my head round it. :)
Hajami Jul 5, 2014 @ 3:07pm 
sorry i corrected my code, because i pasted a mistake.
now it should work.

Edit: Insteed of making a common event you could just use the paralell process without calling the common event. i do it with common event because i use same system on several maps. and its easier to change one commomnn event than many events one per map.

Edit2: there are many different ways to build something that does exxactly what people want^^.
Last edited by Hajami; Jul 5, 2014 @ 3:11pm
RosstheBoss0 Jul 5, 2014 @ 3:22pm 
Ok, so I've given both an attempt and I have one or two questions, for the triggers in your solution Karanum which variable would the conditional branch be checking?
and for yours Hajami, how would I define a range of Id's and a radius at the beginning of the solution?
Karanum Jul 5, 2014 @ 3:47pm 
If you're trying the script-based solution, then in my example it checks variable 15. You can just change that by changing all instances of the number 15 to whatever variable is available.

Otherwise, it depends on the variable you do the calculations on. You'd want to use at least 4 variables (to store the player's x and y and the monster's x and y) and then it's up to you. Either you can make a new variable and do the calculations in that one, though it's faster to just take one of those 4 starting variables and do the calculations on those.

(e.g.: Variable 1 has the player's x, variable 3 has the monster's x. You subtract variable 3 from variable 1. Variable 1 now contains the distance over the x-axis.)
Hajami Jul 5, 2014 @ 3:53pm 
I just wanted to explain how it works with that words you asked about, my english aint the best so i maybe failed that.
You just have to follow the steps but you can change the values in the event script.

for a in (1..50) <--These are the Event Ids that will be Checked when the Common Event is Called. In this Case Events 1 till 50 you can change the numbers.

if Math.hypot($game_map.events[a].screen_x -
$game_player.screen_x , $game_map.events[a].screen_y -
$game_player.screen_y) <= 32 #<-- 32is a Radius of 1 Map Tile. Change the number to change the Radius to the distance you want.

Edit: Iam no Scripter, i just know a little operators and some logic, so i cant help with advanced solutions, but like i said there are many ways to achieve the same goal.
Last edited by Hajami; Jul 5, 2014 @ 3:57pm
RosstheBoss0 Jul 5, 2014 @ 4:05pm 
Ohh ok, I think I'm understanding the ideas a lot better, I'm just giving them a go
Last edited by RosstheBoss0; Jul 5, 2014 @ 4:09pm
RosstheBoss0 Jul 6, 2014 @ 7:15am 
I've got it working :D Now I'm just wondering if it's possible to have it so that if the player were to move out of a certain range of the enemy after it has begun its approach would be possible?
Karanum Jul 6, 2014 @ 7:24am 
What method are you using to make it work?
Nightblade Jul 6, 2014 @ 9:00am 
You may also want to try Yanfly's Event Chase script. It has most of what you're looking for.
Last edited by Nightblade; Jul 6, 2014 @ 9:01am
lassipls Apr 28, 2016 @ 3:09am 
I made a post in the RPG Maker MV forums for MV version of this:

http://steamcommunity.com/app/363890/discussions/0/357284767233145316/
< >
Showing 1-14 of 14 comments
Per page: 1530 50

Date Posted: Jul 5, 2014 @ 9:38am
Posts: 14