Download

Generic type for component

How would you write code that can handle a generic object type? Would you use a template?

I’m trying to figure out how I’d have a class that can have a member that might be a UStaticMesh, a UStaticMeshComponent, a ULightComponent, etc. I’m guessing the answer is to use a template somehow, but I’m not sure exactly how to structure it. I also wonder if UE has a way of handling this.

Sorry if that’s rambling, I’m still getting my head around this.

Any advice?

You probably just want a property that is of common base class type. For example:


UPROPERTY(...)
USceneComponent* GenericComponent;

How you would instantiate it is another matter. You can’t just do it in the constructor since you obviously need it to vary based on some condition. Depending on what your aim is, I guess you’d want to create the component in either OnConstruction or PostInitComponents/BeginPlay.

Ah! That makes sense. I had been trying to use a UObject but I guess it’s not commony basey enough.

I’m tracking objects already in the scene and mapping them to other objects, so no instantiation needed. :slight_smile: Thanks again!