MZ Set Movement Route Script on JavaScript

I'm using Visustella's movement core and using its 8 direction movement. So far it's working. However, events only move in 8 direction when using the Move To script in Set Movement Route. Events wont move in 8 direction when using the moveStraight() function in JavaScript.

The path finding all works, nothing wrong there. I just want my events to walk in 8 direction.

Is there a way to straight up use the Move To: X, Y script of Set Movement Route in Javascript? Thanks!
Moved to: Javascript/Plugin Support

If you want to move diagonally, you have to use the moveDiagonally(x,y) function, not the moveStraight() function.
From the plugin's documentation (emphasis mine):
Move To: x, y
- Replace 'x' and 'y' with the map coordinates to move the unit to through
pathfinding.
- This uses RPG Maker MZ's pathfinding algorithm. It is not perfect so do
not expect the most optimal results.
- Events will go around the player.
The default pathfinding algorithm only checks cardinal directions (up, down, left, right). I think you'll need something that overrides the default algorithm (used for touch-based movement) with an 8-directional pathfinder. I.e. an override for Game_Character.prototype.findDirectionTo that checks 8 adjacent tiles rather than just 4.
Moved to: Javascript/Plugin Support

If you want to move diagonally, you have to use the moveDiagonally(x,y) function, not the moveStraight() function.
Thanks! Unfortunately, this didn't work for me.

From the plugin's documentation (emphasis mine):
​
The default pathfinding algorithm only checks cardinal directions (up, down, left, right). I think you'll need something that overrides the default algorithm (used for touch-based movement) with an 8-directional pathfinder. I.e. an override for Game_Character.prototype.findDirectionTo that checks 8 adjacent tiles rather than just 4.
Thanks! However, Visustella's 'Move To' script in Set Movement Route makes my event move in 8 directions. That's the reason why I'm trying to find how to code 'Set Movement Route' Script in JavaScript.


Screenshot 2022-04-27 173601.jpg

Is there a way to code this in JavaScript?
Is there a way to code this in JavaScript?
Shaz already gave you the javascript command
you have to use the moveDiagonally(x,y) function

if that doesn't work for you, then you should try to find out why it didn't work and what you did wrong, starting with a screenshot of how you used the function
Thanks! However, Visustella's 'Move To' script in Set Movement Route makes my event move in 8 directions. That's the reason why I'm trying to find how to code 'Set Movement Route' Script in JavaScript.
Sorry, my mistake! I misread your original post. 😴

Yea, like Shaz said, use moveDiagonally(x, y) for diagonal movement. The inputs should be numpad directions, maybe that's where you went wrong. E.g. the "Move Upper Left" move route command translates to this.moveDiagonally(4, 8).

(I'm not sure why you're asking for a script for this, though...guess I'm still missing something.)
Sorry, my mistake! I misread your original post. 😴

Yea, like Shaz said, use moveDiagonally(x, y) for diagonal movement. The inputs should be numpad directions, maybe that's where you went wrong. E.g. the "Move Upper Left" move route command translates to this.moveDiagonally(4, 8).

(I'm not sure why you're asking for a script for this, though...guess I'm still missing something.)
Thanks! I'll try this again.
If you are putting this as a Script command in a move route, make sure you put this. at the start:

this.moveDiagonally(4,8)

to move diagonally up and to the left.

Though if you're putting it into a move route, why not just use the Move Upper Left command?
If you are putting this as a Script command in a move route, make sure you put this. at the start:

this.moveDiagonally(4,8)

to move diagonally up and to the left.

Though if you're putting it into a move route, why not just use the Move Upper Left command?
Thanks! This is working. However, how can I implement a pathfinder using this function?

I'm using this javascript code in order to let my event walk in the specified location (visustella's 8 direction movement won't work on this).

JavaScript:
var charEvent = $gameMap._events[eventID];
charEvent.moveStraight( charEvent.findDirectionTo(destinationX, destinationY) );

The reason I want my events to move in 8 direction is because I have a map that has slant roads.

wdad.jpg


As far as I know, the function findDirectionTo() is a 4 direction pathfinder and only return a single number. That's probably why it didn't work with moveDiagonally().
You're right - that function only moves in 4 directions. To make it pathfind and use directional movement you're probably going to have to write a new function/plugin. But before you can do that, you need to define how you want it to work ... will it ALWAYS move diagonally until it reaches the same x or y value, and then it will move vertically or horizontally? Or should it do a fairly even mix of diagonal and vertical/horizontal movement?
You're right - that function only moves in 4 directions. To make it pathfind and use directional movement you're probably going to have to write a new function/plugin. But before you can do that, you need to define how you want it to work ... will it ALWAYS move diagonally until it reaches the same x or y value, and then it will move vertically or horizontally? Or should it do a fairly even mix of diagonal and vertical/horizontal movement?
Are there no existing functions or plugin for this? I think making a new function for this requires understanding of an algorithm. I peek at the findDirectionTo() function and didn't understand the math at all lol.

The reason why I choose RPG Maker is for me to make little programming and focus on the story and gameplay. Guess I need to learn some pathfinding.

Thanks for your help guys!
I'm not aware of any existing plugins that do pathfinding in 8 directions. Doesn't mean there isn't one though.
I'm not aware of any existing plugins that do pathfinding in 8 directions. Doesn't mean there isn't one though.
Last question though.

Screenshot 2022-04-27 173601.jpg


I think I finally got how to do this on javascript:
JavaScript:
var x = 51;
var y = 14;
       
var x2 = 62;
var y2 = 3;

var dir = "Move To: "+x+","+y;
$gameMap._events[eventID]._moveRoute.list[0] = {};
$gameMap._events[eventID]._moveRoute.list[0].parameters = [dir];
$gameMap._events[eventID]._moveRoute.list[0].code = 45;
$gameMap._events[eventID]._moveRoute.list[0].indent = null;

var dir2 = "Move To: "+x2+","+y2;
$gameMap._events[eventID]._moveRoute.list[1] = {};
$gameMap._events[eventID]._moveRoute.list[1].parameters = [dir2];
$gameMap._events[eventID]._moveRoute.list[1].code = 45;
$gameMap._events[eventID]._moveRoute.list[1].indent = null;

So, I messed with RPG Maker's console and was able to modify the event's Set Movement Route. All I need to do is add an array and code = 45; as the value 45 is Set Movement Route's Script Call.

Also, I need to set _moveRouteForcing = true in order for the event to move.

And by modifying the value of _moveRouteIndex = 0, I can redo the first movement of the event listed on the _moveRoute.list. The event that I worked on finally moved diagonally using Visustella's 8 direction movement with RPG Maker's pathfinder. Although if I'm going to implement this on my game, I need to rewrite my code from scratch.

My question is, is this method kinda messy and dangerous?
You're modifying the event's current move route, which might be null (error) or a reference to the map data (expected to be constant), rather than applying a new route.

Instead, you may want to try something like this in a Script command (untested):
Spoiler: Script call suggestion
JavaScript:
// Map event 17
const ev = $gameMap.event(17);
// Waypoints
const pts = [
  { x: 51, y: 14 },
  { x: 62, y:  3 }
];
// Convert points to "path to" move command scripts
const list = pts.map(
  p => ({
    code: Game_Character.ROUTE_SCRIPT,
    parameters: [`Move To: ${p.x}, ${p.y}`]
  })
);
// Apply the new move route
ev.forceMoveRoute({
  list: list,
  repeat: false,
  skippable: true
});

Incidentally, an 8-directional A* pathfinder should be just as simple as a 4-directional one: each step, check 8 adjacent tiles instead of 4. However, then it would probably be a good idea to make the moveStraight method redirect to moveDiagonally when appropriate, to avoid breaking existing code. There might be other stuff to watch out for, too, not sure. 😴
You're modifying the event's current move route, which might be null (error) or a reference to the map data (expected to be constant), rather than applying a new route.

Instead, you may want to try something like this in a Script command (untested):
Spoiler: Script call suggestion
JavaScript:
// Map event 17
const ev = $gameMap.event(17);
// Waypoints
const pts = [
  { x: 51, y: 14 },
  { x: 62, y:  3 }
];
// Convert points to "path to" move command scripts
const list = pts.map(
  p => ({
    code: Game_Character.ROUTE_SCRIPT,
    parameters: [`Move To: ${p.x}, ${p.y}`]
  })
);
// Apply the new move route
ev.forceMoveRoute({
  list: list,
  repeat: false,
  skippable: true
});

Incidentally, an 8-directional A* pathfinder should be just as simple as a 4-directional one: each step, check 8 adjacent tiles instead of 4. However, then it would probably be a good idea to make the moveStraight method redirect to moveDiagonally when appropriate, to avoid breaking existing code. There might be other stuff to watch out for, too, not sure. 😴
Thanks! Will try this later.
Fingers crossed!
Fingers crossed!
It's working! Thanks!

A question though. Do variables should be always constant? And if I want to change the route of an already walking event, should I just run this code again with different x and y points?
A question though. Do variables should be always constant?
I used const in my example because the temporary variables didn't need to change value. You could use var (or let) instead, it doesn't really matter much. Technical details here~
And if I want to change the route of an already walking event, should I just run this code again with different x and y points?
Yes. That will replace the current route with the new route, which is what you want in that situation.
I used const in my example because the temporary variables didn't need to change value. You could use var (or let) instead, it doesn't really matter much. Technical details here~

Yes. That will replace the current route with the new route, which is what you want in that situation.
Thanks! My code is working now!
I'm here again! Sorry for my several follow up questions, but I encountered I think is a major problem.

Even though the 'Move To: X, Y' javascript code is working, I tried to find a way to use what Shaz gave to me, the moveDiagonally() function.

Somehow, I come across Yami_8DirEx.js inside RPG Maker MZ's dlc/plugin folder. There I found a function called findDiagonalDirectionTo() which is findDirectionTo() but returns 8 direction.

I made a few tweaks to Yami_8DirEx.js file and managed to use both moveStraight() and moveDiagonally() and finally able to let my events walk in 8 directions without the Move To: script.

Now, the problem. It lags.

In 4 direction movement, I can make 20 walking events and have no problem. But using 8 direction movement on 10 walking events causes game stutter like every second. I don't use parallel process. My code is being run on map's update() function every 32 frames.

I did set my code to run every 45 frames. It removes the lag but my events are walking weirdly. I also test 10 simple events with the same movement route (pathfinding):

Screenshot 2022-05-02 134158.jpg

But it still lags. Are there any fix on this? I'm using i3 8100 and gtx 1060 btw.

Update: Weird. Seems like disabling visustella's movement core reduces lag. 12 moving events produces little to no lag.

Original thread: https://forums.rpgmakerweb.com/threads/set-movement-route-script-on-javascript.147121/