Visual Encounter Help

Hello everyone hope you are all having a good day.

So for my game i really want to have visual encounters, a faceless sprite just resting somewhere on the map (maps as in routes like pokemon) and if you enter a specific area it will start to target you and chase you and when it touches you it starts a battle.
after the battle if you win id just want the sprite to die and preferably for it to respawn after a set amount of time, but im okay with it respawning when you re enter the area again. i also dont mind it chasing the player through the entire map however a way to block the enemy from going any further would be nice. i also want the enemy to return to its waiting state when you re-enter an area even if when you left it was still chasing. When you run away from the enemy id want the enemy to take a step back and wait for a couple frames before continueing its chase only giving up once you leave the area.

Ive already tried multiple things so far the one i like most but is very flawed is a script made by someone i know.
1723986793413.png
1723986823346.png

the issues i have with it is that the respawning completely doesnt work, the enemy will aggro you instantly when it respawns and theres no way of it just returning to its waiting state. once it sees you it will be permanently stuck in its aggro phase. and i feel like the detection system could definetly be optimised too.

The second one is one i found from a thread on here but it doesnt really work very well either, it was one for mv so maybe that is why
1723987004489.png

1723987058198.png

the main issue here is that it only moves when you are in its area, and then it just starts moving at random leading to it moving out of its own area, where it just becomes a wandering sprite, you can bump into it and nothing happens. and i feel like i want it to chase you further than just the trigger area, and without the random movement it will just sit on the edge of the trigger area waiting for you to get in wich looks incredibly unrealistic. not to mention the extremely buggy hitboxes where i often just walk through the enemy without anything happening or a delayed battle. it usually keeps the hitbox on the tile where the enemy last was wich just feels really unresponsive. i did try to add a bit of code myself trying to fix the whole static sprite thing wandering around but it still looks very buggy. not to mention that im pretty sure you have to create 3 variables for every single enemy you wanna make wich is not something im excited for

if anyone could help me improve one of these two events id REALLY apriciate it as im kind of at a loss since theres barely any visual encounter help i can find online.

thank you in advance.
The second event uses all event commands, no code, so I don't see what difference MV vs. MZ would make. But this is an awkward thing to make with events.

Have you Googled for plugins? There's stuff out there designed to do this. This thread has two:

A few more options here:

There may be more, I only clicked on the first two Google results.
Okay, I think I've got it working.
No plugins.
No switches.
No variables.

All of this can be easily changed to suit your needs, and I tried to match what you were going for in the first two examples given. So here's what I've got:

1) Enemy walks around randomly until you get close (within ~3 tiles away), then starts moving towards you.

2) If you run away from it before it catches you, it will continue to follow unless you get 6+ tiles away which causes it to "lose" you.

3) If it touches you, battle starts.
If you fight and win, enemy disappears for 600 frames, then respawns.
If you escape, it takes a step backwards (should always be away from the player), gives you ~2 seconds of grace period, then begins chasing again.
4) If you leave the map and then return/reload the map while the enemy is waking around normally/randomly, it will have moved back to its original location.
If you leave the map while the enemy is still dead, it will still be 'dead' when you return, but its revival timer will start from zero and revive in its original spawn point after 600 frames have passed on the map.
If you leave the map and return while an enemy is in its 2-second grace period, it will start a fresh 2-second grace period upon returning before it starts moving around randomly or attempting to detect you.



Here's how I set this up and it should work similarly to everything you posted earlier.
There is one map (map #139) and two events (events002 and 003) in this example.
Event 002 is the enemy, "FACELESS SPRITE" with an rtp ghost icon.
Event 003 is an invisible parallel event that controls the movement of all of the enemies on the map.

Spoiler: Screenshots of these two events
Tab #1 of the enemy, Event 002, named FACELESS SPRITE. It has three tabs but can be relatively easily copy/pasted to make other enemies on the same or different maps that function the same way. Its movement should probably be changed to a "custom" movement route that is pseudo-random that you make yourself based on the map it is in and/or however you feel the npc should move around, but this is optional at this point.

faceless1.png




Tab #2 has the battle data and anything you want to do like adding emotes when things happen. My game currently doesn't support the emote animations (it crashes my client atm) so that is why it isn't listed there. Just add it in the way you had it in your own eventing. Looks like I have it wait 120 frames for the grace period after taking a step backwards. This tab can optionally have different graphic for the enemy sprite (if you so desire) that should only play while you are in a grace period. Otherwise just use the same graphic you have in tab #1.
faceless2.png





Tab #3 is what happens when the enemy is dead/defeated. Enemy sprite is blank because I'm assuming it doesn't need to disappear/die once in battle and then again on the overworld. It is triggered after you "win" the battle.
If you want it to wait longer before respawning, like 2500 frames instead of just 600, just add additional waits as needed. If you want it to play an animation before respawning, just trigger an animation on top of this event in tab 3, before the self switches are turned off. If you want the enemy to die permanently, you can trigger a blank 4th tab with yet another self switch that will persist even after reloading the map or whatever.
The way I have it set up, it will revive on the same tile it was on when it died in battle unless you reload the map. If you want it to revive only at its origin point, you can do this with just one extra command and that is in tab #3 just add a SET EVENT LOCATION for the faceless sprite somewhere after the wait 600 frames and pick the exact spot you want it to return to on the map. It will then revive in that spot instead of where it died.

faceless3.png






Finally, this other event is an invisible parallel event that controls all of the enemies on the map.
If you have multiple enemies, copy and paste the whole block and change the values appropriately.
faceless4.png

Here's the three scripts I put in conditional branches that are cut off in the above image:
1) $gameSelfSwitches.value([139,2,'A']) == false
2) Math.abs($gamePlayer.x - $gameMap.event(2).x) <= 3 && Math.abs($gamePlayer.y - $gameMap.event(2).y) <= 3
3) Math.abs($gamePlayer.x - $gameMap.event(2).x) >= 6 || Math.abs($gamePlayer.y - $gameMap.event(2).y) <= 6
So if your map was Map009 and your faceless enemy sprite is event #14 in your own map, you would change the [139,2,'A'] to [9,14,'A'], and then change the four instances of event(2) to event(14). If you forget even one of these I think the entire thing will just not work. This would be the only tedious part of the process, since everything else is a functional copy/paste with basically no further edits needed to get them to work.

In case it isn't clear how this works, the "3" is how many squares away you have to be to aggro the enemy, and the "6" is how many squares away you have to run to lose that aggro. Naturally you can change these to do whatever you like, or delete/change anything as you see fit.

The movement routes can be made to be whatever you desire. If you don't like the 'step backward' command you can use something else to simulate the same thing. If you made a custom movement route for fake randomness in the enemy npc's first tab, you can copy/paste that same custom movement route instead of using the default "random" movement command I'm issuing here, giving you more control over how these npcs wander or pace back and forth or whatever you want them to do when they aren't chasing the player.

Part of the reason I've set this up this way is to minimize the number of conditional branches needed to check for various things, including what state the npc is in or whatever. For example, if the npc is just getting out of battle with you or the player has their grace period, the npc won't change its movement, which is why we are checking $gameSelfSwitches for being on or off.

To make additional enemies you can copy/paste the enemy event, and copy/paste the conditional script as an additional line inside of the same parallel event with every instance of event(2) changed to match the new enemy's event#. There should be no hard limit to how many enemies you can have moving around the map (or chasing the player...) simultaneously this way.

Finally, I'm also like 100% sure there's an easier/more efficient way to set up everything I've just done but I'd like you to have a method that works just to get started, since I'm sure you deserve it.




EDIT: Already found at least two minor but fixable problems with what I've posted, but I need to sleep so I'll take a look at it again tomorrow.
One is that I should have given an example of a custom movement route instead of just suggesting it.
The other is I should have tested a bunch of different aggro and stop-chase ranges. If you have a wide middle ground, it's possible to enter battle, escape from battle, and then have the npc just stare you in the face until you get closer or get farther away. What it should do is probably aggro immediately after the grace period is over unless you're already far enough away to have lost its attention. For the record this is a very minor unintentional effect because you have to have adjusted distances to allow it but nevertheless I'll fix it tomorrow.
@redtide89 Thank you so much for all the help and everything! this does seem like it would be perfect and exactly what im looking for.

Ive already copied all the code you showed but i changed the map to 13 and the event to 27. im not sure if its what you mean with custom routes but currently the enemy doesnt aggro at all, its walked right up against my player character but no battle starts it just walks around, as if the player character was a rock. i kept the aggro range of 3 and 6 the same, i even made the spawning location of the enemy 3 tiles away from the player character but even then nothing gets triggered, or at least ive got no real way to see if it triggered, i tried messing around a bit with the emoticons and with some enemy speeds but when i added a tab that would increase speed on the second if in the parallel event and one that would decrease it in the third however that had the opposite effect of what i wanted, and when i tried to invert it the enemy still was slow when you were close to it but suddenly obtained super speed when you left its range. Im a little out of my element since its also dealing with actual scripting and coding... ive read over your reply multiple times already but maybe i still just missed something. I'll await your reply and the things you mentioned in your edit and then try again, maybe ill try to fix it some more on my own but i feel like if i did i wouldnt really be knowing what im changing. i also tried to increase the aggro range and run away range but that didnt fix anything either.

Again thank you so so much for all the work youre putting in for me i appreciate it alot.
Just as an addendum to @redtide89's solution - you should almost never have parallel events running without a Wait time, unless they specifically require it. And player movement does not specifically require it.

There should be at least a Wait 6 at the bottom of that common parallel event to prevent it from running every frame unnecessarily.

The other thing to mention kind of explicitly, just because I don't think it was in that post - this is a solution for your one ghost as a specific event. If this is a general behavior that you want, enemies chasing players when they get close, doing it this way would get very cumbersome because you'd have to include checks for every monster on your map that behaves that way.

(unless you abstract it out to looking for, say, a notetag on which events are enemies, in which case you might save yourself all of the trouble by using an existing plugin)
@ATT_Turan I have tried looking for plugins but i cannot find a free one that does exactly what i need to do... if this works and i can apply it to every enemy i add even if that means i have to add an event like this to every single map with battles then thats fine... unless you can present me with a plugin thats free and does exactly this, this is definetly the best option ive seen

thank you for the wait thing tho that might decrease any future lag problems
...I provided you with several links already...
...I provided you with several links already...
i have looked into both of those posts several times but i really couldnt figure out wich plugins you meant... i mean i found those lines of code but i have no idea how to turn those into plugins, the only way i know how to use plugins is just by downloading them, putting them in my folder and then adding them in the plugin manager

im sorry if im not very experienced with plugins, or anything besides just making basic events and such, i havent really done anything with plugins yet besides just some basic stuff like adding a healthbar, removing the escape and item tabs for battle and making the menu look a bit prettier all of wich were just on and off switches.
Both of the threads were literally about the same thing you're asking about. All plugins mentioned in them are relevant.

From the first link, post 5:

turns on a self-switch when a player is nearby. From there, it's regular event setup. Make the page with the self-switch have a custom move route to approach the player, event touch trigger, battle processing for the event command.
That does sound like something i could really use if it also allows for the running away thing, although i have no idea how to add that code to my game...

EDIT: ive added it to my game but i cannot read any japanese so now im a little stuck again (google translate didnt help either)
Just as an addendum to @redtide89's solution - you should almost never have parallel events running without a Wait time, unless they specifically require it. And player movement does not specifically require it.

There should be at least a Wait 6 at the bottom of that common parallel event to prevent it from running every frame unnecessarily.
This is correct. Embarrassing, but there was a Wait 10 at the top of the event before I tried to remake it the whole thing in MZ (the prototype I threw together was in MV). This type of event does not need to run on every frame.




Anyway, here is a, uh, demo project that shows the stuff in my previous post working. Well, "working" as much as it was intended to:
https://www.dropbox.com/scl/fi/gmre...ey=6qyfb7ae1obq1ehn2xxxphbj9&st=v6upciff&dl=0
It is two maps with four enemies.
It uses four variables and one common event.
Check the common event if you want to change the aggro/chase ranges or the grace period duration.
It does not need any more variables to add any more maps or enemies to the game.
All of the scripting is put into a single invisible event on the maps with enemies.
It's janky but it works on my end, and if it helps anyone then it's worth the (tiny) bit of time it took to throw it together.



As for the AutoSelfSwitch plugin, parts of it are in english so you can probably still understand it and potentially use it.
Spoiler: AutoSelfSwitch basics
First we need the plugin, and we also need the PluginCommonBase.js that comes in the dlc folder. So you need to make sure you grab that one, put it into your js/plugins folder, and add it to work. You can read all of that in the description with google translate if you like. Anyway, so we activate both of those plugins (PluginCommonBase.js and AutoSelfSwitch.js).


When we mess with the parameters, this box shows up:
0uQnshI.png




If we try to interact with that empty list of lists, then we get this to show up:
bxIYOkA.png

The seven things here might show up in english for you depending on a version you are using. Don't worry if it's all in japanese like the above picture. The seven parameters and their default settings are as follows:

memo = "selfSwitch"
distance = 0
condition = 0
self switch type = A
turn off = false
inversion = false
enable switch = 0





To help you understand how to use it, let's change three things.
1) we change the memo to "chasing".
2) we change the distance to 7.
3) we change the "turn off" to true.
Now hit Okay/Apply/whatever to get out of there.

We make an event called Ghost.
We give it the <chasing> note tag in its Notes box (at the top next to the event's Name, with the brackets).
Tab 1 has the ghost's image and basically everything else is default.
Tab 2 also has the same image, but requires self switch A to be activated. We change this ghost's movement to "Approach".

When you playtest the game, what should happen is the ghost stands still normally, but when you get close it should take small steps towards you. If you get far enough away, it will stop moving towards you.

The important thing to recognize here is that although this is very crude and not suitable for your game by itself, it lets you manipulate things that you already understand (like basic eventing), but gives you another way to control when self switches turn on or off, based on how close the event is to the player. With more effort and some clever thinking, you can probably come up with nice ways to make your game do what you are wanting it to.

And if I've done an awful job at explaining how to do any of this, @ATT_Turan can probably give better insight into it.
@redtide89 thank you so much for all the help! ill see what i can do with the project and with the plugin, I will try to tweak them to fit my game, ill try to do it on my own but thank you so much for your patience and time i apreciate it alot!

Original thread: https://forums.rpgmakerweb.com/threads/visual-encounter-help.171190/