Envision, Create, Share

Welcome to HBGames, a leading amateur game development forum and Discord server. All are welcome, and amongst our ranks you will find experts in their field from all aspects of video game design and development.

How to Set Move Route with script?

Status
Not open for further replies.
What's the script to set an event's or the player's move route?

Or, more specifically, how do I get an event/player to move to a certain x/y on the map, and at certain speeds? Like, say, I wanted Old Man to walk right next to Old Lady but don't want to remember all the Move Speed Ups, Walk Rights, Walk Lefts, Move Speed Downs, etc. Instead he'd move to Old Lady's X(-1),Y coordinate.

NOTE: This isn't the same as Set Event Location, which would instantly put an event at another place. This one's where the event is actually physically moving over there.
 
There are some Pathfinding scripts around the submitted scripts section which allow you to move the player or an event to variable coordinates. I'll edit this post with a link when I dig one up. :)

EDIT: Here's the one I was thinking of

Code:
#==============================================================================
#   â–  Path Finding
#   By: Near Fantastica
#   Date: 24.09.05
#   Version: 1
#
#   Player :: $game_player.find_path(x,y)
#   Event Script Call :: self.event.find_path(x,y)
#   Event Movement Script Call :: self.find_path(x,y)
#==============================================================================

class Game_Character
  #--------------------------------------------------------------------------
  alias pf_game_character_initialize initialize
  alias pf_game_character_update update
  #--------------------------------------------------------------------------
  attr_accessor :map
  attr_accessor :runpath
  #--------------------------------------------------------------------------
  def initialize
    pf_game_character_initialize
    @map = nil
    @runpath = false
  end
  #--------------------------------------------------------------------------
  def update
    run_path if @runpath == true
    pf_game_character_update
  end
  #--------------------------------------------------------------------------
  def run_path
    return if moving?
    step = @map[@x,@y]
    if step == 1
      @map = nil
      @runpath = false
      return
    end
    dir = rand(2)
    case dir
    when 0
      move_right if @map[@x+1,@y] == step - 1 and step != 0
      move_down if @map[@x,@y+1] == step - 1 and step != 0
      move_left if @map[@x-1,@y] == step -1 and step != 0
      move_up if @map[@x,@y-1] == step - 1 and step != 0
    when 1
      move_up if @map[@x,@y-1] == step - 1 and step != 0
      move_left if @map[@x-1,@y] == step -1 and step != 0
      move_down if @map[@x,@y+1] == step - 1 and step != 0
      move_right if @map[@x+1,@y] == step - 1 and step != 0
    end
  end
  #--------------------------------------------------------------------------
  def find_path(x,y)
    sx, sy = @x, @y
    result = setup_map(sx,sy,x,y)
    @runpath = result[0]
    @map = result[1]
    @map[sx,sy] = result[2] if result[2] != nil
  end
  #--------------------------------------------------------------------------
  def setup_map(sx,sy,ex,ey)
    map = Table.new($game_map.width, $game_map.height)
    map[ex,ey] = 1
    old_positions = []
    new_positions = []
    old_positions.push([ex, ey])
    depth = 2
    depth.upto(100){|step|
      loop do
        break if old_positions[0] == nil
        x,y = old_positions.shift
        return [true, map, step] if x == sx and y+1 == sy
        if $game_player.passable?(x, y, 2) and map[x,y + 1] == 0
          map[x,y + 1] = step
          new_positions.push([x,y + 1])
        end
        return [true, map, step] if x-1 == sx and y == sy
        if $game_player.passable?(x, y, 4) and map[x - 1,y] == 0
          map[x - 1,y] = step
          new_positions.push([x - 1,y])
        end
        return [true, map, step] if x+1 == sx and y == sy
        if $game_player.passable?(x, y, 6) and map[x + 1,y] == 0
          map[x + 1,y] = step
          new_positions.push([x + 1,y])
        end
        return [true, map, step] if x == sx and y-1 == sy
        if $game_player.passable?(x, y, 8) and map[x,y - 1] == 0
          map[x,y - 1] = step
          new_positions.push([x,y - 1])
        end
      end
      old_positions = new_positions
      new_positions = []
    }
    return [false, nil, nil]
  end
end

class Interpreter
  #--------------------------------------------------------------------------
  def event
    return $game_map.events[@event_id]
  end
end

To move the player to a certain location, call "$game_player.find_path(X, Y)". For an event, call "$game_map.events[EVENT_ID].find_path(X, Y)"

Be sure to replace X, Y, and EVENT_ID with the appropriate values.
 
Cool, it's working. Not everything, though -- Wait for Move Completion doesn't work (probably because it's through Call Script?). Also, instead of a set x,y is there a way to move to another event's x,y?
 
Yes, wait for move completion won't work with this script. This should be a work around though:

Code:
@> Loop
     @> Wait 1 Frame
     @> Conditional Branch: Script: not $game_map.events[EVENT_ID].runpath
          @> Break Loop
     @> Repeat above

As for the other problem call this script

Code:
x = $game_map.events[EVENT_ID1].x
y = $game_map.events[EVENT_ID1].y
$game_map.events[EVENT_ID2].find_path(x, y)

where EVENT_ID1 is the event that you want EVENT_ID2 to move to. Of course you can substitute the player in there as well.
 
Status
Not open for further replies.

Thank you for viewing

HBGames is a leading amateur video game development forum and Discord server open to all ability levels. Feel free to have a nosey around!

Discord

Join our growing and active Discord server to discuss all aspects of game making in a relaxed environment. Join Us

Content

  • Our Games
  • Games in Development
  • Emoji by Twemoji.
    Top