UAtmosphericFogComponent Fails When Used Within AActor Derived C++ Class

Reproduction

  1. Create a new C++ project in Unreal Editor.
  2. Add a New C++ Class to the project, derived from AActor
  3. Set the header and source as shown below
  4. Drag the actor into a new Empty Level

MyActor.h

#pragma once

#include "GameFramework/Actor.h"
#include "MyActor.generated.h"

UCLASS()
class MYPROJECT_API AMyActor : public AActor
{
	GENERATED_BODY()

public:	
	AMyActor(const FObjectInitializer& ObjectInitializer);

    UPROPERTY()
	UAtmosphericFogComponent* AtmosphericFogComponent;
};

MyActor.cpp

#include "MyProject.h"
#include "MyActor.h"

AMyActor::AMyActor(const FObjectInitializer& ObjectInitializer)
	: Super(ObjectInitializer)
{
	AtmosphericFogComponent = ObjectInitializer.CreateDefaultSubobject<UAtmosphericFogComponent>(this, "AtmosphericFogComponent");
}

The atmospheric fog should fail to appear. However, dragging the atmospheric fog from the left toolbar and placing it in the level works, and adding an atmospheric fog component to a blueprint and then dragging that into the level works. Only when adding it to a C++ AActor derived class does it fail.

System Specs:

Windows 10

Visual Studio 2015

Intel i5 Processor

GTX 780 Graphics Card

Okay, it turns out you need to override the following method for it to work:

In your class header:

#if WITH_EDITOR
	virtual void PostActorCreated() override;
#endif

In your class source:

#if WITH_EDITOR
// Prepare render targets when new actor spawned
void AMyActor::PostActorCreated()
{
	Super::PostActorCreated();
	if (GIsEditor)
	{
		if (!IsTemplate() && AtmosphericFogComponent)
		{
			AtmosphericFogComponent->InitResource();
		}
	}
}
#endif

This is because you must call InitResource() on the AtmosphericFogComponent.

I’m closing this report.