Download

Class causes editor to crash when deleting it form a BP and/or recompiling the BP

I’ve got an atmosphere class that contains pointer to World Light, Atmosphere Fog, Exponential Height Fog and Audio Component:

header:


	UPROPERTY(EditAnywhere) UDirectionalLightComponent* WorldLight = nullptr;		
	UPROPERTY(EditAnywhere) UAtmosphericFogComponent* AtmosphereFog = nullptr;		
	UPROPERTY(EditAnywhere) UExponentialHeightFogComponent* HeightFog = nullptr;	
	UPROPERTY(EditAnywhere) UAudioComponent* AmbientSound = nullptr;	

cpp:


    AAtmosphere::AAtmosphere()
    {
     	PrimaryActorTick.bCanEverTick = false;
    
    	AmbientSound = CreateDefaultSubobject<UAudioComponent>(FName(TEXT("Ambient Sound")));
    	WorldLight = CreateDefaultSubobject<UDirectionalLightComponent>(FName(TEXT("World Light")));
    	AtmosphereFog = CreateDefaultSubobject<UAtmosphericFogComponent>(FName(TEXT("Atmosphere Fog")));
    	HeightFog = CreateDefaultSubobject<UExponentialHeightFogComponent>(FName(TEXT("Height Fog")));;
    }

This class is used as a child to my AtomsphereController class.

header:


        UPROPERTY(EditAnywhere)    	AAtmosphere* Atmosphere = nullptr;
        APlayerController* MyController = nullptr;	

cpp:



    AtmosphereController::UAtmosphereController()
    {
    	bWantsBeginPlay = true;
    	PrimaryComponentTick.bCanEverTick = false;		
    	Atmosphere = CreateDefaultSubobject<AAtmosphere>(FName(TEXT("Atmosphere")));
    }

Now, this AtmosphereController is attached to an actor in my game that is suppose to have control over the atmosphere. If I attach it to this blueprint, I can safely compile ONE time. At this point, the game will run fine in the editor (PIE), and execute as expected. BUT, if I try to compile the BP a second time (no changes, just a mere click of the Compile button) or delete the AtmosphereController from the BP… insta-crash. I know it has something to do with the AtmosphereController’s call to CreateDefaultSubobject, because I can comment it out, recompile the game, and then delete it from my BP. But once I uncomment, and recompile my BP for a second time, it starts crashing again.

Can someone explain WHY this is causing such an awkward side effect?

I don’t think Actors (any class deriving from AActor) can be spawned in a constructor, and therefore cannot be used with CreateDefaultSubobject either. Instead you should probably spawn the AAtmosphere in BeginPlay or consider placing an AAtmosphere instance in the level editor.

My atmosphere class DOES actually spawn this way. If I run PIE, my wield gets light and fog (haven’t tested the audio component, but I would assume it dies as well). But I WILL go a ahead and try to move the atmosphere into a spawn actor call from begin play. Thank you for the suggestion.