Weird Interaction in Event Stop Follow Player - JS

Hi. I'm trying to control the "enemies" events to follow the player under specifics circunstances using js.
This function is called in the updateScene function from the Scene_Map and the events have their autonomous movement "Fixed".

I use the _chasePlayer to set the events to follow the player, and the results end up working.
But when I set the events to stop following the player (in the updateScene too), the events returns to the place where they had started, and I dont want that.
I tried different strategies to stop that, but doesnt work. Even setting the _moveSpeed to 0 doesnt prevent that from happening.

If anyone knows wth is happening, please, help 😀
Please post the code that you're using, and screenshots of any event code as well.
Your vague descriptions are not enough to understand what is happening or to help you.
Ok. I tried to replicate the results that were occuring in my real project, but didnt work (for some unknown reason). But I'll assume this error is the raw one and the cause of my problems.

Suppose I have a event (or events) like this one, with a notetag like "follow_player".

a6a0f458-425f-434e-8edb-075253ed7ccf
YX6LDdF.png


I created a simple script here to illustrate what I wanted, so you can understand the expected results I was hoping to happening.

RtmibIE.png


I want the event with the follow_player tag to follow the player if the switch 1 is active. In my previous attempts, the event was following the player correctly, but when the gameSwitch 1 turn off, it stop following the player and starts to move back to the original position it was placed in the map.

On the other hand, this simple piece of the code isnt even working at all. The event is still idle, even with the switch on. What can I do to modify the movement and the type of movement of events via script?

If you can solve or help with one of these two situations (probably this second one that I examplify with some code), I will be grateful 😀

[EDIT]
Oh, it seems I forgot to put some more code in the script.

Spoiler
Test_Game_Event_updateSelfMovement = Game_Event.prototype.updateSelfMovement;

Game_Event.prototype.updateSelfMovement = function (){

if (this._stopCount > 0 && this._chasePlayer){
var direction = this.findDirectionTo($gamePlayer.x, $gamePlayer.y);
if (direction > 0) this.moveStraight(direction);
}
else
Test_Game_Event_updateSelfMovement.call(this);
}

This piece of code fixed my problems! In fact, the other weird situation that was hapenning was due to the findDirectionTo arguments that I was using. I was not passing the gamePlayer position as the arguments, but a variable referencing the previous position of player. When the switch turned off, the reference variable was resetting, making the event follow to the (0,0) position.
Sorry by the confusion here 😅 The problem is solved.
Can we see the code you wrote for handling the _chasePlayer flag?
I think you misunderstand several things.

By default, the engine does not remember any event position, so all events revert back to original position on map reloading. That is the only possible reason for the event to go back to its original position like you described, and that had nothing to do with your code for following.
The only way to prevent that is to use a plugin that updates event positions through map reloads - there are several of them already existing, for example Shaz' "remember event position" (might be named a bit different)

That said, why use javascript at all? That (minus the remember position) is one of the most basic functions supported by the default engine.

Simply make a two-page event, page one without condition and autonomous movement = fixed, page two with condition switch X = on, autonomous moment = approach player.
Finished - that event will try to follow the player if the switch is On and stop when it is off

If you want to include a remember position command, you need the plugin installed and use autonomous movement = custom on the second page, with the custom being one approach player command followed by the script command to update position.
Sorry for the confusion. I edited my last reply. But thanks anyway for the attention.

And thanks for the exemplification too, Andar. I'm not using the followPlayer via events cause I want the conditions to the followPlayer be more specific, like when pressing some key instead of some switch is turned on.

And about the Shaz script, I will take a look. It will probably help me too 😀
Glad you got it sorted, but you do realise Game_Event can already call a function called moveTowardPlayer that negates the need to use findDirectionTo, right?
Oh, yes. I didnt used because I wanted the events to go to the position the player was in the moment the switch turned on. Just to facilitate I omitted this information.

Original thread: https://forums.rpgmakerweb.com/threads/weird-interaction-in-event-stop-follow-player-js.75251/