Custom USceneComponent (or alternative)

I have a cube that derives from Actor and this cube has 6 walls. Now every wall is a StaticMeshComponent but I need to make it a class (or struct) instead so that I can later replace one of those walls with another class (like a class that has a double-door or something). But from what class do I derive?

  • USceneComponent: Does not work: https://answers.unrealengine.com/questions/743194/howto-add-staticmesh-to-scenecomponent.html Either an undocumented feature from UE4 or an UE4-bug. But this would have been the logical choice.

  • AActor: I don’t think that it would be a good idea to make every wall an actor (so 7 actors per cube) for performance reasons and such.

  • UActorComponent: Has no position in the world.

  • UObject: This could in theory work but, I believe that it doesn’t automatically replicate and I can’t store any UStaticMeshComponents in it. It bugs UE4 out. Apparently when you have a UStaticMeshComponent placed in say Actor X and then store a reference to that component in another actor or UObject, UE4 will randomly crash and/or not render it. I think UE4 get’s confused between the pointer and the real object because they are the same in c++ code.

I tried everything I know, nothing seems to work.

You should derive from USceneComponent - I believe any other option is basically incorrect.

I read the question you linked and tested it for myself, and in a new project it seems to just work as I expected. Were you also seeing the same crash?

Thanks I got it to work, my test code:

PrimaryComponentTick.bCanEverTick = false;

	this->SetMobility(EComponentMobility::Static);

	SMC = CreateDefaultSubobject<UStaticMeshComponent>(FName("PleaseDontCrash"));
	SMC->AttachToComponent(this, FAttachmentTransformRules::KeepRelativeTransform);
	SMC->SetMobility(EComponentMobility::Static);
	SMC->SetStaticMesh(ULib::CreateObject<UStaticMesh>(P_WALL_MESH));

But then I have no idea why it crashed before. But I’m not gonna complain, the code is and was good then. Thanks!

P.S. Also make sure to add this line:

UCLASS( ClassGroup=(Custom), meta=(BlueprintSpawnableComponent) )