Can't properly use Physics2D.OverlapCollider unless I know how many collisions there will be?

I’m attempting to use Physics2D.OverlapCollider to test how many objects are within a collider.
OverlapCollider’s contructor looks like this:

public static int OverlapCollider(Collider2D collider, ContactFilter2D contactFilter, Collider2D[] results);

the results variable is the array which will be given the results of the test. The problem is, if the array isn’t large enough to hold the results or if it is null, the array will not be given the results. What is the logic behind this? The docs say this:

“The integer return value is the number of colliders that overlap the collider and which could be stored in the supplied array given its length. The results array will not be resized if it doesn’t contain enough elements to report all the results. The significance of this is that no memory is allocated for the results and so garbage collection performance is improved when the check is performed frequently.”

So if I do not know how many colliders are going to be within my testing collider I am forced to make the array extremely large so that it can be occupied with the results from the test. This seems super inefficient. I don’t even know when there would even be a case where this method is applicable without just making the result array extremely large. The only time you wouldn’t need to over estimate the array’s size is if you knew exactly how many colliders were going to within your test collider, and at that point you probably wouldn’t need to perform this test in the first place. So whats the point?

In my setup I have no idea how many colliders are going to be within the test collider but there could be anywhere between 1 and possibly thousands. Here’s my current code that I’ve been forced to go with:

            ContactFilter2D newContactFilter = new ContactFilter2D();
            newContactFilter.layerMask = nodeMask;
            newContactFilter.useLayerMask = true;
            Collider2D[] hitNodes = new Collider2D[9999];
            Physics2D.OverlapCollider(newCaptureRegion.polygonCollider, newContactFilter, hitNodes);

Am I misunderstanding something here? Maybe I’ve overthought this problem and making an array of that size really isn’t going to slow my code down all that much? Seems counter intuitive to have to use this method in an ungarbage-colleciton friendly way when the method is apparently designed to be MORE garbage-collection friendly. Thanks!

You could do a test, but I think it is saying it will fill the array but not make a new one to hold the colliders that don’t go in it. You could probably do a test yourself and resize it if it was getting near capacity. It returns the number of the results that were in the collider, so it would be pretty easy to do.

I think the idea is that you make the array once, and can reuse the same array every time you need to call OverlapCollider. So you shouldn’t actually need to garbage collect for that array until the instance of that script gets destroyed. You should have an idea of the max possible value based on the size of the collider you are checking compared to the size and number of other colliders in your game.

I have doubts that this method was written with the intention of returning thousands of colliders.

No I tried that it will only return an int of the size of the array or smaller if there are less colliders than can occupy the array.
So if your array has a length of 10 and it returns 3 colliders it returns 3. If your array has a length of 10 and it hits 20 colliders it will only return 10.

I don’t think it’s true to say that if you knew how many could be inside it, then you wouldn’t have to use the method. I mean, imagine you knew 3 objects could be inside… that means they could also not be inside; that sounds perfectly logical to me. :slight_smile:

In any event, if you truly need space for thousands… be sure to re-use the array, rather than creating a new one. Be sure to check only the number returned by the method, too, if you’re processing them in some way.

Hope that helps a little. :slight_smile:

You’re definitely right that the method does have some use cases. Maybe I was being a bit dramatic haha but it does seem like it’s use cases become much narrower because of the way it’s structured? Maybe I’m not quite realizing the performance benefits of it being this way though. I’m not too sure. And I might not truly need thousands, but definitely possible to have several hundred so I guess I’ll just reuse a single array with a length around a thousand. Thanks again! :slight_smile: