NewObject using TSubclassOf, with ABSTRACT parent class - How to do it?

Perhaps there is something simple to this I am missing?
I have a parent class UMyAbstractParent, and it is marked as abstract. (Parent is UObject)
I have some instantiable classes which derive from this parent, lets call one UMyConcreteClass
which is blueprintable.

There is a blueprint function which has (TSubclassOf itemType) as parameters.
The function basically wants to do this:

UMyAbstractParent* newItem = NewObject<UMyAbstractParent>(itemType);

Obviously we are never going to try to instantiate the abstract base class, only derived concrete classes…
HOWEVER, this line of code as written dies, because the engine asserts that the templated class is abstract.

All the versions I can find of NewObject are templated ones, and I was unable to find a way of passing a flag or something to get it to skip the assertion.

Is there another/better/accepted way to do this that i don’t know about?
I would really like to keep the base class as abstract, because it makes sense.
And I really don’t want to have to do something hacky, like implement a concrete intermediate class that the abstract base class derives from, just to feed something ‘acceptable’ to the NewObject() function.

What you want to do will work, you just passed in the item type as the wrong parameter. The subtype parameter should be the second one. The first parameter is the desired Outer (or owning object). If you don’t care you can call GetTransientPackage(). So the code you want is:

UMyAbstractParent* newItem = NewObject<UMyAbstractParent>( GetTransientPackage( ), itemType );
1 Like

You are of course completely correct!
All I can do is put it down to an all-night-coding/copypasta incident.

Thank you so much for taking the time to respond :grinning:

1 Like