Movement route script problem-pls help!

Hi,

I have an RPG Maker MV game with an event "Maid" character who at a certain time walks toward the exit and fades out (leaves). For some reason, the event moves to the exit properly, but the switch won't activate. I have the exact same code on another event and it works properly. Any help would be greatly appreciated!

At the designated time I activate the switch which turns on the event page for her to leave. In that event page's movement route I have the following code:

Event Pager 14 - Movement Route
Script: //travel to Exit
Script: if ($gameMap.event(this._eventId).regionId() === 8 || $gameMap.event(this._eventId).regionId() === 24 || $gameMap.event(this._eventId).regionId() === 9 ) { this.moveStraight(this.findDirectionTo(14, 20)) };
Script: if ($gameMap.event(this._eventId).regionId() === 13 ) { this.moveStraight(this.findDirectionTo(20, 16)) };
Script: if ($gameMap.event(this._eventId).regionId() === 14 ) { this.moveStraight(this.findDirectionTo(20, 22)) };
Script: //if at exit, fade
Script: if (this.pos(20, 22)) {$gameSwitches.setValue(1043, true)};

Event Page 15 - Movement Route
Switch 1043 required for page to run
Opacity: 200
Wait: 15 frames
Opacity: 150
Wait: 15 frames
Opacity: 100
Wait: 15 frames
Opacity: 50
Wait: 15 frames
Through On
Transparent On

Both movement routes are: "repeat movements" = true, "skip if cannot move" = false. Both pages are parrallel trigger.
could you try replacing your last if condition with this:
JavaScript:
if ($gameMap.event(this._eventId).x==20 && $gameMap.event(this._eventId).y==22){
$gameSwitches.setValue(1043, true);
};
I replaced it with your suggestion, but still same issue. 🙁
Are you sure that the switch doesn't turn on? Did you check the debug menu? Or maybe the coordinates are wrong. Also, could you post a screenshot of the event in the editor if possible?
Thanks for your help....debug is showing switch 1043 as off. Please see the screenshots below:
I think I see the problem now. It might be because you're using the script from the autonomous movement route. Try pasting the script call to the event contents. And just a note, parallel event doesn't affect the autonomous movement route. It affects whatever's inside the event, inside the box in the right side.
Still same issue :/

Its weird, because I used basically the same code and process as eariler on two earlier pages (13 and 14) and it works fine, but these two don't.
I'm out of suggestions XD Hopefully someone else will drop by to give you some pointers.
Thanks so much for your help!
there are two possibilities here: either the switch is turned but nothing reacts to it, or the switch is not turned because the condition (position) was wrong.

you need to find out which one of those is the case, then we can help - and in both cases it would help to see screenshots of the event instead of descriptions. I can't count the number of cases where the error was in the screenshots and those were different from what was described etc.
If you turn that switch on (e.g. via debug or a Control Switches command) does it stay switched on? I wonder if there's some event using that switch for something else.

The scripting looks OK to me, though I think it could be written more concisely, e.g.
JavaScript:
var r = this.regionId();
if ([8,9,24].indexOf(r) >= 0) this.moveStraight(this.findDirectionTo(14, 20));
else if (13 === r) this.moveStraight(20, 16);
else if (14 === r) this.moveStraight(20, 22);
else if (this.pos(20, 22)) $gameSwitches.setValue(1043, true);
Just erase the line-breaks and you can use it on a single line in a move route script element. Since move routes update by 1 command every frame the character is not moving, this should also eliminate any brief pauses between moves.
This really helps Caethryl, thank you! I'll try it today after work.

If you turn that switch on (e.g. via debug or a Control Switches command) does it stay switched on? I wonder if there's some event using that switch for something else.

The scripting looks OK to me, though I think it could be written more concisely, e.g.
JavaScript:
var r = this.regionId();
if ([8,9,24].indexOf(r) >= 0) this.moveStraight(this.findDirectionTo(14, 20));
else if (13 === r) this.moveStraight(20, 16);
else if (14 === r) this.moveStraight(20, 22);
else if (this.pos(20, 22)) $gameSwitches.setValue(1043, true);
Just erase the line-breaks and you can use it on a single line in a move route script element. Since move routes update by 1 command every frame the character is not moving, this should also eliminate any brief pauses between moves.
ps
I had really high hopes for this...Unfortunately, it didn't work for me. I made a new event to test it, and put a script call in the movement route like this (all in one line):

var r = this.regionId(); if ([8,9,24].indexOf(r) >= 0) this.moveStraight(this.findDirectionTo(14, 20)); else if (13 === r) this.moveStraight(20, 16); else if (14 === r) this.moveStraight(20, 22); else if (this.pos(20, 22)) $gameSwitches.setValue(1043, true);

but the event won't even show up.

In the Debugger console I get no errors...

On a side note, I also tried creating a function in visual studio using the same code called funMoveToExit() to see if I could just call several lines of code from a custom plugin, but of course that didn't work either. I guess if you call a function from javascript "this" object isn't the same as if you are scripting into the event itself.

I realize everyone is just offering help out of kindness, so really appreciate you all taking the time out to help me!
Screenshot 2020-10-21 220452.png

image_2020-10-21_220727.png

If you turn that switch on (e.g. via debug or a Control Switches command) does it stay switched on? I wonder if there's some event using that switch for something else.

The scripting looks OK to me, though I think it could be written more concisely, e.g.
JavaScript:
var r = this.regionId();
if ([8,9,24].indexOf(r) >= 0) this.moveStraight(this.findDirectionTo(14, 20));
else if (13 === r) this.moveStraight(20, 16);
else if (14 === r) this.moveStraight(20, 22);
else if (this.pos(20, 22)) $gameSwitches.setValue(1043, true);
Just erase the line-breaks and you can use it on a single line in a move route script element. Since move routes update by 1 command every frame the character is not moving, this should also eliminate any brief pauses between moves.
I had really high hopes for this...Unfortunately, it didn't work for me. I made a new event to test it, and put a script call in the movement route like this (all in one line):

var r = this.regionId(); if ([8,9,24].indexOf(r) >= 0) this.moveStraight(this.findDirectionTo(14, 20)); else if (13 === r) this.moveStraight(20, 16); else if (14 === r) this.moveStraight(20, 22); else if (this.pos(20, 22)) $gameSwitches.setValue(1043, true);

but the event won't even show up.
As I said, your move script looked OK in terms of reaching the destination and turning on the switch; my version should simply eliminate any 1~5 frame pauses between moves. 😴

If the event doesn't show up, I imagine you're testing from a save, rather than from new game. If so: test from a new game! Temporarily change the player starting position if necessary. Save data includes (amongst other things) all events on the map at the time of saving, along with their current move routes etc. This can include events you have removed or replaced since making the save file. 👋

If testing from a new game doesn't immediately fix the problem...did you try this, and if so what was the result?
If you turn that switch on (e.g. via debug or a Control Switches command) does it stay switched on? I wonder if there's some event using that switch for something else.
Sorry for the delay! So here's what I found:
Number 1: when I turn the switch (1043) on manually with the debugger it turns off so I think that you are right, there must be some other event turning it off, I will dig through and find that.

Number 2: I made a new, clean event with the no eventing/scripting in it except a custom movement route (the one line version that you posted) and started a new game. The event immediately disappears when the game launches.
I wonder if one of my plugins is messing this up...
Screenshot 2020-10-24 110417.png
Screenshot 2020-10-24 110451.png
Found the switch turning off problem (Number 1 above), thanks for that!

Also found out why the event was disappearing but still having the problem of the script in the movement route immediately makes my event disappear. The script I was using had this: "this.moveStraight(20, 22)" when it should have been "this.moveStraight(this.findDirectionTo(20, 22))"

However, still having problem with this part "else if (this.pos(20, 22)) $gameSwitches.setValue(1043, true);" However, when I turn the switch on manually it stays on.

Here's my new code that works, except for the switch turning on.

"var r = this.regionId(); if ([8,9,24].indexOf(r) >= 0) this.moveStraight(this.findDirectionTo(14, 20)); else if (13 === r) this.moveStraight(this.findDirectionTo(20, 16)); else if (14 === r) this.moveStraight(this.findDirectionTo(20, 22)); else if (this.pos(20, 22)) { $gameSwitches.setValue(1043, true) };"

To reiterate, turning the switch on manually works now, and the event moves to the location properly now, but the event doesn't seem to recognize that the event is in the correct location (20,22) nor turn on the switch (1043) when it gets there.
Figured it out, had to move the "if (this.pos(20, 22))" statement to before the "this.findDirectionTo(20, 22)" statement.

Thank you so much for your help @caethyril , @Andar and @Hyouryuu-Na !

Doesn't Work:
"var r = this.regionId(); if ([8,9,24].indexOf(r) >= 0) this.moveStraight(this.findDirectionTo(14, 20)); else if (13 === r) this.moveStraight(this.findDirectionTo(20, 16)); else if (14 === r) this.moveStraight(this.findDirectionTo(20, 22)); else if (this.pos(20, 22)) { $gameSwitches.setValue(1043, true) };"

Works:
var r = this.regionId(); if ([8,9,24].indexOf(r) >= 0) this.moveStraight(this.findDirectionTo(14, 20)); else if (this.pos(20, 22)) $gameSwitches.setValue(1043, true); else if (13 === r) this.moveStraight(this.findDirectionTo(20, 16)); else if (14 === r) this.moveStraight(this.findDirectionTo(20, 22)); else if (this.pos(20, 22)) $gameSwitches.setValue(1043, true);

Original thread: https://forums.rpgmakerweb.com/threads/movement-route-script-problem-pls-help.128919/