Creating a patrolling guard that will chase on sight

Hi All,

New user here. I wanted to post because I found a unique solution to a problem that, after searching for a bit, didn't have many answers.

I wanted to create a patrolling guard that would chase the player on sight. Naturally Yanfly's Event Chase Player was the beginning of the answer.

The problem I kept having was, I wanted the guard to patrol a route, chase on player sight, and return to his patrol once the player ran away.

Yanfly's Event Chase has a return function, and it worked very well, returning the guard to the beginning of his patrol after losing sight of the player.

The problem was, Yanfly's script is being called in the custom route movement section of the guard event, which is also where I need the patrol movements to run. This is because I need 'event touch' on for the player to enter combat with the guard.

What was happening is the guard would return to his patrol start, but would pick up the actual route sequence where he left off after chasing the player, i.e. if the whole pattern was (Turn Up, Move Up, Move Up, Turn Down, Move Down, Move Down) and the guard got through (Turn Up, Move Up, Move Up, CHASE PLAYER) he would return to the x,y, of the very beginning of his route, but then enact (Turn Down, Move Down, Move Down, Turn Up, Move Up, Move Up). Correct route in the wrong sequence, ending up with him patrolling the wrong place.

I finally came upon the answer by using custom movement script, and saving the x and y location of the guard after each movement. So essentially instead of returning to the patrol start x,y, he returns to the point in the patrol he started chasing from, and resumes it there. Because of the way the return function in Yanfly's works, you don't have to worry about the x,y, being overwritten again until the guard has reached its start x,y, even if you re-aggro it and drag it somewhere else as it is returning.

The code is, for (Turn Up, Move Up, Move Up, Turn Down, Move Down, Move Down) (using Yanfly's Event Chase Plugin), written in the autonomous movement custom route:

this._chaseRange = 5; //(movement script)
Turn Up //(normal call)
$gameMap.event(5).moveStraight(8);this._startLocationX = this.x;this._startLocationY = this.y; //(ALL in ONE movement script)
$gameMap.event(5).moveStraight(8);this._startLocationX = this.x;this._startLocationY = this.y; //(Another separate movement script)
Turn Down //(normal call)
$gameMap.event(5).moveStraight(2);this._startLocationX = this.x;this._startLocationY = this.y; //(ditto)
$gameMap.event(5).moveStraight(2);this._startLocationX = this.x;this._startLocationY = this.y; //(ditto)


Basically the stock movements are replaced with script movements, and each appended by overwriting the start x,y. It doesn't need to be done for turns, as they are the same x,y. Hope this helps someone in the same situation.

Original thread: https://forums.rpgmakerweb.com/threads/creating-a-patrolling-guard-that-will-chase-on-sight.117544/