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.

Move Event towards another event HELP!

I think im having a brain fart... but how in the world can you make an event move towards another events location? Ive been on it for the last 6 hours trying to do trials and error... now im assuming there aint no way? I know there is set event location but that would just teleport an event to another events location without having movement to the event location.
 
Well, I made a script for you :) I wasn't too sure of any other way to do it (there probably is, I just forgot lol). Well anyway, what the script does is allow you to make one event move towards the event of another, and all you have to supply is the ID of the event that will be getting followed.

Script:

[rgss]#==============================================================================
# Title: Move Towards Event
# Author: The Law G14
# Requester: susanM
#==============================================================================
 
#==============================================================================
# ** Game_Character (part 3)
#------------------------------------------------------------------------------
#  This class deals with characters. It's used as a superclass for the
#  Game_Player and Game_Event classes.
#==============================================================================
 
class Game_Character
  #--------------------------------------------------------------------------
  # * Move toward Event
  #--------------------------------------------------------------------------
  def move_toward_event(event_id)
    # Set Main Event
    main_event = $game_map.events[event_id]
    # Get difference in coordinates
    sx = @x - main_event.x
    sy = @y - main_event.y
    # If coordinates are equal
    if sx == 0 and sy == 0
      return
    end
    # Get absolute value of difference
    abs_sx = sx.abs
    abs_sy = sy.abs
    # If horizontal and vertical distances are equal
    if abs_sx == abs_sy
      # Increase one of them randomly by 1
      rand(2) == 0 ? abs_sx += 1 : abs_sy += 1
    end
    # If horizontal distance is longer
    if abs_sx > abs_sy
      # Move towards event, prioritize left and right directions
      sx > 0 ? move_left : move_right
      if not moving? and sy != 0
        sy > 0 ? move_up : move_down
      end
    # If vertical distance is longer
    else
      # Move towards event, prioritize up and down directions
      sy > 0 ? move_up : move_down
      if not moving? and sx != 0
        sx > 0 ? move_left : move_right
      end
    end
  end
end
 
#==============================================================================
#
# *** END OF SCRIPT ***
#
#==============================================================================
[/rgss]

The call script is as follows:

[rgss]move_toward_event(X)
[/rgss]

X being the ID of the event that will be followed.


Instructions

So basically, go to "Set Move Route" in the List of Event Commands of one Event. At the top, set that scroll thing to "This Event". Then, go to the bottom right where it says "Script", and put the call script in. Just change X to the ID of the event that will be followed by our current event. And that's it! Hope it all works for you :)
 

Kabeem

Member

Hi there. Sorry for bumping a month old topic but I came here because I was looking for a pathfinding script. I thought you should know that the problem with this script is that the event will get stuck if something is in the way, such as a building.
 
That is by no way a problem, but an expected outcome... as long as the event continues to move as soon as it doesn't have to pass a wall again (aka doesn't actually get stuck beyond repair), that is exactly what the script does - it's NOT a pathfinding script.
 
What about adding something to this script to make it a simple pathfinding script? My idea is this: place hidden "path marker" events around the map, around walls, every several spaces, then tell the event what event it wants to get to, scan for the marker nearest it, the one nearest that, etc. Then you can have the main event go to each marker on its way to its destination? Still not perfect, but seems easy enough to implement (I might be totally wrong since I am terrible at scripting.)

OR

Would it be possible to have the event move a random direction instead for a few spaces if it gets 'stuck', then try to find its event again?
 
just using one of the many pathfinding scripts that already out there would be way easier, they can cause lag, but so will doing what you propose :P also there are several really good anti lag scripts out there, which reminds me i really wish i would finish mine lol
 

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