I have a custom AActor class AStreet that creates an instance of a custom USceneComponent class UPath in its constructor (it also creates instances at runtime, but that doesn’t seem to cause any errors).
AStreet::AStreet(const class FObjectInitializer& ObjectInitializer)
: Super(ObjectInitializer)
{
// If Path is added here, blueprint object cannot be saved anymore.
AddPath();
}
The function creating the instance of Path in AStreet also stores a reference to the Path object in a TArray. This ‘add to the array’ causes the following error message when I try to save the blueprint based on class AStreet:
Can’t save
…/…/…/…/…/…/Users/IgorIII/Documents/Unreal
Projects/GraphLinkError/Content/BP_Street.uasset:
Graph is linked to private object(s)
in an external package. External
Object(s): Path
If I don’t add the Path object to the array, the error doesn’t show.
bool AStreet::AddPath()
{
// Either of the two versions produce the same result
//UPath* path = ConstructObject<UPath>(UPath::StaticClass(), this, TEXT("Path"));
UPath* path = NewNamedObject<UPath>(this, TEXT("Path"));
// If the path is not added to the array, the Street Blueprint can be saved
Paths.Add(path);
path->AttachParent = RootComponent;
return true;
}
I’m not sure if this is a bug, or if it’s just a basic misunderstanding of how object instantiation, object references and garbage collection works (I suspect the graph link error is caused by a reference in Paths not being found anymore).
If I use AddPath() at runtime (not in the AStreet constructor, but for example in BeginPlay), everything works fine.
If I create the UPath object via FObjectInitializer (from the constructor), I can save the blueprint, but I get a whole bunch of crashes once I try to place an object based on this blueprint in the world.
How do I properly add a component at runtime? How do I properly add a component in the constructor? Why can’t I store a reference to the sub-component in an array?