OK, so I have an event that's set to trigger a battle upon touching the player, and that works fine, but what I need it to do is wander the map randomly, until it comes within, say 5-6 spaces of the player, at which point it will begin moving towards them. How would I go about doing this?
Change event's move route when near player?
RPG Maker MV SupportSo to do this you'd need two things:
1 - Check the distance between the player and an event
2 - Change an event move type
Here is how you do 2 - $gameMap._events[id]._moveType = x
With x being the type of movement in the drop down menu in the editor (0 for fixed, 1 for random,...)
Checking the distance between an event and the player is achievable in this way:
if (Math.abs($gamePlayer.x - $gameMap._events[id]._x) + Math.abs($gamePlayer.y-$gameMap._events[id]._y) < distance) {
//change distance to the number of tiles
if ($gameMap._events[id]._moveType !== 2) {alert("entering range")} // change the alert to what you want to do
//when the player enters range
$gameMap._events[id]._moveType = 2 //sets event id move type to approach
} else {
if ($gameMap._events[id]._moveType !== 2) {alert("exiting range")} // change the alert to what you want to do
//when the player exits range
$gameMap._events[id]._moveType = 1 //sets event id move type to random
}
This code should work. It does in my project anyway. Have a parallel process with this script call checking every few frames should get you the desired effect.
I'd recommend editing it a bit, to not set the movetype to a number if it already is that number for example.
You could even edit it further to display a balloon over the triggered event when in range if you wanted.
1 - Check the distance between the player and an event
2 - Change an event move type
Here is how you do 2 - $gameMap._events[id]._moveType = x
With x being the type of movement in the drop down menu in the editor (0 for fixed, 1 for random,...)
Checking the distance between an event and the player is achievable in this way:
if (Math.abs($gamePlayer.x - $gameMap._events[id]._x) + Math.abs($gamePlayer.y-$gameMap._events[id]._y) < distance) {
//change distance to the number of tiles
if ($gameMap._events[id]._moveType !== 2) {alert("entering range")} // change the alert to what you want to do
//when the player enters range
$gameMap._events[id]._moveType = 2 //sets event id move type to approach
} else {
if ($gameMap._events[id]._moveType !== 2) {alert("exiting range")} // change the alert to what you want to do
//when the player exits range
$gameMap._events[id]._moveType = 1 //sets event id move type to random
}
This code should work. It does in my project anyway. Have a parallel process with this script call checking every few frames should get you the desired effect.
I'd recommend editing it a bit, to not set the movetype to a number if it already is that number for example.
You could even edit it further to display a balloon over the triggered event when in range if you wanted.
there is no direct solution for this, you need something to calculate the distance and change the behaviour based on that.
For a very limited amount of events that can be done with parallel processes, but if you want more than a few events with that behaviour, you'll have to look for a plugin.
For a very limited amount of events that can be done with parallel processes, but if you want more than a few events with that behaviour, you'll have to look for a plugin.
@Andar: I wonder, why would you need a plugin?
A parallel process with a while loop that does what I posted earlier for each event Id provided would work perfectly? It would only use 1 parallel process event, and you could do the checks for 50 events if you wanted. By doing it inside a common event and drawing the ids to check from a game variable you could very well use this in all your maps.
Or is there something I am missing? Lag maybe?
@Astfgl66 yes, lag is the point you're missing. That's also why I didn't give any numbers - it depends on your computer how much processing the engine can handle without the player experiencing lag.
And unfortunately all lag from all events is cumulative, so even if you don't experience any problems on earlier or smaller maps, that will go up as soon as you're using parallel processes for other parts as well.
If the OP knows that this will be the only complex parallel in his/her game at all, then there is no problem to using a parallel process even with two or three dozen enemies here. But that is something we can only guess...
And unfortunately all lag from all events is cumulative, so even if you don't experience any problems on earlier or smaller maps, that will go up as soon as you're using parallel processes for other parts as well.
If the OP knows that this will be the only complex parallel in his/her game at all, then there is no problem to using a parallel process even with two or three dozen enemies here. But that is something we can only guess...
Andar,
Does this sound like a workable alternative for small maps with chokepoints:
You could step onto an event that triggers a switch/variable. That switch would dictate a new page for the "enemy" event where it moves towards the player and activates a battle on player touch, then self switches itself off (or uses another variable if you want it to respawn when you leave and re-enter)
I don't think that would require any parallel processes. And if you used variables that reset themselves you could ease the number of switches required. Does that sound workable? Any lag considerations there? I'd be interested to hear your thoughts (I'm just not comfortable with scripting yet so I'd prefer eventing it so I can modify as needed)
Yes, it can be done this way (like turning all enemy events in a room to approach when stepping into the door).
A lot depends on your map design (approach doesn't work well if there are a lot of obstacles or walls between player and event), and there are several ways on how to handle the reset that depend on your actual game structure, but that is a way that a lot of people use.
I think this plug in would solve the issue for you.
http://yanfly.moe/2015/10/21/yep-20-event-chase-player/
Insert these lines into the script call window within the Movement Route event to give an event the chase or flee flag.
Script Call lines
this._chaseRange = x
Event will chase player if reaching x range.
this._fleeRange = x
Event will flea from player if reaching x range.
this._chaseSpeed = x
Event’s movement speed when chasing.
this._fleeSpeed = x
Event’s movement speed when fleeing.
this._sightLock = x
Event will flee/chase player for x frames.
this._seePlayer = true
Requires the event to be able to see player.
this._seePlayer = false
Doesn’t require event to be able to see player.
this._alertBalloon = x
This balloon will play when player is seen.
this._alertSound = x
This sound will play when player is seen.
this._alertCommonEvent = x
This event will play when player is seen.
It is best to play this inside of a custom move route for the event at a high frequency rate. Keep in mind these effects only occur after the setting is made and ran, which means upon loading a map, if the event with a low frequency hasn’t loaded up ‘this._chaseRange = x’ in its movement queue yet, the event will not chase the player just yet.
Well, I'm going to sound like a broken record maybe and advocate doing it via scripting again.
After doing a lot of checking I've found that what really matters in terms of fps is really the number of parallel process, not the complexity.
I've done some testing with checks for 100 events over a 60x60 map and just that parallel process and got flawless results (my events changed movetype and displayed baloon animations over their head when the player was entering or exiting range) without a single fps drop (fps literally capped at 60). I've done experiments with 10 simple parallel processes at the same time and found out that fps did drop (and my computer isn't some kind of beast).
So as long as you're smart and use a single common event as a parallel and switches for different system inside that common event you should be fine.
That's all for PC, now for mobile I can't say.
So my conclusion would be:
For ease of use a plugin is best, for simple things normal eventing works, for something custom tailored to your needs scripting it yourself is probably the better idea.
Edit: Not that that is some particularily complex eventing anyway. For ace, as an exercise, I fully evented an ATB battle system with two main parallel events and got no fps drop either... And trust me those events were much more complex and involved lots of calculations.
After doing a lot of checking I've found that what really matters in terms of fps is really the number of parallel process, not the complexity.
I've done some testing with checks for 100 events over a 60x60 map and just that parallel process and got flawless results (my events changed movetype and displayed baloon animations over their head when the player was entering or exiting range) without a single fps drop (fps literally capped at 60). I've done experiments with 10 simple parallel processes at the same time and found out that fps did drop (and my computer isn't some kind of beast).
So as long as you're smart and use a single common event as a parallel and switches for different system inside that common event you should be fine.
That's all for PC, now for mobile I can't say.
So my conclusion would be:
For ease of use a plugin is best, for simple things normal eventing works, for something custom tailored to your needs scripting it yourself is probably the better idea.
Edit: Not that that is some particularily complex eventing anyway. For ace, as an exercise, I fully evented an ATB battle system with two main parallel events and got no fps drop either... And trust me those events were much more complex and involved lots of calculations.
OK, well using the script calls in a parallel process appears to have worked perfectly so far. 🙂 If it gets laggy I'll switch to the plugin, though I'm not likely to have many other parallel processes, so it sould work fine. Thanks for the help. :3
Original thread: https://forums.rpgmakerweb.com/threads/change-events-move-route-when-near-player.63537/