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.

[FILLED] Conditional Branch Script based on player/event location

Status
Not open for further replies.
Hey peoples, how are you doing today?

This is an XP request, this time :)

I seem to be doing a few more public requests than I usually do (there's 3 this year D:).  And I hope I don't lose anyone who might've been interested in filling this one out, had they been able to make out what I had been intending to request heh.

I searched but I could not find this, though I could have sword it had existed?  I could have sworn a version of what I'm about to request had been done, at least in part.

I'm looking for a script that would I believe ideally be used in a conditional branch event command.  I'm not sure, but I believe that's where it would go best.  Basically if player or other event on the map is within x amount of tiles from another event, or set location (a specific tile such as 14x16 - which would be 14 tiles across, 16 down), the condition is met.

Basically two examples that might help understand what I mean:
1) There is a map filled with trees and an NPC who walks around randomly.  When not within range of any of the trees, talking to him would have a basic dialogue box of "Wonderful weather, no?" or some crap.  But when within range of the trees (set by tile - or if that's not it, an event on the tree itself) he responds with something based on the tree.  A large tree with apples, he's within 4 tiles he'll say "Wonderful apples, I think I'll pick some later." and he moves 6 tiles over, he's within 4 tiles of a dead tree "Pity, we'll have to cut this tree down soon.", and he moves over more and is now within 6 tiles of a very tall tree "I met my first love under this tree." etc.

2) There is an event that follows the player around on another map, however when this event goes near certain food stands, a conditional branch sets off the move route and it moves towards these food stands.  When spoken to, the dialogue is about the exact food stands, instead of the usual.  All that can be done in the events normally, naturally.  When the player goes X amount of tiles away from the event that follows him/her around, a another conditional branch sets a new move route to catch up to the player.  Etc.
Think of like a caterpillar script, only party members won't be right up the player's ass the entire time, and will window shop and such.

3) There are 4 events on a map.  And when any of them get close to each other, they stop and do a little dance by turning in circles 5 times and then walk away from each other.  When any of them come within 3 tiles of each other, that's what they do, and won't do that normally.  They move around randomly and the hero can push them.

See, I know all the above can be done in events.  It's just very tedious and sometimes causes a strange lag when trying to do so many parallel processes and variable checks, etc in this way.  Feel up to it?  Any questions?

Thanks, either way, and have a good day :thumb:
 
Well, you can test event distances with this:

Code:
#============================================================================== 
# ** Modules.View Range (5.0)               By Near Fantastica & SephirothSpawn
#------------------------------------------------------------------------------
# * Description :
#
#   The View Range Module is used to test objects position in comparision with
#   another object. It can test if an object is within a defined circular
#   range of another object or a rectangular defined region. It can also tell
#   you this distances between objects.
#------------------------------------------------------------------------------
# * Syntax :
#
#   Test if object is within defined range from another object :
#    - VR.in_range?(object1, object2, range)
#    - VR.in_range?(object1x, object1y, object2x, object2y, range)
#
#   Test if object is within defined rectanlge :
#    - VR.in_rect_range?(object, <args>)
#    - VR.in_rect_range?(x, y, <args>)
#
#    args can either be : x, y, width, height or Rect.new(x, y, width, height)
#
#   Test range between objects :
#    - range = VR.range(object1, object2, <integer>)
#    - range = VR.range(object1x, object1y, object2x, object2y, <integer>)
#
#    integer : if true, returns integer ; if false, returns float
#==============================================================================

#==============================================================================
# ** View Range
#==============================================================================

module VR
  #----------------------------------------------------------------------------
  # * Within Range Test
  #----------------------------------------------------------------------------
  def VR.in_range?(*args)
    # If 3 Arguments (Element, Object, Range)
    if args.size == 3
      x = (args[0].x - args[1].x) ** 2
      y = (args[0].y - args[1].y) ** 2
      r = args[2]
    # If 5 Arguments (Elementx, Elementy, Objectx, Objecty, Range)
    elsif args.size == 5
      x = (args[0] - args[2]) ** 2
      y = (args[1] - args[3]) ** 2
      r = args[4]
    else
      p 'Wrong Defined Number of Arguments'
      return 
    end
    return (x + y) <= (r * r)
  end
  #----------------------------------------------------------------------------
  # * Within Rect Range Test
  #----------------------------------------------------------------------------
  def VR.in_rect_range?(*args)
    # If 2 Arguments (Object, Rect)
    if args.size == 2
      x_, y_ = args[0].x, args[0].y
      x, y, w, h = args[1].x, args[1].y, args[1].width, args[1].height
    # If 3 Arguments (Objectx, Objecty, Rect)
    elsif args.size == 3
      x_, y_ = args[0], args[1]
      x, y, w, h = args[2].x, args[2].y, args[2].width, args[2].height
    # If 5 Arguments (Object, Rectx, Recty, Rectwidth, Rectheight)
    elsif args.size == 5
      x_, y_ = args[0].x, args[0].y
      x, y, w, h = args[1], args[2], args[3], args[4]
    # If 6 Arguments (Objectx, Objecty, Rectx, Recty, Rectwidth, Rectheight)
    elsif args.size == 6
      x_, y_, x, y, w, h = *args
    else
      p 'Wrong Defined Number of Arguments'
      return
    end
    # Return Object Within Rect
    return x_.between?(x, x + w) && y_.between?(y, y + h)
  end
  #----------------------------------------------------------------------------
  # * Range
  #----------------------------------------------------------------------------
  def VR.range(*args)
    # If 2 Arguments (Element, Object)
    if args.size == 2
      x = (args[0].x - args[1].x) * (args[0].x - args[1].x)
      y = (args[0].y - args[1].y) * (args[0].y - args[1].y)
      integer = true
    # If 3 Arguments (Element, Object, Integer
    elsif args.size == 3
      x = (args[0].x - args[1].x) * (args[0].x - args[1].x)
      y = (args[0].y - args[1].y) * (args[0].y - args[1].y)
      integer = args[2]
    # If 4 Arguments (Elementx, Elementy, Objectx, Objecty)
    elsif args.size == 4
      x = (args[0] - args[2]) * (args[0] - args[2])
      y = (args[1] - args[3]) * (args[1] - args[3])
      integer = true
    # If 5 Arguments (Elementx, Elementy, Objectx, Objecty, integer)
    elsif args.size == 5
      x = (args[0] - args[2]) * (args[0] - args[2])
      y = (args[1] - args[3]) * (args[1] - args[3])
      integer = args[4]
    else
      p 'Wrong Defined Number of Arguments'
      return
    end
    r = Math.sqrt(x + y)
    return integer ? r.to_i : r
  end
end

So, to test if event comes with 5 tiles of tile[3, 2]
Code:
VR.in_range?((e = $game_map.events[id]).x, e.y, 3, 2, 5)

If you need any help with it, just ask.
 
Oh wow that was easy :)

I was just about to post what I thought was a problem, but stupid me had the wrong event ID ha ha.  That's extremely helpful SS, thanks.  It works like a charm so far.

And thanks Hevendor for the suggestion too.
 
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