Get all children of a level actor?

Hello,

I have been wasting time looking for an answer to a seemingly simple question.

Let’s say I have a simple level hierarchy as shown below

329953-capture.png

Now, using Level Blueprints, I want to get an array of all the actors (including StaticMeshActors) under the selected object (MainParent 1). Basically I want to get Parent2, Child1, Child2 & ChildA in one array.

How do I do this? Sorry if the question is silly.

Thank you

I already have a variable reference to the ‘MainParent 1’ actor in my level blueprint. And I dont need to cast it since its of the base ‘Actor’ Type. And I already tried using ‘Get all child Actors’ node and its returning 0 everytime.

Almost certain there’s no direct way of doing this in BPs, there’s this:

  • Get Attached Actors

329949-screenshot-2.jpg

But you’d need to recursively query in loops since this does not travel the attachment chain but rather returns direct descends only.


Alternatively, have the children inherit a tag from whatever they get attached to. Then you can Get All Actors with Tag.

1 Like

hmm, I see, you have arranged these items in the scene, right?. I know it works if this was done through blueprints. Well what if you convert the parent to a blueprint, like BP_MainParent, and go from there. Perhaps doing the whole hierachy in the blueprint viewport. “Actor” is so generic, perhaps that’s why it doesn’t work.

But I use Level Sequences on these actors. I cant use it if I move these actors into a blueprint

Alright!! So this is the node I’m supposed to be using.

Yes, Like you said, I need to recursively fetch all the children’s children. But at least it works!

Thank you!

HERE
I created a static recursive function in a BlueprintFunctionLibrary so that I can use it through out the project.

TArray<AActor*> UCustomFunctionLibrary::GetAllAttachedActors(AActor* parentActor)
{
    TArray<AActor*> arrayToReturn;

    /*Creating and using an additional temp array since modifying the length of the same array while looping through it will cause a crash*/
    TArray<AActor*> tempArray;
    parentActor->GetAttachedActors(tempArray);

    for (AActor* childActor : tempArray)
    {
        arrayToReturn.Add(childActor);

        for (AActor* secondChildActor : GetAllAttachedActors(childActor))
            arrayToReturn.Add(secondChildActor);
    }
    return arrayToReturn;
}