Home   Help Search Login Register  

Author Topic: Nearest object from an array  (Read 1632 times)

0 Members and 1 Guest are viewing this topic.

Offline Rellikki

  • Members
  • *
  • Die-hard OFP player
Nearest object from an array
« on: 24 Aug 2009, 17:04:43 »
I was wondering how do I return the nearest object to a given position from an array of objects?
Thanks in advance.

Offline Hawke

  • Members
  • *
Re: Nearest object from an array
« Reply #1 on: 24 Aug 2009, 17:18:14 »
Are you asking how to get the postions of the nearest objects?

Offline Rellikki

  • Members
  • *
  • Die-hard OFP player
Re: Nearest object from an array
« Reply #2 on: 24 Aug 2009, 18:23:58 »
Well, already explained to you in MSN, but for anyone else wondering - Basicly I have an array of different objects and I want to know which one of them is the closest one to, for example, the player. Simple as that.

Walter_E_Kurtz

  • Guest
Re: Nearest object from an array
« Reply #3 on: 24 Aug 2009, 19:11:11 »
It's a bit higher-level than I'm comfortable with, but I'd start with General Barron's findunits.sqf.

First, add the following to your init.sqs
findUnits = preprocessFile "findUnits.sqf"

You need to input:
 - the unit to which this applies
 - the array of units to be checked
 - this distance they need to be within; make this very large, but can be reduced according to your need
 - whether you want them sorted by distance; set this to true

I think this should work with all sorts of objects, not just units; for instance, if your array is Pubs then
nearestPubs = [player, 1000, Pubs, true] call findUnits

"nearestPubs select 0" is the closest pub to the player. Mine's a Weissbier - it's Summer.

Offline Hawke

  • Members
  • *
Re: Nearest object from an array
« Reply #4 on: 24 Aug 2009, 20:28:58 »
Code: [Select]
; ****************************************************************
; Script file for Armed Assault
; Created by: TODO: Author Name
; ****************************************************************

Hint "Please Stand Still While We Find The Nearest Unit"

;//////////////////////////
;Edit _array to your array list E.g. _array = [[unit1],[Banker],[Civ],[Mobster]]
_array = []

_max = (count _array)
_num = 0
_nearest = [Defalt,99999999999999999999999999999]
~2
;Hint Format ["%1 units are in the array",_max]
;/////////////////////////

#if
;////Check if the _num has reached the same number of units in the array
?(_num > _max): goto "next"
~1
_unit2 = _array select _num
_unit = _unit2 select 0
_dist = player distance _unit
;Hint Format ["Distance: %1 Name: %2",_dist,(name _unit)]
;////////////////////////////////////






?(_dist <= (_nearest select 1)): goto "new"

_num = _num + 1
goto "if"



;///First Nearest//////
#1
_nearest = [_unit,_dist]
;Hint Format ["First   %1 ",_nearest]
~2
_num = _num + 1
goto "if"

#New
_nearest = [_unit,_dist]
_num = _num + 1
goto "if"

#if2
_num = _num + 1
goto "if"


#next
?(_num == _max): goto "if2"
Hint "Nearest Unit Found!"
~1
_pos = getpos (_nearest select 0)
player setpos _pos
Hint "Done!"
Exit

Hope it helps  :)
« Last Edit: 25 Aug 2009, 04:41:17 by Hawke »

Offline Rellikki

  • Members
  • *
  • Die-hard OFP player
Re: Nearest object from an array
« Reply #5 on: 24 Aug 2009, 21:08:56 »
Thanks Hawke, that script seems to work perfectly, apart from the missing ) in line 34. ;)

Thanks for your advice too, Walter, but I'm not much into SQF, to be honest. I find it too complicated.

Edit #1: Well, another problem... How do I remove certain elements from the array later in the mission, so they would be disregarded from the script then? I tried the command below, which didn't seem to have any effect at all...

Code: [Select]
array = array - [element1]
I checked the amount of elements in the array before and after with a "hint format" command and it was always the same, nothing was changed.
I changed the _array in the script to a global array which is initilialized in init.sqs, so elements could be later removed from it...

Edit #2: According to Biki, nested arrays cannot be substracted. However, I got an idea out of nowhere to rewrite the script a little, to avoid those nested arrays. Here's what I came up with:

init.sqs
Code: [Select]
array = [element1,element2,element3]
script.sqs
Code: [Select]
_unit = _this select 0
_max = count array
_nearest = array select 0
_num = 0
_dist = array select 0 distance _unit

#check
?(_num > _max): goto "nearest"

_newdist = _unit distance (array select _num)
? _newdist < _dist: _nearest = array select _num

_num = _num + 1
goto "check"

#nearest
_pos = position _nearest
_unit setPos _pos
exit

This script seems to work as good and the elements can be removed from the array too without any problems. Thanks to Hawke for the reference from the original script. ;) I learned a lot from it.

Problem solved.
« Last Edit: 26 Aug 2009, 16:09:02 by Rellikki »

Offline Raptorsaurus

  • Editors Depot Staff
  • *****
Re: Nearest object from an array
« Reply #6 on: 09 Oct 2009, 08:53:06 »
I made a function that finds all the objects of an array of types within a given distance.