Assigning actors to Arrays based on distance to Player

I’m kinda new to Arrays and blueprints in general… I have multiple EnemyAI actors in my level and I want only the actor that is within a certain distance from the player to be assigned to a new array. When the player is outside that proximity, the enemy actor is removed from the array.

Basically I’m trying to make a smart system to dynamically switch out actors so I can use their location in the material editor to drive a sphere mask. But just for a max of 2 actors in the array. I’ve tried a few things but cannot find a way to determine the actual index of the actor in range.

You could manually check the distance and add/remove indexes but it will get all tangled up in blueprints. Instead, add a sphere collision to the actor and do this:

If the collision is getting in the way of something else, have a look at setting object collision channels. This will allow you to isolate the objects you want to interact with.

To add to this, you can resize the array to 2 to get rid of the excess actors. If you want the closest two, you can iterate through the resulting array, get distance to actor in question, add the distances to another array and get min of that array, twice.

Not sure what you mean by indexes in this context. If the actor has an identifier, like an int variable, you Cast and get that value.

Awesome thanks! the sphere collision does seem like a simpler approach.