Custom route move hitch on changing maps

I had set up the enemies so that they would chase you if you came within 3 tiles of them. This worked fine when I entered the map.

However, if I dodged any to move on to another map and then came back, the enemies no longer moved, no matter how close I got to them.

I tried this with 2 methods.
The first used YEP_EventChasePlayer with FOSSIL which uses script calls within the Route of a Custom Autonomous Movement. Fine until I changed maps.

I then used the following script call. Bear in mind that it has to be all one line as that is the only thing the script call box in Route Move will accept.

Code:
var x=$gamePlayer._x; var y=$gamePlayer._y; var idx=$gameMap.event(this._eventId).x; var idy=$gameMap.event(this._eventId).y; if((x>=idx-3)&&(x<=idx+3)){if((y>=idy-3)&&(y<=idy+3)){$gameMap.event(this._eventId).moveTowardPlayer()}else{$gameMap.event(this._eventId).setDirection(2)}}else{$gameMap.event(this._eventId).setDirection(2)}
Again, runs fine unless I change map and then come back.

The only difference in the map between the first & second visit is that there is a bit of dialogue on autorun the first time.
If I reverse the process and start on the second map, the same thing happens when I return to it. It therefore does not seem to be a consequence of anything obvious on the map itself, just something about changing maps.

I am at a loss and would appreciate any insight that could be given.
Thank you.

EDIT
Further testing:
Put the enemies onto the normal 'Approach' autonomous move. Dodge them to get to another map. Return - and they do not move, just as in the other tests. It is beyond bizarre if even Approach doesn't solve my problem.
I'm honestly not sure about the different behaviour when leaving and returning to the map, but a move route runs in the context of the event, not the interpreter, so this is the event itself. So your long script call

Code:
var x=$gamePlayer._x; var y=$gamePlayer._y; var idx=$gameMap.event(this._eventId).x; var idy=$gameMap.event(this._eventId).y; if((x>=idx-3)&&(x<=idx+3)){if((y>=idy-3)&&(y<=idy+3)){$gameMap.event(this._eventId).moveTowardPlayer()}else{$gameMap.event(this._eventId).setDirection(2)}}else{$gameMap.event(this._eventId).setDirection(2)}

can be shortened to this:
Code:
var x=$gamePlayer._x; var y=$gamePlayer._y; var idx=this.x; var idy=this.y; if((x>=idx-3)&&(x<=idx+3)){if((y>=idy-3)&&(y<=idy+3)){this.moveTowardPlayer()}else{this.setDirection(2)}}else{this.setDirection(2)}

and that can be further shortened to this:
Code:
var x=$gamePlayer._x; var y=$gamePlayer._y; var idx=this.x; var idy=this.y; if((x>=idx-3)&&(x<=idx+3)&&(y>=idy-3)&&(y<=idy+3)){this.moveTowardPlayer()}else{this.setDirection(2)}

and now I'm just playing
Code:
var p=$gamePlayer; if(Math.abs(this.deltaXFrom(p.x)) <= 3 && Math.abs(this.deltaYFrom(p.y)) <= 3){this.moveTowardPlayer()}else{this.setDirection(2)}

But I don't think any of that would have anything to do with it working first time you're on the map and not when you return.

It sounds to me like it might be a plugin interfering. Can you boot up a new project with 2 maps and put one of these events and nothing else (except the transfer between maps) - does it work there? Then add the autorun that plays the dialogue first time around - does it still work? Do the events only have one page? Any chance a different page without these commands is being activated (or a different page with these commands but wrong event trigger)?

And this is a pretty dumb suggestion, but you ARE actually coming back to the same map, right? You don't have two near-duplicate maps where you haven't added the logic to the events on the second map, and are actually "returning" to that one?

Feel free to zip it up and send to me if you can't figure it out.
@Shaz I cannot claim to understand how your voodoo incantations work their magic, but when I put your shorter version into the enemies' route moves, everything works smoothly. I can go in and out of the 2 maps and each time the enemies can move, but don't do so until I'm the correct number of tiles away.

My relief is enormous, and so are my thanks for this. You have saved me - and not for the first time either.
I cannot claim to understand how your voodoo incantations work their magic
Sometimes, either can I 😉


I really don't know why that made such a big difference. I get that the different scope (event vs interpreter) might mean some things are defined correctly where they weren't before, but to me, that would mean it should not have worked at all before - even without leaving the map.

I'm glad that solved it 🙂


btw - your longer version script call
Code:
var idx=$gameMap.event(this._eventId).x
would be used if you were using a Script event command (just not within a move route) and my shorter version would not work because those functions are in the event/character scope, not the interpreter scope.

Thread closed

Original thread: https://forums.rpgmakerweb.com/threads/custom-route-move-hitch-on-changing-maps.155919/