How to create an object from class?

Hi,

I’ve got a class that is using some components for calculations. Based of the desired behaviour those components can be changed. Therefore the components inherit from an abstract base class.

UPROPERTY(BlueprintReadWrite, EditAnyWhere, Category = Components)
		TSubclassOf<class UBaseMovementAdjustmentComponent> AdjMovementComponent = UDefaultMovementAdjustmentComp::StaticClass();

This component can be changed via blueprint. If not specified a default implementation will be used.

How do I create an object for that I only know it inherits from an abstract class.

I’ve tried the following :

NewObject<UBaseMovementAdjustmentComponent>(AdjMovementComponent->StaticClass());

But it tries to create an instance of the abstract parent class, which ofc fails.

The only solution I’ve found (which is obviously trivial) is to have either:

  • The core functionality moved into the base class, leaving the implementations empty and not making it abstract
  • Using a dummy non-abstract class derived from the base abstract class

But, this setup goes against the design principles of the Unreal framework (in my simple opinion). Essentially the framework is a “greedy” one in the sense that it sees very little use of abstract classes, especially in usage as members. Functionally in game development this is a sensible approach as we’re generally dealing with tangible objects, but I’m sure I’m missing the point somewhere. So the question is really why do you need to start with an abstract class? Additionally, are you aware of the issues you will run into when attempting to use MovementComponents different from the ones declared at the relevant position in the relative hierarchies? That provides headaches for years to come.

Thanks for your reply.
I’m developing a plugin and by making parts of the plugin changeable I want it to be fully customizeable. If the user wants some behaviour changed, e.g. how movement is affected, he just creates a custom class that inherits from my provided abstract base class and sets it via the BP Uproperty.
I assumed a child class can also be seen as a parent class. So i could create objects with the type of the parent but passing the child class as parameter.

I’ve made mistake when creating the Object. The first parameter is an OuterObject but since UClass inherits from UObject that was a valid parameter.

NewObject<UBaseMovementAdjustmentComponent>(this,AdjMovementComponent);

Now it works as intended :slight_smile: