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
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.
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.
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.
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;
}