<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"><head>
<script type="text/javascript" language="JavaScript">
function reDo() {
if (innerWidth != origWidth || innerHeight != origHeight)
location.reload();
}
if ((parseInt(navigator.appVersion) == 4) && (navigator.appName == "Netscape")) {
origWidth = innerWidth;
origHeight = innerHeight;
onresize = reDo;
}
onerror = null;
</script>
<style type="text/css">
< !-- div.WebHelpPopupMenu {
position: absolute;
left: 0px;
top: 0px;
z-index: 4;
visibility: hidden;
}
p.WebHelpNavBar {
text-align: right;
}
-->
</style>
<script type="text/javascript">
gRootRelPath = "../../../..";
gCommonRootRelPath = "../../../..";
gTopicId = "9.2.2.13.2_9";
<script type="text/javascript" src="../../../../template/scripts/rh.min.js"></script>
<script type="text/javascript" src="../../../../template/scripts/common.min.js"></script>
<script type="text/javascript" src="../../../../template/scripts/topic.min.js"></script>
<script type="text/javascript" src="../../../../template/scripts/topicwidgets.min.js"></script>
<script type="text/javascript" src="../../../../whxdata/projectsettings.js"></script>
<link rel="stylesheet" type="text/css" href="../../../../template/styles/topic.min.css"/>
<link rel="stylesheet" type="text/css" href="../../../../template/Charcoal_Grey/topicheader.css"/>
<meta name="topic-status" content="Draft"/>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>instance_nearest</title>
<meta name="generator" content="Adobe RoboHelp 2019"/>
<link rel="stylesheet" href="../../../../assets/css/default.css" type="text/css"/>
<meta name="rh-authors" content="Mark Alexander"/>
<meta name="topic-comment" content="Page outlining the function instance_nearest"/>
<meta name="rh-index-keywords" content="instance_nearest"/>
<meta name="search-keywords" content="instance_nearest"/>
</head>
<body>
<div class="topic-header rh-hide" id="rh-topic-header" onclick="rh._.goToFullLayout()">
<div class="logo">
</div>
<div class="nav">
<div class="title" title="instance_nearest">
<span>instance_nearest</span>
</div>
<div class="gotohome" title="Click here to see this page in full context">
<span>Click here to see this page in full context</span>
</div>
</div>
</div>
<div class="topic-header-shadow rh-hide" id="rh-topic-header-shadow"></div>
<h1>instance_nearest</h1>
<p>This function will check all the instances of the given object to see which is nearest to the given x/y point. All checks will be from the given x/y position to the <i>origin</i> (the x/y position) of instances of the object specified. If no instances
of the object exist, the function will return the keyword <a href="../../../GML_Overview/Instance_Keywords.htm"><span class="inline">noone</span></a>, but if there are instances then it will return the <a href="Instance_Variables/id.htm"><tt>id</tt></a> of the instance found. Please note that if the instance running the code has the same object index as the object being checked, then it will be included in the check (this includes checks for parent objects if the calling instance is also a child
of the parent).</p>
<p> </p>
<h4>Syntax:</h4>
<p class="code">instance_nearest(x, y, obj);</p>
<table>
<tbody>
<tr>
<th>Argument</th>
<th>Description</th>
</tr>
<tr>
<td>x</td>
<td>The x position to check from.</td>
</tr>
<tr>
<td>y</td>
<td>The y position to check from.</td>
</tr>
<tr>
<td>obj</td>
<td>The object to check for instances of.</td>
</tr>
</tbody>
</table>
<p> </p>
<h4>Returns:</h4>
<p class="code">Real (either an instance ID value or the keyword <tt>noone</tt>)</p>
<p> </p>
<h4>Example:</h4>
<p class="code">var inst, xx;<br/> xx = x;<br/> x -= 10000;<br/> inst = instance_nearest(xx, y, object_index);<br/> if inst != id<br/> {
<br/> draw_line(x, y, inst.x, inst.y);<br/> }
<br/> x += 10000;</p>
<p>The above code move the current instance 10000 pixels then check its previous position to find the nearest instance of the same object type. If that instance is itself, it will do nothing more than move back to its original position, but should the
instance found be different, it will draw a line between the two.</p>
<p> </p>
<p> </p>
<p> </p>
<div class="footer">
<div class="buttons">
<div class="clear">
<div style="float:left">Back: <a href="Instances.htm">Instances</a></div>
<div style="float:right">Next: <a href="instance_number.htm">instance_number</a></div>
</div>
</div>
<h5>© Copyright YoYo Games Ltd. 2020 All Rights Reserved</h5>
</div>
</body></html>