Pure javascript event movement control question

inqs

Villager
Member
Joined
Feb 28, 2019
Messages
15
Reaction score
1
First Language
English
Primarily Uses
RMMV
I would like to control the movement of an event purely through javascript. Using Yanfly's movement plugin (YEP 124) I have the script command this.moveToPoint(x, y) seems like what I want but I get odd results because I don't know how to pass control back to the game's scene manager.

For example, I tried to do something like the following code:

Code:
    for (;;) {  // I want event to move until it reaches destination
      if (myEvent.x === destX && myEvent.y === destY) {
        break  // exit loop since destination reached
      }
      myEvent.moveToPoint(destX, destY, true) // step toward destination
      $gameMap._interpreter.wait(20) // let display catch-up
    }
but instead of seeing the event move smoothly towards the destination it looks like it teleports most of the way there and walks the last few steps. I think this is because the RMMV scene manager needs to take control to update the event location but of course my loop has control.

I know I can achieve the desired effect using event pages, self switches, and custom move route but I would like to know how to do this in pure javascript. Do I need to setup some kind of asynchronous function or something else?

Thanks.
 

caethyril

^_^
Veteran
Joined
Feb 21, 2018
Messages
2,089
Reaction score
1,508
First Language
EN
Primarily Uses
RMMZ
Explanation
The interpreter handles every event command as a single entity. Script calls are passed whole into an eval, so the interpreter wait call does not get a chance to be applied until the for-loop is broken. Movements work by updating the character's integer tile coordinate to match the destination; the sprite position is then updated to reflect the appropriate position between tiles. This means you'll only see the sprite moving smoothly between tiles on the final step, when no other move commands are being prematurely applied.

Suggested Fix
Rather than try to work around the existing interpreter/move route system, it's probably easier to work with it. Try a script call along these lines:
Code:
var mx = 5, my = 8;  // target tile coordinates
var scr = 'if (!this.pos(' + mx + ',' + my + ')) this.moveToPoint(' + mx + ',' + my + '); else this._moveRoute.repeat = false;';
var lst = [{ code: Game_Character.ROUTE_SCRIPT, parameters: [scr] }, { code: Game_Character.ROUTE_END }];
var route = { list: lst, repeat: true, skippable: false, wait: false };
this.character(0).forceMoveRoute(route);
Basically, this gives the current event a repeating move route with a single line of script. The script checks whether it's at the destination: if not, it moves towards the destination, if it's there already then it tells the move route to stop repeating. The standard move route update stuff takes care of everything else~ :kaothx:
 

inqs

Villager
Member
Joined
Feb 28, 2019
Messages
15
Reaction score
1
First Language
English
Primarily Uses
RMMV
Thanks! That works perfectly.
 

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

Latest Threads

Latest Profile Posts

so hopefully tomorrow i get to go home from the hospital i've been here for 5 days already and it's driving me mad. I miss my family like crazy but at least I get to use my own toiletries and my own clothes. My mom is coming to visit soon i can't wait to see her cause i miss her the most. :kaojoy:
Couple hours of work. Might use in my game as a secret find or something. Not sure. Fancy though no? :D
Holy stink, where have I been? Well, I started my temporary job this week. So less time to spend on game design... :(
Cartoonier cloud cover that better fits the art style, as well as (slightly) improved blending/fading... fading clouds when there are larger patterns is still somewhat abrupt for some reason.
Do you Find Tilesetting or Looking for Tilesets/Plugins more fun? Personally I like making my tileset for my Game (Cretaceous Park TM) xD

Forum statistics

Threads
105,868
Messages
1,017,070
Members
137,577
Latest member
SadaSoda
Top