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.

Little Doubt about ViewRange Module.

Hello Again.

Well, i have seen the MACL and i found the famous ViewRange script (Near fantastica) and i have a Doubt about this.

Does this script work of the same way than the original?, because the script from the MACL its more short.

Here is it.

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
#==============================================================================

MACL::Loaded << 'Modules.View Range Module'

#==============================================================================
# ** 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

If the Answer is Yes, how i can use this?..

Thanks.
 
Doesn't work the same way.

The version 3 system in the forum is used in map events and is designed to turn an event's self switch ON (A, B, C or D) if the system finds the 'player' within a semi-circle in front of the targetted event.

This version returns an integer value if the player is found within range of the target event's range (in either a semi-circle or rectangle area). If the 'player' isn't in range, it returns a floating-point value.

Guess it really boils down to HOW you plan to use it... version 3 is easier for non-scripters as it uses map events & self-switches, while this one is more advanced and precise and uses script values and test 'em to see if they're integers or floats.
 
Thank you for the answer.
In that case. I will use the script that you recommended me to. ( at The Lost Scripts's forum )
The fact that my intention was supposes itself to play-act the script The guards

Thanks again.

(A question about pathfinding, What code i Should use to make an event go to the Player Position <= I made this question here to not open another Topic):
 
Probably the version 1 system as the system will not suffer from trying to find a path to a location 'Taken up' by an event. It's easier to work with too.

Version 2 is sweet and has the ability to make 'custom move routes' of its own, but if you try to move to a tile location already occupied... you may get an error pop-up. Version 1 will work fine as it doesn't care if the spot is occupied, and I figure the events/enemies will be set to 'start combat' on an 'event-touch' trigger...
 
Truthfully, this version is simpler than earlier versions of the VR. Earlier VR's created ViewRange objects, and did a lot of un-needed things.

Here, you send and objects and/or numbers. That's it. To test if the player is within 3 tiles of event 5
Code:
if VR.in_range?($game_player, $game_map.events[5], 3)

Doesn't get much simpler than that. :lol:
 

Anonymous

Guest

The old VR did that as well, Sephiroth Spawn.

The difference with this VR is that it can handle various inputs, instead of just objects (it can also handle literal integers) and can also check if an object is within a rectangle which is awesome.
 

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