AActor::GetAttachedActors gives empty array in shipping build?

I’m trying to get the actors attached to one actor in the editor hierarchy using AActor::GetAttachedActors (note: not child actor components). I noticed you can’t do this in BeginPlay, you have to add a timer for example and wait a few frames before the function works. However, it doesn’t seem to work in a shipping build at all.

Is there some build configuration setting I have to add or something?

Definitely do not do this in an actors BeginPlay. You can’t be sure that your “main” actor begins play after your other actors begin play. If you have to add a timer before running a function, there’s usually something wrong. Using a timer in the way you’re doing here will give you a lot of issues when working with different system setups.

Can you explain what you are trying to achieve?

I’m trying to go through the children of an actor and hide some of them if their name contains a certain string. This has to be done at the start of a level behind a loading screen.

Here’s my code:

in BeginPlay:

if (bScanChildObjectNames)
{
	GetWorld()->GetTimerManager().SetTimer(HideTimer, this, &UBackgroundObjectActivator::DoChildObjectScan, 2.5f, false);
}

The function called by the timer:

void UBackgroundObjectActivator::DoChildObjectScan() 
{
	TArray<AActor*> Actors = TArray<AActor*>();
	GetOwner()->GetAttachedActors(Actors, false, true);
	Actors.Add(GetOwner());
	FString Apartment = Cast<UPelimalliGameInstance>(UGameplayStatics::GetGameInstance(GetWorld()))->GetCurrentApartment().Identifier;
	for (AActor* Actor : Actors)
	{
		FString Name = Actor->GetActorNameOrLabel();
		if (Name.Contains(Apartment))
		{
			for (UActorComponent* Comp : Actor->GetComponentsByClass(UStaticMeshComponent::StaticClass()))
			{
				Cast<UStaticMeshComponent>(Comp)->SetVisibility(false);
			}
		}
	}
}

I tried using GetWorld->bActorsInitialized in tick function to check if actors are initialized and then call the function but still the array given to GetAttachedActors remains empty in a shipping build. In editor and development build it works fine.

Sorry about the slow response.

You’ll likely want to take a look at this:

There are more graphs like this. What is probably causing this problem is that a standalone game and the editor boots up differently. This is true for actor lifecycle as well.

How do you attach your child actors to your parent actor? I’m assuming you’re doing it during BeginPlay of the child actors.
If you try to do this in your child actors BeginPlay, then it won’t work to call your function based on “UWorld->bActorsInitialized”. Actors are Initialized before they BeginPlay, and if you try to do it in your parent Actor’s BeginPlay, then you can’t be certain that all child Actors have begun play either.

You should take a look at “AGameMode”. It contains functions and properties that track if the game is running. Especially take a look at GameMode::StartMatch(). When this function is fired, the game transitions from “WaitingToStart” to “InProgress”. This is when all actors BeginPlay is fired.

If you want you can share when your child actors are attached to parent, then I (or someone else) can suggest where to call your DoChildObjectScan function. For now I suggest you look at GameMode.

Edit: I just want to point out that the “issue” here isn’t that it’s not working in a shipping build. That is just a symptom. The actual cause is likely how and when you call your functions. It might seem really obvious, but if you want something to run at the start of your game, then that code should originate from something that runs when the game starts, whether that is a callback, a simple function call or whatever.

Got it solved. It didn’t have anything to do with execution order or such anyway. Apparently the names the actors have (or rather labels I think they are called) in the world outliner don’t persist into a shipping build. The actual names are just “Actor_123” etc which I can’t use to find the actors I want. So I made a little editor utility widget that adds the labels to the tags of the actors and use that to find the actors.

Thank you for the tips anyway.