## Recommended Posts

Hi!

Just a quick question regarding getElementsWithinColShape:

Does the table that results from this function have a particular order?

Example : Assume there are at least 2 elements detected with the function getElementsWithinColShape(x,y), also assume that the colShape is a sphere, and is attached to the player.

Would the resulting table's first element be the one that is the nearest to the player?

Or would the first element of the table be the furthest element from the player?

Or even, would the table's elements not have any particular order? (random?)

Thanks in advance! (And don't hesitate to ask for precision, my English is far for quality!)

It returns an indexed table.

Sorry for the late reply, i was off for a couple of days!

Yes i know it returns an indexed table indeed, my question was more like :

"Does this indexed table has a particular order?

Like, is the nearest objects from the center of the colsphere at index 1 in the table?

Or has the table a random order?"

Sorry i wasn't clear! D:

Thanks though!

Sorry for the late reply, i was off for a couple of days!

Yes i know it returns an indexed table indeed, my question was more like :

"Does this indexed table has a particular order?

Like, is the nearest objects from the center of the colsphere at index 1 in the table?

Or has the table a random order?"

Sorry i wasn't clear! D:

Thanks though!

No, they aren't arranged in an order depending upon distance. However, it's easy enough to sort them in that way.

Example:

```local colShape = createColSphere(0,10,20,50) -- 0,10,20 are x,y,z position, 50 is radius
local elements = getElementsWithinColShape(colShape) -- get elements

table.sort(elements,
function(a,b)
local x, y, z = getElementPosition(colShape)
local x1, y1, z1 = getElementPosition(a)
local x2, y2, z2 = getElementPosition(b)
return getDistanceBetweenPoints3D(x,y,z,x1,y1,z1) < getDistanceBetweenPoints3D(x,y,z,x2,y2,z2)
end
) -- the table is now sorted, elements[1] is the nearest element whereas elements[#elements] is the farthest.
```

## Create an account

Register a new account