Making an Detect -> Chase -> battle -> corpse enemy event

Hi I recently started developing with rpg maker MZ and have little experience with the engine, I was looking for a way to make an event that represents an enemy to detect the player when the player is within a certain range of it, to then start chasing the player, and once the event touches the player to initiate a battle, and if the battle is won the event is made stationary and can be interacted with the action button. kind of like fear and hunger, or almost exactly like fear and hunger lol. Ive done some digging and found that F&H used a plugin for MV, but I would prefer to stick to MZ. I already found a script that lets an event chase the player and it works how I want it to. I just cant figure out the detection and starting the battle, because currently I have it so that the event begins chasing the player when the player steps within a region (in this case 1), but when the player leaves the region the event stops chasing until player re-enters the region, its not bad but not perfect, and either way I can't figure out how to make it to were when the event touches the player the battle starts because I have to set the event trigger to parallel instead of event touch inorder for the event to move when the player steps within its assign region. any help would be appericated!

the code i used:
$gameMap._events[this._eventId].moveStraight($gameMap._events[this._eventId].findDirectionTo( $gamePlayer.x,$gamePlayer.y ));
and the video I got it from:


Ive attached images of the content of the event and the region map
$gameMap._events[this._eventId].moveStraight($gameMap._events[this._eventId].findDirectionTo( $gamePlayer.x,$gamePlayer.y ));
You can shorten that quite a bit to:
JavaScript:
this.moveStraight(this.findDirectionTo( $gamePlayer.x,$gamePlayer.y ));

As to your actual problem, if I understand correctly, you want the event to never stop chasing the player once it's started, right? I think a good way to do this would be separate out the region detection from the movement.

  1. Page 1 of your event will be the version of the enemy that hasn't detected you yet. It can be fixed, or move at random, or whatever you prefer.
  2. Page 2 of your event will be the version of the enemy that has detected you. It's activated by a switch, let's call it #0001 EnemyAlerted. Set the move type to Custom and put your pathfinding script into the route in the left panel, rather than in a Set Movement Route command. Don't forget to tick "skip if cannot move". This page is set to trigger on Event Touch, and the commands on the right side trigger the battle and set Self Switch A when you win.
  3. Page 3 of your event is the corpse. Triggers on Action Button and gives whatever items. It's activated by Self Switch A, and sets Self Switch B so you can't take the item twice.
  4. Page 4 is a corpse that has already been searched. It could be a blank page, or it could use the corpse icon, up to you. It's activated by Self Switch B.

Then you need an event that activates when you step into the specified region. You can find a plugin that can let you do this, or you can just make an event set to Player Touch, get it all set up, and then copy-paste it to cover the entire boundary of the region. The content of this event is just to set switch #0001 EnemyAlerted (or whatever switch you chose) to ON. It has a second page, activated by that switch, which does nothing.
Depending in how you want to tackle this, there are may ways to do it, even without plugins.
I've been watching the same videos, I remember there is one where he uses arrays to do this.
In my case, since I tend to have Visustella plugins, I basically made a version of the same thing but with self switches and self variables using EventsMoveCore.

I can't put my code here because, well....let's say that because of forum rules, I can't even tackle the subject, but I can talk about the approach I followed.

Basically, I made event states using self variables and self switches and event spawners to have them as event templates, so I only code once and reuse a lot.

I have first page of the event be an autorun, I set up basic things like enemyId, enemyData, enemyActions, enemyLineOfSightRange, etc. after it finishes, I specifically make it so it goes to page one setting enemyActions variable so it triggers that page conditions.

Then each page is basically one of the actions, let's say as follows:
1 - Idle/patroll (parallel process)
2 - chase (event touch)
3 - Malee attack (parallel process)
4 - Long range attack (parallel process)
5 - Stun (parallel process)
6 - Defend (parallel process)
7 - Wait (parallel process)
8 - Next Action (autorun)
9 - Death/Inactive (default)

In your case maybe most of this is unnecesary, but the point is that each action has it's own behaviour coded in the event, and since it's only one event that can be multispawned, I code once and use a lot of times.

The guy in the video uses arrays and common events to do the same thing that I do...I just simply changed the arrays to self variables, but the end result is basically the same. If I remember correctly, there is a demo project you can download and analyze or copy things from it, but I would suggest that you at least try to understand the process behind it.

Sorry if my answer might not give you specific code or instructions, I'm kind of limited because of forum rules (in case someone wonders, the AI rules, no I didn't use AI to code, trust me that thing hates me, I'm already in their hit list 🤣)

Anyway, the video I mention is the following:

Original thread: https://forums.rpgmakerweb.com/threads/making-an-detect-chase-battle-corpse-enemy-event.182509/