Download

Runtime-Created Render-Target causes 'Fatal Crash'?

The following code works perfectly when using PIE, but in Standalone it instantly crashes the game, and just gives the message ‘Fatal Error’ (Doesn’t even trigger the Unreal Crash Reporter).

Any ideas why? I can’t see anything here that should cause a crash…



ABZGame_TargetCapture::ABZGame_TargetCapture(const FObjectInitializer& ObjectInitializer) : Super(ObjectInitializer)
{
	RenderArm = ObjectInitializer.CreateDefaultSubobject<UBZGame_AssignableSpringArm>(this, TEXT("RenderArm"));
	RenderArm->TargetArmLength = 300.0f;
	RenderArm->bAbsoluteRotation = true;

	RootComponent = RenderArm;

	RenderCapture = ObjectInitializer.CreateDefaultSubobject<USceneCaptureComponent2D>(this, TEXT("RenderCapture"));
	RenderCapture->bCaptureEveryFrame = true;
	RenderCapture->AttachTo(GetRenderArm(), UBZGame_AssignableSpringArm::SocketName);
	RenderCapture->SetRelativeLocationAndRotation(FVector::ZeroVector, FRotator::ZeroRotator, false);
	RenderCapture->FOVAngle = 80.0f;

	RenderTarget = ObjectInitializer.CreateDefaultSubobject<UTextureRenderTarget2D>(this, TEXT("RenderTarget"));
	RenderTarget->ClearColor = FLinearColor::Black;
	RenderTarget->InitAutoFormat(256, 256);

	if (RenderTarget && RenderCapture)
	{
		GetRenderCapture()->TextureTarget = GetRenderTarget();
	}
}

void ABZGame_TargetCapture::SetInitialParams(UMaterialInstanceDynamic* TargetMaterial, FName ParamName, uint32 CaptureSizeX, uint32 CaptureSizeY, UBZGame_GameObjectComponent* TargetObject, AActor* InRefActor)
{
	/* Asserts To Check If Params Are Valid */
	ASSERTV(TargetMaterial != NULL, *FString::Printf(TEXT("Target Capture Actor %s Has No Material To Update!"), *GetName()));
	ASSERTV(ParamName != NAME_None, *FString::Printf(TEXT("Target Capture Actor %s Material Parameter Name Is NULL"), *GetName()));
	ASSERTV(CaptureSizeX != 0, *FString::Printf(TEXT("Target Capture Actor %s Zero Size on 'X'"), *GetName()));
	ASSERTV(CaptureSizeY != 0, *FString::Printf(TEXT("Target Capture Actor %s Zero Size on 'Y'"), *GetName()));
	ASSERTV(TargetObject != NULL, *FString::Printf(TEXT("Target Capture Actor %s Has no Target Object!"), *GetName()));
	ASSERTV(InRefActor != NULL, *FString::Printf(TEXT("Target Capture Actor %s Has no Reference Actor!"), *GetName()));
	
	ReferenceActor = InRefActor;

	AActor* OwningActor = TargetObject->GetOwner();
	if (OwningActor)
	{
		AttachRootComponentToActor(OwningActor);
		RenderArm->SetAttachedActor(OwningActor);
		RenderArm->SetRelativeLocation(FVector::ZeroVector);
		RenderArm->TargetArmLength = TargetObject->GetTargetCameraLength();
		RenderArm->bDoCollisionTest = false; // Temporary

		if (RenderTarget)
		{
			RenderTarget->SizeX = CaptureSizeX;
			RenderTarget->SizeY = CaptureSizeY;
			RenderTarget->UpdateResourceImmediate(false);

			TargetMaterial->SetTextureParameterValue(ParamName, GetRenderTarget());
		}
	}
}


Update: Fixed it. It wasn’t this causing the crash, it was a Screen Percentage CVar that isn’t accessible at runtime (though a nicer error message would have been better).