Get nearest objects

Wise

Copper
Mod Dev
Messages
60
Likes
11
#1
Is it possible to get all objects of specified type with given range without iterating through all objects in the world?
 

Kronox

Coal
Trusted Dev
Mod Dev
Messages
46
Likes
29
#2
Is it possible to get all objects of specified type with given range without iterating through all objects in the world?
To be honest, I don‘t think so. Of couse you are able to minimize the amout of Objects by referring to the Type e.g.:
Code:
NetObjectManager.GetObjectsOfType<T>()
However, I do not know of a more effective approach :confused:
 
OP
OP
Wise

Wise

Copper
Mod Dev
Messages
60
Likes
11
#3
To be honest, I don‘t think so. Of couse you are able to minimize the amout of Objects by referring to the Type e.g.:
Code:
NetObjectManager.GetObjectsOfType<T>()
However, I do not know of a more effective approach :confused:
Thanks for suggestion, but seems i found a better method:
NetObjectManager.GetObjectsWithin(Vector2 position, float range)

it greatly reduces a number of objects to iterate through
 

Kirthos

Gold
Trusted Dev
Mod Dev
Messages
155
Likes
86
#4
Thanks for suggestion, but seems i found a better method:
NetObjectManager.GetObjectsWithin(Vector2 position, float range)

it greatly reduces a number of objects to iterate through
Thanks for suggestion, but seems i found a better method:
NetObjectManager.GetObjectsWithin(Vector2 position, float range)

it greatly reduces a number of objects to iterate through
Take care of using
NetObjectManager.GetObjectsWithin(Vector2 position, float range)
It iterate on all the objects
Here it's the code of the function, like you can see there is even a commentary saying it's slow to use it.

C#:
public IEnumerable<INetObject> GetObjectsWithin(Vector2 position, float range)
{
    // don't use this, its fairly slow.  O(N)
    return this.objects.Select(pair => pair.Value).OfType<INetObjectPosition>().Where(obj => Vector2.WrappedDistance(obj.Position.XZ, position) <= range).Cast<INetObject>();
}
If you really need to get object inside a radius a lot of time, I think the best solution is to make a dictionary where you store object and the chunk where they are, then you only get the chunk around the position you need to check.
But if you use it less than 1 time/s you can use the same function.
Depending on why you need to do that player will not notice it since it's only server sided.
 
OP
OP
Wise

Wise

Copper
Mod Dev
Messages
60
Likes
11
#5
Take care of using
NetObjectManager.GetObjectsWithin(Vector2 position, float range)
It iterate on all the objects
Here it's the code of the function, like you can see there is even a commentary saying it's slow to use it.

C#:
public IEnumerable<INetObject> GetObjectsWithin(Vector2 position, float range)
{
    // don't use this, its fairly slow.  O(N)
    return this.objects.Select(pair => pair.Value).OfType<INetObjectPosition>().Where(obj => Vector2.WrappedDistance(obj.Position.XZ, position) <= range).Cast<INetObject>();
}
If you really need to get object inside a radius a lot of time, I think the best solution is to make a dictionary where you store object and the chunk where they are, then you only get the chunk around the position you need to check.
But if you use it less than 1 time/s you can use the same function.
Depending on why you need to do that player will not notice it since it's only server sided.
Yeah i know how it works, just wanted do not do it by myself.
Since GetObjectsOfType is working the same way and i need to get objects in range anyway, don't think there is a shoter way to do this.
Calling this only at OnCreate so i suppose that perfomance is not a problem
 
Top Bottom