Access Violation with Render Target Asset when using FObjectFinder

anonymous_user_89fb8479
Tue, 28 Nov 2017 05:49:34 +0000
Rendering

I’m having trouble trying to load and access a Render Target asset from the content browser in my C++ class. Loading the asset seems to work fine using ConstructorHelpers::FObjectFinder in the constructor of the class, but when I try to access it later I get an access violation error. What exactly is going wrong with my code?

Here is the code in the class’ constructor:

// Initialize Root Component
RGBCameraRoot = CreateDefaultSubobject<USceneComponent>(TEXT("RGBCameraRoot"));
RootComponent = RGBCameraRoot;

// Initialize Camera
RGBCamera = CreateDefaultSubobject<USceneCaptureComponent2D>(TEXT("RGBCamera"));
RGBCamera->AttachToComponent(RGBCameraRoot, FAttachmentTransformRules::SnapToTargetNotIncludingScale);

// Initialize Render Target
ConstructorHelpers::FObjectFinder<UTextureRenderTarget2D> RenderTargetAsset(TEXT("/Game/Camera/Cap.Cap"));
RenderTarget = RenderTargetAsset.Object;

// Initialize Texture
Texture2D = CreateDefaultSubobject<UTexture2D>(TEXT("RGBTexture"));

// Set Camera Texture Target as Render Target
RGBCamera->TextureTarget = RenderTarget;

And here is my BeginPlay(), where I get the access violation error, specifically on the line where I attempt to construct a Texture2D out of the Render Target :

// Called when the game starts or when spawned
void AMyRGBCam::BeginPlay()
{
	Super::BeginPlay();
	RGBCamera->TextureTarget = RenderTarget;
	Texture2D = RenderTarget->ConstructTexture2D(this, FString("Texture2D"), EObjectFlags::RF_NoFlags);
}

Am I perhaps not using FObjectFinder correctly? I’m new to Unreal Engine, and I’d really appreciate the help!

1 post - 1 participant