Creating a Move Route with a Script command

These are the commands you need to use if you want to create a move route and apply it to one or move events, by using a Script call in an event list.  You might want to do it this way, rather than using the Set Move Route event command, for a number of reasons:

  • the move route is quite long and movement commands are repeated (you can use a loop to repeat one or more commands rather than writing them all out individually)
  • you don't know which event you want to apply the move route to (you can use a variable to hold the event id, or even decide in the script if the move route should be applied to an event, a player, a follower or a vehicle)
  • you want to apply the same move route to several events, but don't want to copy/paste the entire thing for each event



Here's how to do it ...


A move route is an object of type RPG::MoveRoute - you can find this in the help file under RGSS Reference Manual > Game Library > RPGVXAce Data Structures > RPG::MoveRoute.  It contains values for repeat, skippable, and wait (the checkboxes you see on the Set Move Route command), as well as an array of RPG::MoveCommand objects (also defined in the help file, just below RPG::MoveRoute).  So to create a move route via script, you're going to need to create a new RPG::MoveRoute object plus one or more RPG::MoveCommand objects.  This is how each one is defined in the help file:

Spoiler
Code:
class RPG::MoveRoute
  def initialize
    @repeat = true
    @skippable = false
    @wait = false
    @list = [RPG::MoveCommand.new]
  end
  attr_accessor :repeat
  attr_accessor :skippable
  attr_accessor :wait
  attr_accessor :list
end



class RPG::MoveCommand
  def initialize(code = 0, parameters = [])
    @code = code
    @parameters = parameters
  end
  attr_accessor :code
  attr_accessor :parameters
end



As an example, I'll show you how to create a move route with a script call, that does what the Quick Event for a chest does.  A chest quick event has Wait for Completion turned on, Repeat Action and Skip turned off, and the following commands:

Code:
> Direction Fix OFF
> Turn Left
> Wait: 3 frame(s)
> Turn Right
> Wait: 3 frame(s)
>

notice there's a blank movement at the end of the move route.


So we'll create the move route itself first, and change the values for @repeat and @wait from the defaults (we don't want to skip, and to not skip is the default, so we can omit that one), and clear out the one blank movement command (we'll add it back later, after the real movements have been added):

Code:
mr = RPG::MoveRoute.new
mr.repeat = false
mr.wait = true
mr.list = []



Now we have to add in all the correct movement commands, followed by the empty one.  To do this, we need to know the code for the move action, and what parameters it expects, if any, and create a new RPG::MoveCommand for each one.  The Game_Character script lists all of these - just find the text in the constants section that describes the movement command, and use the code that follows.  Then look in the process_move_command method to see if any parameters are required.


Here are the codes and parameters for each of the commands above:

Code:
> Direction Fix OFF ... code 36, no parameters
> Turn Left         ... code 17, no parameters
> Wait: 3 frame(s)  ... code 15, parameter is the number of frames (see line 150 in default script)
> Turn Right        ... code 18, no parameters
> Wait: 3 frame(s)  ... code 15, parameter is the number of frames
>                   ... no code or parameters needed - this is just a fresh RPG::MoveCommand

Okay, so now that we know the codes and parameters, we just need to add them to mr.list:

Code:
mr.list.push( RPG::MoveCommand.new(36, []) )
mr.list.push( RPG::MoveCommand.new(17, []) )
mr.list.push( RPG::MoveCommand.new(15, [3]) )
mr.list.push( RPG::MoveCommand.new(18, []) )
mr.list.push( RPG::MoveCommand.new(15, [3]) )
mr.list.push( RPG::MoveCommand.new )



That finishes up your move route.  The last thing you need to do is apply it to your event (@event_id is the 'current' event, if you are putting this as a Script command on an event page - if you're actually writing a script and not attaching it to an event, you need to use an actual number for the event id):

Code:
$game_map.events[@event_id].force_move_route(mr)

So your full script is going to look something like this:

Spoiler
Code:
mr = RPG::MoveRoute.new
mr.repeat = false
mr.wait = true
mr.list = []

mr.list.push( RPG::MoveCommand.new(36, []) )
mr.list.push( RPG::MoveCommand.new(17, []) )
mr.list.push( RPG::MoveCommand.new(15, [3]) )
mr.list.push( RPG::MoveCommand.new(18, []) )
mr.list.push( RPG::MoveCommand.new(15, [3]) )
mr.list.push( RPG::MoveCommand.new )

$game_map.events[@event_id].force_move_route(mr)





Now, you can apply the same move route to a number of events by using a loop with a variable for the event id.  Let's say you have a little cutscene and there are 5 NPCs consecutively numbered as events 5, 6, 7, 8 and 9.  You want to make the player follow a particular path around them, and you want them all to turn to face the player as he/she moves.  You could do it with a number of move routes, trying to synchronise the timing, or you could do this:


mr = RPG::MoveRoute.new
mr.list = []
mr.list.push( RPG::MoveCommand.new(25,[]) )
mr.list.push( RPG::MoveCommand.new )
for evt in 5..9
$game_map.events[evt].force_move_route(mr)
end


The move route defaults are kept (repeat, no skip, no wait).  After you add that script, add the player's move route, with a Wait for Completion.  Then cancel the NPC move routes (so they stop turning to face the player if the player moves again) with this:

Code:
mr = RPG::MoveRoute.new
mr.list = []
mr.list.push( RPG::MoveCommand.new )
for evt in 5..9
  $game_map.events[evt].force_move_route(mr)
end



This is just a move route with the default settings, and no movement command, which has the effect of cancelling out any previous move routes that might have been running on the event.
This actually explains it pretty well to me, but some things are still confusing me. For example, play se. I get that I'm going to have to set parameters for name, pitch, volume. but for some reason I keep getting an error. I am unsure where to fit the play se in and where to add the .play to it.
Sure - this is a little trickier, because the methods in Game_Character don't tell you exactly what the parameter is.  So you have to go searching to find out what the parameter is.  You know it's an SE, so you just have to find out how to create a new SE.


Help file > RGSS Reference Manual > Game Library > RPGVXAce Data Structure > RPG::AudioFile > RPG::SE


The command, if you wanted to play the CatMeow.ogg file with default settings for volume and pitch, would be:

Code:
mr.list.push( RPG::MoveCommand.new( 44, [RPG::SE.new('CatMeow')]) )

Command 44 is Play SE, and the parameter array needs to contain a SE object with the SE name and optionally the volume and pitch.
That worked like a charm, thanks Shaz. I have, hopefully, one last question related to this topic. How would I set it so that part of my move route is skippable, but part is not? For example, I am making an event that if you are next to said event, and you push button X, and you are facing them then you hit them, they move backwards and an SE plays. I also want them to flash and a variable to increase when you do this. I want the movement portion to be skippable, but I want the SE and the flash to still trigger.


Using regular move routes I simply made two, but within a script call I am finding there is not enough room for all this.
If you can do it with a Set Move Route command, you should be able to do it with a script event.  By "not enough room" do you mean there aren't enough lines?  The latest version of Ace allows for more lines than the original version, so if you don't have the latest, it would be worth grabbing.  If that still isn't enough, instead of mr for the move route command, try using @mr in all locations.  This changes it from a local variable that exists only during the execution of the individual script call, to a local variable for the event's interpreter.  This means you can have multiple script calls that refer to the same variable, and it does not get cleared/reset between them.  So you could build as much as possible with one script call, then continue building it with the next, as if it were all in a single script call.


But if you're using so many that it can't fit within a script call command, I'd question whether it's better to just use a regular move route.
I think the global option may work for me. I'm going to give that a shot. I'd show you what I am working with, but that would be really off topic, so I will work with this global and see what can be done.
If you struggle and want specific help, just start a new thread in VX Ace Support and link back to this one for the explanation.  Screenshots of the actual move route that you're trying to mimic in script, or of the script commands you're using, will be really helpful too.
Really well done, but I'd like to know how to use a script call within a move route to move the event. What I'm trying to do (for obscure plot foreshadowing reasons) is prevent an event from moving outside a certain space. I'd use blank events to block his path, but there are other events on the map that should be allowed to move in and out of this space as they please.


EDIT: Also, the event will either follow this rule or not depending on what event page he's on.
Since it's not related to this tutorial, you should start a new thread in Ace Support to ask your question.
Since it's not related to this tutorial, you should start a new thread in Ace Support to ask your question.

Thanks for letting me know. I ended up figuring it out on my own (again). I swear this happens every time I come here for help and you reply. It's kind of funny actually, but yeah, I should have made a new thread to answer the question instead of asking it here.
It is a really good method, much shorter than the one I found previously 🙂


But I have a little problem with it. I use it with  $game_player.force_move_route instead of  $game_map.events[evt].force_move_route to move the player, but unlike the move command I can set in the event window, this method immediately does the next thing in the list (for example displaying a text) as the moving starts, not and the end of the movement.


So the difference:


Code: move, text display


Script effect: text display, moving


Original Move command effect: moving, and after moving, displaying the text


So far I couldn't find any method to place this forced movement to the right order.
@Gabda That is how all move routes (even evented ones) work, unless they have "wait for completion" checked.


And that is listed as option in the original topic - you forgot to set the wait to true in your script.
@Andar Yes, it should do that, but it is not working for me. It is the same for wait=true and wait=false.


But with the "wait for completion", there is a difference if it checked or not.
Then please show the code - the script does nothing but create a regular moveroute in the list, and if that doesn't work then either you made a mistake, or a different script broke all move routes.
OK. As the player starts to move to the right, the "Done." text is visible.

Spoiler
@>Script:


mr = RPG::MoveRoute.new
mr.repeat = false
mr.wait = true
mr.list = []


mr.list.push( RPG::MoveCommand.new(3, []) )
mr.list.push( RPG::MoveCommand.new(3, []) )
mr.list.push( RPG::MoveCommand.new(3, []) )
mr.list.push( RPG::MoveCommand.new(3, []) )
mr.list.push( RPG::MoveCommand.new(3, []) )
mr.list.push( RPG::MoveCommand.new(3, []) )
mr.list.push( RPG::MoveCommand.new )


$game_player.force_move_route(mr)


@>Text: -, -, Normal, Bottom


Done.

Even with a clean, new project.
This isn't really meant to be used for stuff that needs to happen in a sequence.


If you look at Game_Interpreter's Set Move Route command, you'll see in addition to setting the move route on the character, it also does a Fiber.yield while the move route is happening.  This is what stops the next command from running.  This isn't catered for by the instructions above.


You could try adding this line at the end of the script, but I don't know whether it'd help:


Fiber.yield while $game_player.move_route_forcing


If that doesn't work, you could just put in a loop that waits 4 frames, checks if the player is at the destination tile, and breaks if they are.


However, I have to ask why you're trying to do this to the player via a script call?  A simple Move Route command would do the job.  I've given a list of reasons why you might want to do it as a script call rather than use the event command, and what you're doing doesn't match any of them.  There is absolutely no reason that you have to do this via a script call.
The line you suggested works like a charm 🙂


The example with the text was just the simplest representation of my problem.


I want to control the character from a file, and do the interaction with the environment by changing the tiles instead of doing it with events.


And as I read the whole map from a file, I can't create a event for every item in the map. So far everything is working but the order of movement and interaction, as all the tile changes happen immediately as the movement starts. Thanks for the info on Fiber, I didn't know where should I look for the timing (as I am new to RPG maker and Ruby).
Glad to know it worked.  I was actually expecting it would throw an error.
Hello, I was wondering how one would do this with changing the players current graphic. I'm currently having a hard time working with the syntax for it, and I'm not sure if I even did this right:

def pbTransform
transform = RPG::MoveRoute.new
transform.repeat = false
transform.list = []

transform.list.push( RPG::MoveCommand.new(41, ['transform1']) )

transform.list.push( RPG::MoveCommand.new )
$game_player.force_move_route(transform)
end
From Game_Character:
Code:
    when ROUTE_CHANGE_GRAPHIC;    set_graphic(params[0], params[1])

so it's expecting 2 elements in the array, not 1.

From Game_CharacterBase:
Code:
  def set_graphic(character_name, character_index)

so array element 0 will be the character name, and array element 1 will be the index.

So if you want to use the very first image in 'transform1' the command will be:
Code:
transform.list.push(RPG::MoveCommand.new(41, ['transform1', 0])


Although, if you're doing that on the player, you would be better off just using an event command. The reason you'd want to build a script to do it is if you wanted to issue the same command on a number of different events, or if your event id is coming from a variable.

Original thread: https://forums.rpgmakerweb.com/threads/creating-a-move-route-with-a-script-command.59367/