Spawn Components and Actors from C++ not working in Packaged Build

I’m working on a game for Oculus Quest 2 and am trying to spawn components from C++ and create a Pooling system. The code works fine in PIE, but won’t spawn any components or actors whenever I try to.

The code is supposed to check an array to check whether there’s a vacant object to reuse, and spawn another one if not:

Header file:

UPROPERTY(BlueprintReadOnly, Category = "Pooling|Text")
	TArray<UTextRenderComponent*> PoolTextRenderComps;

UFUNCTION(BlueprintCallable, Category = "Pooling|Text")
	void GetTextFromPool(UTextRenderComponent*& TextComp);

UFUNCTION(BlueprintCallable, Category = "Pooling|Text")
	void AddToTextRenderPool(UTextRenderComponent* TextRenderComp);

Class file:

void APoolingManager::GetTextFromPool(UTextRenderComponent*& TextComp)
{
	
	print("Trying to get Text from Pool...");
	if (PoolTextRenderComps.IsEmpty())
	{
		UTextRenderComponent* SpawnedComp = NewObject<UTextRenderComponent>(this, UTextRenderComponent::StaticClass());
		SpawnedComp->RegisterComponent();
		SpawnedComp->CreationMethod = EComponentCreationMethod::Instance;
		TextComp = SpawnedComp;
		print("Created Component %s", *SpawnedComp->GetName());
		return;
	}
	TextComp = PoolTextRenderComps.Last();
	print("Retrieved component %s", *TextComp->GetName());
	print("Still have %i elements available", PoolTextRenderComps.Num());
	PoolTextRenderComps.Pop();
	
}

void APoolingManager::AddToTextRenderPool(UTextRenderComponent* TextRenderComp)
{
	if (TextRenderComp) {
		PoolTextRenderComps.Add(TextRenderComp);
		TextRenderComp->DetachFromComponent(FDetachmentTransformRules::KeepWorldTransform);
	}
}

These functions are supposed to be called from blueprints whenever they need to:

What am I doing wrong? I’ve put multiple checks to see if the class itself is working and the values retrieved from the function, which seems to be valid until the next node, where the log tells me there are a lot of “Accessed None” errors.

Thanks in advance for your help!

Game istance is for the most part static. You may need to make a static version of your pooling manager in the game game instance for it to work.

In what function in the gameinstance are you initializing the pooler class?

1 Like

I do it on the Instance class:

header:
	UPROPERTY(EditAnywhere, Category = "Pooling")
		TSubclassOf<APoolingManager> PoolingManagerClass;

	UPROPERTY(BlueprintReadWrite, Category = "Pooling")
		APoolingManager* PoolingManager;
class:
	void UTCGameInstance::Init()
	{
		Super::Init();

		if (!PoolingManager) {
			const FVector Location(0.0f, 0.0f, 0.0f);
			const FRotator Rotation(0.0f, 0.0f, 0.0f);
			const FActorSpawnParameters SpawnInfo;
			PoolingManager = GetWorld()->SpawnActor<APoolingManager>(PoolingManagerClass, Location, Rotation, SpawnInfo);
			print("Created Global Owner");
		}

		UE_LOG(LogTemp, Display, TEXT("Inited Game Instance C++"));
	}

But THAT didn’t seem to work as well, so I put it to be spawned manually via Blueprint:

I’m getting a valid pooler and retrieved component.




Do you have the correct game instance setup in your project settings? (It might be set to the default).

1 Like

I do, they all spawn and work in the Editor.

The issue only happens in the packaged build though, which makes it way more annoying to replicate it. :confused:

Ok found a solution. You need to add a slight delay in the init function. The pooler seems to spawn correctly then & the rest works.
Works in shipping pack too.



I don’t think it should, but it worked! I suppose I’ll have to do some stuff around this, but thank you for your help! :smiley:

If you want to implement the init in c++ with a delay then you can use lambda with timers

Most likely the inline option would be the shortest route.

Well, since knowing about the delay and trying the timer in C++, I dug up some more and found this post, where the OP is trying to achieve basically the same as I am, but the World doesn’t exist at the time the Instance Init() is called, which brings me a lot of issues and unwanted errors, so I guess I’ll have to place it somewhere else other than the Instance :confused:

The GameMode or even PlayerController should work just fine for that though, so I’ll just try some stuff and check what works best!


From the life cycle flow chart I would probably to with Game Mode Base => Begin Play
The world should be initialed at that point and it’s a singleton so easier to keep track of…

Putting it into the GameMode worked as a charm! The only issue is that the execution order for some reason in the Editor is different than Packaged, not so much of a hassle, but it’s good to keep that in mind.