<?xml version="1.0"?>
<?xml-stylesheet type="text/css" href="https://www.simswiki.info/skins/common/feed.css?303"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
		<id>http://simswiki.info/index.php?action=history&amp;feed=atom&amp;title=BHAV%3AFind_nearest</id>
		<title>BHAV:Find nearest - Revision history</title>
		<link rel="self" type="application/atom+xml" href="http://simswiki.info/index.php?action=history&amp;feed=atom&amp;title=BHAV%3AFind_nearest"/>
		<link rel="alternate" type="text/html" href="http://simswiki.info/index.php?title=BHAV:Find_nearest&amp;action=history"/>
		<updated>2026-09-25T08:22:50Z</updated>
		<subtitle>Revision history for this page on the wiki</subtitle>
		<generator>MediaWiki 1.20.2</generator>

	<entry>
		<id>http://simswiki.info/index.php?title=BHAV:Find_nearest&amp;diff=59704&amp;oldid=prev</id>
		<title>Kiwi tea at 17:11, 17 September 2012</title>
		<link rel="alternate" type="text/html" href="http://simswiki.info/index.php?title=BHAV:Find_nearest&amp;diff=59704&amp;oldid=prev"/>
				<updated>2012-09-17T17:11:02Z</updated>
		
		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;table class='diff diff-contentalign-left'&gt;
				&lt;col class='diff-marker' /&gt;
				&lt;col class='diff-content' /&gt;
				&lt;col class='diff-marker' /&gt;
				&lt;col class='diff-content' /&gt;
			&lt;tr style='vertical-align: top;'&gt;
			&lt;td colspan='2' style=&quot;background-color: white; color:black;&quot;&gt;← Older revision&lt;/td&gt;
			&lt;td colspan='2' style=&quot;background-color: white; color:black;&quot;&gt;Revision as of 17:11, 17 September 2012&lt;/td&gt;
			&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 34:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 34:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;&amp;#160; 0x000B: Push Interaction (Local 0x0004, Literal 0x0001, inherited priority), True Target: True, False Target: Error&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;&amp;#160; 0x000B: Push Interaction (Local 0x0004, Literal 0x0001, inherited priority), True Target: True, False Target: Error&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;−&lt;/td&gt;&lt;td style=&quot;background: #ffa; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;[[Category:Modding]]&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;background: #cfc; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;[[Category:&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;Sims 2 &lt;/ins&gt;Modding]]&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;</summary>
		<author><name>Kiwi tea</name></author>	</entry>

	<entry>
		<id>http://simswiki.info/index.php?title=BHAV:Find_nearest&amp;diff=50043&amp;oldid=prev</id>
		<title>MogHughson at 14:39, 22 April 2011</title>
		<link rel="alternate" type="text/html" href="http://simswiki.info/index.php?title=BHAV:Find_nearest&amp;diff=50043&amp;oldid=prev"/>
				<updated>2011-04-22T14:39:03Z</updated>
		
		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;table class='diff diff-contentalign-left'&gt;
				&lt;col class='diff-marker' /&gt;
				&lt;col class='diff-content' /&gt;
				&lt;col class='diff-marker' /&gt;
				&lt;col class='diff-content' /&gt;
			&lt;tr style='vertical-align: top;'&gt;
			&lt;td colspan='2' style=&quot;background-color: white; color:black;&quot;&gt;← Older revision&lt;/td&gt;
			&lt;td colspan='2' style=&quot;background-color: white; color:black;&quot;&gt;Revision as of 14:39, 22 April 2011&lt;/td&gt;
			&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 33:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 33:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;&amp;#160; 0x000A: Expression&amp;#160; &amp;#160; &amp;#160;  (Stack Object ID := My object id),&amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; True Target: 0x000B, False Target: Error&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;&amp;#160; 0x000A: Expression&amp;#160; &amp;#160; &amp;#160;  (Stack Object ID := My object id),&amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; True Target: 0x000B, False Target: Error&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;&amp;#160; 0x000B: Push Interaction (Local 0x0004, Literal 0x0001, inherited priority), True Target: True, False Target: Error&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;&amp;#160; 0x000B: Push Interaction (Local 0x0004, Literal 0x0001, inherited priority), True Target: True, False Target: Error&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;#160;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;background: #cfc; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;color: red; font-weight: bold; text-decoration: none;&quot;&gt;&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;#160;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;background: #cfc; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;color: red; font-weight: bold; text-decoration: none;&quot;&gt;[[Category:Modding]]&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;</summary>
		<author><name>MogHughson</name></author>	</entry>

	<entry>
		<id>http://simswiki.info/index.php?title=BHAV:Find_nearest&amp;diff=50035&amp;oldid=prev</id>
		<title>MogHughson at 13:24, 22 April 2011</title>
		<link rel="alternate" type="text/html" href="http://simswiki.info/index.php?title=BHAV:Find_nearest&amp;diff=50035&amp;oldid=prev"/>
				<updated>2011-04-22T13:24:11Z</updated>
		
		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;table class='diff diff-contentalign-left'&gt;
				&lt;col class='diff-marker' /&gt;
				&lt;col class='diff-content' /&gt;
				&lt;col class='diff-marker' /&gt;
				&lt;col class='diff-content' /&gt;
			&lt;tr style='vertical-align: top;'&gt;
			&lt;td colspan='2' style=&quot;background-color: white; color:black;&quot;&gt;← Older revision&lt;/td&gt;
			&lt;td colspan='2' style=&quot;background-color: white; color:black;&quot;&gt;Revision as of 13:24, 22 April 2011&lt;/td&gt;
			&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 1:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 1:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;==Overview==&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;==Overview==&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;−&lt;/td&gt;&lt;td style=&quot;background: #ffa; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;Here we show a [[SimAntics]] BHAV code snippet that will find the closest &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;desk&lt;/del&gt;. It loops round looking for objects of a certain type and calculating the distance between that object and the current Sim. It tests the distance of each object with the previous lowest distance and if closer saves the new distance and the new object ID. Once all objects of the type have been cycled round, we drop out of the loop and make use of the nearest object that we have found.&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;background: #cfc; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;Here we show a [[SimAntics]] BHAV code snippet that will find the closest &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;sink&lt;/ins&gt;. It loops round looking for objects of a certain type and calculating the distance between that object and the current Sim. It tests the distance of each object with the previous lowest distance and if closer saves the new distance and the new object ID. Once all objects of the type have been cycled round, we drop out of the loop and make use of the nearest object that we have found. &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;This example will then push an interaction onto the current Sim to use the nearest sink to get a glass of water&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;==Illustrating==&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;==Illustrating==&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 7:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 7:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;* [[0x001F|Set to Next (0x001F)]] [[obj with category|obj with category == temp 0]] varient&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;* [[0x001F|Set to Next (0x001F)]] [[obj with category|obj with category == temp 0]] varient&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;* [[0x000B|Distance (0x000B)]]&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;* [[0x000B|Distance (0x000B)]]&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;#160;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;background: #cfc; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;color: red; font-weight: bold; text-decoration: none;&quot;&gt;* [[0x000D|Push Interaction (0x000D)]]&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;==The BHAV code==&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;==The BHAV code==&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 16:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 17:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;&amp;#160; 0x0002: Expression (Local 0x0004 := Literal 0x0000), True Target: 0x0003, False Target: Error&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;&amp;#160; 0x0002: Expression (Local 0x0004 := Literal 0x0000), True Target: 0x0003, False Target: Error&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;===The Loop===&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;===The Loop===&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;−&lt;/td&gt;&lt;td style=&quot;background: #ffa; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;Now we begin our loop round all the objects of a certain type. We are going to look for &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;desks&lt;/del&gt;. We set the value for &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;desk &lt;/del&gt;types in [[Sims 2 Temp|Temp]] variable zero as that is what the next instruction expects. We use the Global constant for this as [[SimPE]] helpfully shows us the text assigned to the constant which helps us to know we have found the correct value.&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;background: #cfc; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;Now we begin our loop round all the objects of a certain type. We are going to look for &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;sinks&lt;/ins&gt;. We set the value for &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;sink &lt;/ins&gt;types in [[Sims 2 Temp|Temp]] variable zero as that is what the next instruction expects. We use the Global constant for this as [[SimPE]] helpfully shows us the text assigned to the constant which helps us to know we have found the correct value.&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;−&lt;/td&gt;&lt;td style=&quot;background: #ffa; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;&amp;#160; 0x0003: Expression (Temp 0x0000 := Const 0x0106:&lt;del class=&quot;diffchange diffchange-inline&quot;&gt;0x33&lt;/del&gt;), &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;&amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160;  &lt;/del&gt;True Target: 0x0004, False Target: Error&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;background: #cfc; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;&amp;#160; 0x0003: Expression &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt; &lt;/ins&gt;(Temp 0x0000 := Const 0x0106:&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;0x1C&lt;/ins&gt;), &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;&amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &lt;/ins&gt;True Target: 0x0004, False Target: Error&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;&amp;#160; 0x0004: Set to Next (Stack Object ID, obj with category == temp 0), True Target: 0x0005, False Target: 0x0009&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;&amp;#160; 0x0004: Set to Next (Stack Object ID, obj with category == temp 0), True Target: 0x0005, False Target: 0x0009&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;−&lt;/td&gt;&lt;td style=&quot;background: #ffa; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;If we find one, that is our Set to Next instruction returned True, we will calculate the distance between our Sim and this &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;desk&lt;/del&gt;.&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;background: #cfc; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;If we find one, that is our Set to Next instruction returned True, we will calculate the distance between our Sim and this &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;sink&lt;/ins&gt;.&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;&amp;#160; 0x0005: Distance (Temp 0x0000 := My object id .. Stack Object), True Target: 0x0006, False Target: Error&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;&amp;#160; 0x0005: Distance (Temp 0x0000 := My object id .. Stack Object), True Target: 0x0006, False Target: Error&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;−&lt;/td&gt;&lt;td style=&quot;background: #ffa; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;Then we will check if it is closer than the closest one we previously found, which for the first &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;desk &lt;/del&gt;found by the loop it will be. If it isn't closer then we loop back up to look for the next &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;desk&lt;/del&gt;. We loop back to the instruction that sets the type into Temp 0x0000 since we used Temp 0x0000 for something else since we put that value into it, so we need to set it again.&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;background: #cfc; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;Then we will check if it is closer than the closest one we previously found, which for the first &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;sink &lt;/ins&gt;found by the loop it will be. If it isn't closer then we loop back up to look for the next &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;sink&lt;/ins&gt;. We loop back to the instruction that sets the type into Temp 0x0000 since we used Temp 0x0000 for something else since we put that value into it, so we need to set it again.&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;&amp;#160; 0x0006: Expression (Temp 0x0000 &amp;lt; Local 0x0003), True Target: 0x0007, False Target: 0x0003&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;&amp;#160; 0x0006: Expression (Temp 0x0000 &amp;lt; Local 0x0003), True Target: 0x0007, False Target: 0x0003&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;−&lt;/td&gt;&lt;td style=&quot;background: #ffa; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;If it is closer, we save the new closest distance so far into one [[Sims 2 Local|Local]] variable, and the object ID of the object that is this closest one, into another Local variable. Then we loop back to the run the loop again to see if there are any more &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;desks &lt;/del&gt;that might be closer. Note that we always loop back to the instruction that sets the value in Temp 0x0000. In many cases it might be unnecessary, since Temp 0x0000 hasn't changed, but if you call out to any function in the loop, that function may set Temp 0x0000 to something else and then your loop will be looking for a different category next time around. Certainly if you use Temp 0x0000 in your loop (as we do this time) it is essential.&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;background: #cfc; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;If it is closer, we save the new closest distance so far into one [[Sims 2 Local|Local]] variable, and the object ID of the object that is this closest one, into another Local variable. Then we loop back to the run the loop again to see if there are any more &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;sinks &lt;/ins&gt;that might be closer. Note that we always loop back to the instruction that sets the value in Temp 0x0000. In many cases it might be unnecessary, since Temp 0x0000 hasn't changed, but if you call out to any function in the loop, that function may set Temp 0x0000 to something else and then your loop will be looking for a different category next time around. Certainly if you use Temp 0x0000 in your loop (as we do this time) it is essential.&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;&amp;#160; 0x0007: Expression (Local 0x0003 := Temp 0x0000),&amp;#160; &amp;#160;  True Target: 0x0008, False Target: Error&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;&amp;#160; 0x0007: Expression (Local 0x0003 := Temp 0x0000),&amp;#160; &amp;#160;  True Target: 0x0008, False Target: Error&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;&amp;#160; 0x0008: Expression (Local 0x0004 := Stack Object ID), True Target: 0x0003, False Target: Error&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;&amp;#160; 0x0008: Expression (Local 0x0004 := Stack Object ID), True Target: 0x0003, False Target: Error&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;===After the loop===&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;===After the loop===&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;−&lt;/td&gt;&lt;td style=&quot;background: #ffa; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;Once we have exhausted all the &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;desks&lt;/del&gt;, the Set to Next instruction will return false and will jump to the instruction which is going to now use this discovered closest &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;desk&lt;/del&gt;.&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;background: #cfc; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;Once we have exhausted all the &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;sinks&lt;/ins&gt;, the Set to Next instruction will return false and will jump to the instruction which is going to now use this discovered closest &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;sink to get a glass of water&lt;/ins&gt;. &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;First we test whether we found any sink at all, and only if we did do we then push an interaction from that sink [[TTAB]] onto the Sim. Push Interaction always pushes onto the [[Sims 2 Variables#Stack Object|Stack Object]] so we must first set our current Sim into that [[Sims 2 Variables|variable]]&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;−&lt;/td&gt;&lt;td style=&quot;background: #ffa; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;&amp;#160; 0x0009: &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;''Your code here''&lt;/del&gt;&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;background: #cfc; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;&amp;#160; 0x0009: &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;Expression&amp;#160; &amp;#160; &amp;#160;  (Local 0x0004 &amp;gt; Literal 0x0000),&amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; True Target: 0x000A, False Target: True&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;#160;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;background: #cfc; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt; 0x000A: Expression&amp;#160; &amp;#160; &amp;#160;  (Stack Object ID := My object id),&amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; True Target: 0x000B, False Target: Error&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;#160;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;background: #cfc; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt; 0x000B: Push Interaction (Local 0x0004, Literal 0x0001, inherited priority), True Target: True, False Target: Error&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;</summary>
		<author><name>MogHughson</name></author>	</entry>

	<entry>
		<id>http://simswiki.info/index.php?title=BHAV:Find_nearest&amp;diff=47347&amp;oldid=prev</id>
		<title>MogHughson: /* The Loop */</title>
		<link rel="alternate" type="text/html" href="http://simswiki.info/index.php?title=BHAV:Find_nearest&amp;diff=47347&amp;oldid=prev"/>
				<updated>2010-09-25T14:45:51Z</updated>
		
		<summary type="html">&lt;p&gt;‎&lt;span dir=&quot;auto&quot;&gt;&lt;span class=&quot;autocomment&quot;&gt;The Loop&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;table class='diff diff-contentalign-left'&gt;
				&lt;col class='diff-marker' /&gt;
				&lt;col class='diff-content' /&gt;
				&lt;col class='diff-marker' /&gt;
				&lt;col class='diff-content' /&gt;
			&lt;tr style='vertical-align: top;'&gt;
			&lt;td colspan='2' style=&quot;background-color: white; color:black;&quot;&gt;← Older revision&lt;/td&gt;
			&lt;td colspan='2' style=&quot;background-color: white; color:black;&quot;&gt;Revision as of 14:45, 25 September 2010&lt;/td&gt;
			&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 23:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 23:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;Then we will check if it is closer than the closest one we previously found, which for the first desk found by the loop it will be. If it isn't closer then we loop back up to look for the next desk. We loop back to the instruction that sets the type into Temp 0x0000 since we used Temp 0x0000 for something else since we put that value into it, so we need to set it again.&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;Then we will check if it is closer than the closest one we previously found, which for the first desk found by the loop it will be. If it isn't closer then we loop back up to look for the next desk. We loop back to the instruction that sets the type into Temp 0x0000 since we used Temp 0x0000 for something else since we put that value into it, so we need to set it again.&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;&amp;#160; 0x0006: Expression (Temp 0x0000 &amp;lt; Local 0x0003), True Target: 0x0007, False Target: 0x0003&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;&amp;#160; 0x0006: Expression (Temp 0x0000 &amp;lt; Local 0x0003), True Target: 0x0007, False Target: 0x0003&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;−&lt;/td&gt;&lt;td style=&quot;background: #ffa; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;If it is closer, we save the new closest distance so far into one [[Sims 2 Local|Local]] variable, and the object ID of the object that is this closest one, into another Local variable. Then we loop back to the run the loop again to see if there are any more desks that might be closer.&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;background: #cfc; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;If it is closer, we save the new closest distance so far into one [[Sims 2 Local|Local]] variable, and the object ID of the object that is this closest one, into another Local variable. Then we loop back to the run the loop again to see if there are any more desks that might be closer&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;. Note that we always loop back to the instruction that sets the value in Temp 0x0000. In many cases it might be unnecessary, since Temp 0x0000 hasn't changed, but if you call out to any function in the loop, that function may set Temp 0x0000 to something else and then your loop will be looking for a different category next time around. Certainly if you use Temp 0x0000 in your loop (as we do this time) it is essential&lt;/ins&gt;.&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;&amp;#160; 0x0007: Expression (Local 0x0003 := Temp 0x0000),&amp;#160; &amp;#160;  True Target: 0x0008, False Target: Error&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;&amp;#160; 0x0007: Expression (Local 0x0003 := Temp 0x0000),&amp;#160; &amp;#160;  True Target: 0x0008, False Target: Error&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;&amp;#160; 0x0008: Expression (Local 0x0004 := Stack Object ID), True Target: 0x0003, False Target: Error&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;&amp;#160; 0x0008: Expression (Local 0x0004 := Stack Object ID), True Target: 0x0003, False Target: Error&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;#160;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;background: #cfc; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;color: red; font-weight: bold; text-decoration: none;&quot;&gt;&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;===After the loop===&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;===After the loop===&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;Once we have exhausted all the desks, the Set to Next instruction will return false and will jump to the instruction which is going to now use this discovered closest desk.&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;Once we have exhausted all the desks, the Set to Next instruction will return false and will jump to the instruction which is going to now use this discovered closest desk.&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;&amp;#160; 0x0009: ''Your code here''&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;&amp;#160; 0x0009: ''Your code here''&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;</summary>
		<author><name>MogHughson</name></author>	</entry>

	<entry>
		<id>http://simswiki.info/index.php?title=BHAV:Find_nearest&amp;diff=46434&amp;oldid=prev</id>
		<title>MogHughson at 22:43, 11 August 2010</title>
		<link rel="alternate" type="text/html" href="http://simswiki.info/index.php?title=BHAV:Find_nearest&amp;diff=46434&amp;oldid=prev"/>
				<updated>2010-08-11T22:43:01Z</updated>
		
		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;==Overview==&lt;br /&gt;
Here we show a [[SimAntics]] BHAV code snippet that will find the closest desk. It loops round looking for objects of a certain type and calculating the distance between that object and the current Sim. It tests the distance of each object with the previous lowest distance and if closer saves the new distance and the new object ID. Once all objects of the type have been cycled round, we drop out of the loop and make use of the nearest object that we have found.&lt;br /&gt;
&lt;br /&gt;
==Illustrating==&lt;br /&gt;
This code snippet illustrates several [[primitives]]. Use these links to see full details about how to use these primitives in order to recreate the BHAV code below.&lt;br /&gt;
* [[0x0002|Expression (0x0002)]]&lt;br /&gt;
* [[0x001F|Set to Next (0x001F)]] [[obj with category|obj with category == temp 0]] varient&lt;br /&gt;
* [[0x000B|Distance (0x000B)]]&lt;br /&gt;
&lt;br /&gt;
==The BHAV code==&lt;br /&gt;
===Preparatory steps===&lt;br /&gt;
Firstly we start by zeroing out the [[Sims 2 Variables#Stack Object|Stack Object]] so that our loop through all the objects starts from the beginning.&lt;br /&gt;
 0x0000: Expression (Stack Object ID := Literal 0x0000), True Target: 0x0001, False Target: Error&lt;br /&gt;
Then we set up our [[Sims 2 Local|Local]] variables to hold the closest distance so far and the object ID of the closest object of the type we are after that we have found. We set the distance to 9999 (0x270F when displayed in hex) so the first object we find is bound to be closer than that.&lt;br /&gt;
 0x0001: Expression (Local 0x0003 := Literal 0x270F), True Target: 0x0002, False Target: Error&lt;br /&gt;
 0x0002: Expression (Local 0x0004 := Literal 0x0000), True Target: 0x0003, False Target: Error&lt;br /&gt;
===The Loop===&lt;br /&gt;
Now we begin our loop round all the objects of a certain type. We are going to look for desks. We set the value for desk types in [[Sims 2 Temp|Temp]] variable zero as that is what the next instruction expects. We use the Global constant for this as [[SimPE]] helpfully shows us the text assigned to the constant which helps us to know we have found the correct value.&lt;br /&gt;
 0x0003: Expression (Temp 0x0000 := Const 0x0106:0x33),              True Target: 0x0004, False Target: Error&lt;br /&gt;
 0x0004: Set to Next (Stack Object ID, obj with category == temp 0), True Target: 0x0005, False Target: 0x0009&lt;br /&gt;
If we find one, that is our Set to Next instruction returned True, we will calculate the distance between our Sim and this desk.&lt;br /&gt;
 0x0005: Distance (Temp 0x0000 := My object id .. Stack Object), True Target: 0x0006, False Target: Error&lt;br /&gt;
Then we will check if it is closer than the closest one we previously found, which for the first desk found by the loop it will be. If it isn't closer then we loop back up to look for the next desk. We loop back to the instruction that sets the type into Temp 0x0000 since we used Temp 0x0000 for something else since we put that value into it, so we need to set it again.&lt;br /&gt;
 0x0006: Expression (Temp 0x0000 &amp;lt; Local 0x0003), True Target: 0x0007, False Target: 0x0003&lt;br /&gt;
If it is closer, we save the new closest distance so far into one [[Sims 2 Local|Local]] variable, and the object ID of the object that is this closest one, into another Local variable. Then we loop back to the run the loop again to see if there are any more desks that might be closer.&lt;br /&gt;
 0x0007: Expression (Local 0x0003 := Temp 0x0000),     True Target: 0x0008, False Target: Error&lt;br /&gt;
 0x0008: Expression (Local 0x0004 := Stack Object ID), True Target: 0x0003, False Target: Error&lt;br /&gt;
===After the loop===&lt;br /&gt;
Once we have exhausted all the desks, the Set to Next instruction will return false and will jump to the instruction which is going to now use this discovered closest desk.&lt;br /&gt;
 0x0009: ''Your code here''&lt;/div&gt;</summary>
		<author><name>MogHughson</name></author>	</entry>

	</feed>