Evenly distribute/split homing projectiles between multiple enemies

depending on how often you are doing this C++ might not be that much of an improvement, and it can be speed up considerably still by using “Get Squared Distance” (just keep in mind the distance limit would need to be squared to compensate) this way so save having to do the most expensive part of distance checks being the square roots.

once the array is sorted (or even during the sort) anything outside the range would be removed. the reason to suggest C++ for array sorting is to define a sorting predicate, but by nature of sorting predicates they can not trim/remove. and trimming would make sorting faster, but we come back to how often this is going to be happening.

if you don’t want to go into C++ just don’t have this be usable too often. get all Actors of Type then walk through the array doing a distance check if greater then distanceMax remove them.

then you would sort the Array (this is where C++ you could define a sorting predicate, but) this is doable in Blueprints as long as you don’t do this “to often” you can look here for implementing a sort How to sort an array? in blueprints.

for the distribution system given your criteria, have a temp integer equal to you ProjectilesMax, then there are a couple of options depending on code flow if if(Array.Length >= ProjectilesMax) then just use a loop
if(Array.Length < ProjectilesMax) then you need a bit more

the getting the array could be cleaned up by using a manager to keep track of your Entities, and you could cash the distances on the first traversal to save on math for each compare, but these are optimizations.

other optimizations after it is functional:

  • object pooling so you don’t have to create and destroy these objects all the time which can lead to segmentation
  • replace the top for loop with maybe a Do N where the target index would also be the CurrentN (only do this if the Length is less then the MaxProjectiles) this might require unit testing to determine if it is faster.
  • do these need to be physical objects or could they be just a visual effect after a trace
1 Like