Better sorting method

Alright I have an issue

Let’s say all these troops wanted to find the closest troop to attack. One way you could do it is for each troop iterate through all the enemy troops and find which one is closest per troop. If you had 30 enemies per group, that would be 900 iterations. Yikes.

Does anyone know any way I could find a way to figure out this issue for each troop without exploding my PC? (30 enemies per groups seems like not that much but there’s gonna be 20 groups lol)

1e51d4325132b570c4b0ba4c4515165d

1 Like

Hmm, maybe you can try CollectionService to help you optimize your performance. But again, it will consume a lot of memory no matter how you set it up. I’m assuming that these troops will have some kind of pathfinding mechanism in order to gap close and hit each other when in range; in that case, that’s some heavy performance and not much can be done. If you’re worried about these types of issues, try doing fewer iterations (or removing those that are unnecessary), or lower the number of troops/groups. Remember to keep the idea of optimization; but if you REALLY need the iterations/groups to be there, then expect noticeable lag in your game (especially when your device heats up).

1 Like

Did you try it yet? 900 iterations actually isn’t that many for just math. 900 iterations is a lot if you’re moving or creating parts or rerendering things for each of those iterations, but you might be in the clear since you’re just doing math.