How to create an array of sub objects?

As in topic, I know how to create a sub object for an object:

UPROPERTY( EditAnywhere, Instanced )
MyObject* m_obj;

and in constructor:

m_obj = ObjectInitializer.CreateDefaultSubobject< MyObject >( this, TEXT( “OBJ” ) );

I think this is correct ( ? )

But I have no idea how to create an array of objects that user can expand in editor and add new objects and fill their data, so I would like to have something like:

UPROPERTY( EditAnywhere, Instanced )
TArray< MyObj* > m_obj;

But then, how do I call the CreateDefaultSubObject? Is there any way to do it?

You need to mark your MyObj as EditInlineNew in the UCLASS/USTRUCT declaration if you want them to be able to instantiate the object within the editor property panel. You already have the Instanced flag set on your property, so that should be everything.

If you are expecting the user to create/fill those objects, then you don’t have to do anything. If you want to manually fill that data, then it’s the exact same process, just with adding it to the array at the end:

MyObj* ANewObject = ObjectInitializer.CreateDefaultSubobject< MyObject >( this, TEXT( "SubObjectA" ) );
m_obj.Add(ANewObject);

// Rinse repeat for however many objects you want to add.

EditInlineNew is exactly what I needed, thanks.

Out of curiosity for the future - you say that I can create the object manually and append at the end of the array. Is there any callback from editor, that I get, when somebody presses “+” in an array - so that I can manually spawn a new object at the end of array? Otherwise, I don’t see a way to do this without writing custom editor I guess :frowning:

Yes. See this post.

I realize this is a really old post, but hoping for some help on this same scenario in UE5. I’m trying to instantiate an array of USplineComponents within BeginPlay(). In my Actor’s .h I declare

public:

    UPROPERTY(Instanced)
    TArray<USplineComponent*> Splines;

In the Actor’s .cpp

void AMyActor::BeginPlay()
{
    ...
    USplineComponent* spline = CreateDefaultSubobject<USplineComponent>(TEXT("SomeSpline"), false); // tried true here as well
}

When I PIE, UE5 crashes in CoreUObject.dll. I’ve tried subclassing USplineComponent with

UCLASS(EditInlineNew)
class KUIPER_API UInstancedSplineComponent : public USplineComponent
{
	GENERATED_BODY()
	
};

but it still crashes. I don’t need the spline components exposed in the editor, I just need to generate them dynamically during gameplay.

EDIT:
I was able to fix this by using

USplineComponent* spline = NewObject<USplineComponent>(this, TEXT("SomeSpline"));

instead of CreateDefaultSubobject

CreateDefaultSubobject can only be used in a constructor since it uses the current FObjectInitializer. If there is no such initializer because no constructor is currently executing you crash (due to a failing assertion).