This is a locked, single-post thread from Creation Asylum. Archived here to prevent its loss.
No support is given. If you are the owner of the thread, please contact administration.
I have updated this method, which now only ever uses up to 3 arguments. What it does is simple: use it as a script call in an event to get the distance between two objects. Some examples:
[ruby]$game_variables[1] = get_distance()[/ruby]This line in a Call Script will set Variable 1 to the distance between the player and the event that called it, rounding to the nearest whole number. Best used within the Script command in an event.
[ruby]get_distance(-1, 3) > 7[/ruby]This line will return true if Event ID 3 is more than 7 tiles distant from the player. Best used within a conditional, inside any event on the same map as the Event ID 3 you want to compare against.
The Script
[ruby]#==============================================================================
# ** Distance Checking
#------------------------------------------------------------------------------
# This little tool finds the distance between two objects.
#
# Courtesy of DrakoShade; April 23, 2008.
#------------------------------------------------------------------------------
# To call this from within an event, simply use the following line:
# get_distance(pointa, pointb, round)
# You don't actually have to make any arguments.
# get_distance() will check the distance between the player and the calling
# event. The arguments have to be added in order, however, so if you want to
# check the player against a point other than the caller, you have to input
# both points, and if you want rounding to be other than default, all three
# arguments must be made.
#
# "pointa" and "pointb" can accept two different kinds of input, and behave
# exactly the same. If you want to, you can input a point in the form
# of an array [x,y]. Otherwise, you can input -1 to return the player's
# position, 0 for the position of the calling event, or the event number
# of any event on the map.
# Calling for an event number that doesn't exist (such as Event 207 when there
# are only 16 events on the map) will cause an error. As will inputting
# anything other than an array, -1, 0, or an event number.
# "round" is a boolian (either true or false) telling the script whether or
# not you want the number returned to be rounded. I have it defaulted to
# true, but you can change that in the second line of actual script by
# simply replacing false with true.
# Thanks to Etheon for help cleaning this up.
#==============================================================================
class Game_Interpreter
def get_distance(pointa = -1, pointb = 0, round = true)
if pointa.is_a?(Array)
xa, ya = pointa[0], pointa[1]
elsif pointa.is_a?(Integer)
a = get_character(pointa)
if a.is_a?(Game_Character)
xa, ya = a.x, a.y
end
end
if pointb.is_a?(Array)
xb, yb = pointb[0], pointb[1]
elsif pointb.is_a?(Integer)
b = get_character(pointb)
if b.is_a?(Game_Character)
xb, yb = b.x, b.y
end
end
d = Math.sqrt(((xb-xa)*(xb-xa))+((yb-ya)*(yb-ya)))
d = d.round if round
return d
end
end[/ruby]