make an event move toward another event

Discussion in 'RPG Maker VX Ace' started by frostwolf3, Mar 18, 2019.

  1. frostwolf3

    frostwolf3 Veteran Veteran

    Messages:
    30
    Likes Received:
    2
    First Language:
    english
    hey there,
    first of all i'm terribly sorry if i'm posting in the wrong place, idk why i always end up in the wrong place if not then cool

    I would like to make an event move toward a second event
    I know I can make event move toward players but I don't need that
    I would really appreciate it if i didn't have to use plugin, and i don't mind using scripting
    thanks guys
     
    #1
  2. Heirukichi

    Heirukichi Veteran Veteran

    Messages:
    935
    Likes Received:
    319
    Location:
    Italy
    First Language:
    Italian
    Primarily Uses:
    RMVXA
    An obvious greedy - though not optimal - solution would be to close the gap on the X axis and then on the Y axis. This provides the shortest theoretical path without taking passability into account.

    If you are moving your event from a script call within the event itself you can do this:
    Code:
    ev = $game_map.events[@event_id]
    destination_event = $game_map.events[other_event_id] # change this to be the other event ID
    dist_x = ev.x - destination_event.x # if this is > 0 your destination is on the right, if it is < 0 it is on the left
    dist_y = ev.y - destination_event.y # >0 means your destination is above, <0 means it is below
    total_distance = dist_x.abs + dist_y.abs # this is your total distance
    while (total_distance > 1)
      moved = false;
      dx = ev.x - destination_event.x
      dy = ev.y - destination_event.y
      if (dx != 0)
        x_mod = dx / dx.abs
        dir = x_mod > 0 ? 4 : 6
        if ev.passable?(ev.x - x_mod, ev.y, dir)
          ev.move_straight(dir)
          Fiber.yield while ev.moving?
          moved = true
      end; end; next if moved;
      if (dy != 0)
        y_mod = dy / dy.abs
        dir = y_mod > 0 ? 8 : 2
        if ev.passable?(ev.x, ev.y - y_mod, dir)
          ev.move_straight(dir)
          Fiber.yield while ev.moving?
          moved = true
      end; end; break unless moved;
      total_distance -= 1
    end
    The code should be something like this. I cannot check eventual mistakes because I cannot access the engine right now. However I am quite sure it is a bit too long to fit in your script box.

    Another option would be drawing the path between those events using regions (if those events are fixed) and define a path according to those regions.

    NOTE: the script call above stops if it is not possible to close the gap with the next step. It works in a similar way to "Move toward player" so if there are walls in the way it will have no effect at all. Be sure to use this wisely. Other ways to close the gap calculating the shortest path are way too long to be written here and I would really recommend using a script if you want something like those.

    P.S. plugins are for MV, not for VX Ace so of course you will not be using them. XD
     
    Last edited: Mar 18, 2019
    #2
    frostwolf3 likes this.
  3. frostwolf3

    frostwolf3 Veteran Veteran

    Messages:
    30
    Likes Received:
    2
    First Language:
    english
    @Heirukichi I honestly can't thank you enough for the code thank you thank you thank you
    and i cant use a default path because the other event is constantly moving
    and thanks for the tip ^_^
     
    #3
  4. Heirukichi

    Heirukichi Veteran Veteran

    Messages:
    935
    Likes Received:
    319
    Location:
    Italy
    First Language:
    Italian
    Primarily Uses:
    RMVXA
    You're welcome, I just hope I did not make any mistakes when writing it. Not having syntax highlighting nor any way to complete words automatically really makes writing code much harder. If you add a touchscreen keyboard to the mix you get what is probably the worst possible combination.

    However if you find any mistake and do not know how to fix it feel free to ask and I will try to help as much as I can. As a side note you should know that everything I wrote in that code can be done without using a script call. Event commands are enough to do that. You cannot check passability with them but if you check your event coordinates after telling it to move and they are the same it means the tile was not passable.
     
    #4
    frostwolf3 likes this.

Share This Page