In case you are having saving issues "Graph is linked to external private object" from creating objects in data assets

I’m creating this thread (mainly for future me, but for anybody else unfortunate enough to get this issue) just to give ways about how I solved this issue (well, twice).

Problem:
I created a UObject on runtime for one of the variables in a C++ data asset. I created a data asset from that in the editor, saved, duplicated it and renamed it, and tried to save again. I wasn’t allowed to save due to getting an error about “Graph is linked to external private object”.

Solutions:

  1. In my first occurence, in the initializer, i was creating it like so:
MyCreatedObject = NewObject<UMyObject>(this,DesiredObjectClass.Get());

Don’t do that, this resulted in the object not being able to handle deep duplication of said data asset.

Instead, do this:

MyCreatedObject= Cast<UMyObject>(ObjectInitializer.CreateDefaultSubobject(this, TEXT("MyCreatedObject"), DesiredObjectClass.Get(), DesiredObjectClass.Get()));

And ensure that the constructor is from the ObjectInitializer, like so:

UMyDataAsset::UMyDataAsset(const FObjectInitializer& ObjectInitializer)
	:Super(ObjectInitializer)
{
MyCreatedObject = Cast<UMyObject>(ObjectInitializer.CreateDefaultSubobject(this, TEXT("MyCreatedObject"), DesiredObjectClass.Get(), DesiredObjectClass.Get()));
}

Note: THIS ONLY WORKS IN THE INITIALIZER.

  1. In my second occurence, it wasn’t in the initializer (it was in the PostEditChangeProperty function) , and was like so:
MyOtherCreatedObject = NewObject<UMyOtherObject>(GetOuter(),OtherDesiredObjectClass.Get());

Don’t do this, the outer ends up being incorrect. However, since you’re not in the initializer, you can’t use the above method.
The actual issue is the GetOuter, I should be using GetOutermostObject, like this:

MyOtherCreatedObject = NewObject<UMyOtherObject>(GetOutermostObject(),OtherDesiredObjectClass.Get());

I’ll be honest and say I’m not 100% certain about the exact reasons why this occurs (i’m still very new to C++ in UE), but I couldn’t find any answers online about this and it was really frustrating, so hopefully this would help anybody else in the same situation (or maybe me in the future if i forget again).

Also small note, people have been talking about using NewNamedObject, and even the official documentation mentions this.
I’M PRETTY CERTAIN THIS DOES NOT EXIST IN 5.2.0 (and I doubt in the future releases as well). I tried searching the entire solution and online and couldn’t find out what file to include to make it work.