Good way to get all actors of given type in radius around main character?

Hello,

I’m trying to grab/get all of the actors (with a specific tag) around my main player character (perhaps in an array), and then select/grab/manipulate the closest actor with that given tag in C++.

I don’t know if I should do this using physics/collisions with something like a simple sphere collision sweep? I also have seen the “GetAllActorsOfClass” function, but that seems a bit unnecessary for my needs. The only problem I have with sweeping a sphere shape from say above my character downwards to the ground, is that I don’t think the sweep returns an array. It simply grabs the first “hit” object, but not all of the objects within the area correct?

Is there a C++ function that can detect all actors within a given radius around my player location (for example), and that can return an array of actors (or similar behavior)?

Thanks for any help.

Ok I found a solution, not sure if it’s the best one or not. I added a sphere collider component onto my main character and give it a large enough radius to overlap things around it. Then I set both the actors I wanted to detect and my main character to generate overlap events. Then I used the “GetOverlappingActors” function to populate an array of the actors I wanted to detect and from there I can manipulate the actors to do things.

So far it seems to be working well.

2 Likes

I think it’s the best solution :slight_smile:

Adding a collider is great if you need the list of object that are colliding ever tick. You can also turn on and off the collision of the collider as needed.

If you just need to query now and again, you can simply use the function: UKismetSystemLibrary::SphereOverlapActors or UKismetSystemLibrary::SphereOverlapComponents

3 Likes

This works too. Keeps your character BP clean :slight_smile:

Also check out UWorld, which has spatial query methods like UWorld::OverlapMultiByObjectType, which underpin the Kismet functions @clarkelaprairie mentioned.