• News

• Industries

• Learning & Support

• Community

• Marketplace

# Array of enemies

So I am trying to implement a system into my game which causes enemies to slowly raise then come smashing down, my player has an array of enemies, but how do I find out which ones are close to me. I’m not sure how to search the array to find the closest ones, or a number of closest ones.

Do a for each loop to get the distance of all the enemies to the player. Then you can check which enemies are in a certain range to the player.

If you’re doing C++, Rama has a wiki article showing how to use the object and actor iterators to find specific objects and actors. You can filter on enemy actors and look for the ones who are within a certain range. He makes a good case for not keeping your own array because if you do you have to manage their lifespan as well. It’ll mess up garbage collection if you have a reference to an actor which has been marked for destruction.

https://wiki.unrealengine.com/Iterators:Object%26_Actor_Iterators,_Optional_Class_Scope_For_Faster_Search

Thx for the quick replays, and help. Was starting to lose faith in the forums, cause this is the first real answer I have gotten.

Thanks a million guys.

I forgot to add, this is in blueprints btw, and jaywebb, do you know of anything else? Because I did that set up, and every enemy gets affected no matter what range they are

Example of getting the closest enemy

Here is a quick example of finding the closest enemy. In this example, if the player is within 200uu of the enemy, you can destroy the enemy by pressing the “Q” key.

1. Create your Enemy blueprint. ( In my case, I just duplicated the third person character)
2. Inside the Enemy blueprint, get the distance to the player.
3. Inside the Player blueprint, on whatever event ( in my case pressing the Q key ) loop through all the enemy actors and get their distance to the player. From that element (the enemy), do want you want ( In my case destroy)

Hope this helps

Yeah I have already done that, and right before I went to post this I noticed why all my enemy enums were changing because it was before the branch, but they still don’t act right, going to put up some pictures to show you what I mean. For some reason I get a error when trying to upload the ingame picture. So you can see in the blueprints that enemies are suppose to start rising, if within the range, but enemies do completely random things. Sometimes the move way high up, a little up, or the just move up 1 tiny little notch. I will try to get the image uploaded again, nor will they rotate at all.

Getting all actors of a given class isn’t something I’d recommend doing during the course of regular gameplay. You’d be better off compiling the list before the game begins, and modifying it whilst the game is running.

well I was hoping to get an explanation, as to why my enemies do different things firing off the same code. Like I said. The pictures I put up is the enemies movement when getting “Ground Pounded” but some enemies don’t move at all, sometimes they move a little, and sometimes they move a lot

Protip: Get all actors 1 delay second after begin play and store that as array of actors. Then wenn pressing Q use that array instead of a get all. Update that array when needed if actor spawns or vanishes/invalids.

My problem with that, is I don’t know the nodes I already have the enemies in a array, but I don’t know how to search the array to find what enemies are close, or how to remove them from the array when they die.