Download

Custom USceneComponent in OnConstruction + CreateDefaultSubobject = Boom

Hello,

I’m trying to attach a custom component to any/all sockets on a mesh during the OnConstruction event in C++. Here’s my OnConstruction method…


void AVehiclePart::OnConstruction(const FTransform & Transform)
{
	Super::OnConstruction(Transform);

	// Bring in and set the StaticMeshAsset from the defaults
	StaticMeshComponent->SetStaticMesh(StaticMeshAsset);

	// Query socket tags, add Ports accordingly
	if (StaticMeshComponent->HasAnySockets())
	{
		// Grab a list of all the sockets on the static mesh
		TArray<FComponentSocketDescription> Sockets;
		StaticMeshComponent->QuerySupportedSockets(Sockets);

		// Nuke any VSockets
		VSockets.Empty(Sockets.Num());

		// Itterate over the list
		for (int32 i = 0; i < Sockets.Num(); ++i)
		{
			// Skip any non valid socket types
			if (Sockets*.Type != EComponentSocketType::Socket) continue;

			// Grab the reference to the socket by name
			const UStaticMeshSocket * Socket = StaticMeshComponent->GetSocketByName(Sockets*.Name);
			
			FString name = Sockets*.Name.ToString();
			name = Socket->GetName();

			UE_LOG(LogTemp, Warning, TEXT("%s"), *(name));

			// Make sure our query returned an actual socket
			if (Socket == nullptr) continue;

			// Create a new VSocket to attach
			UVehicalSocketComponent * VSocket = CreateDefaultSubobject<UVehicalSocketComponent>(TEXT("Vehicle Socket"));

			// Attach the socket
			VSocket->AttachTo(StaticMeshComponent, Sockets*.Name);

			// Place a VSocket on each of our sockets
			VSockets.Add(VSocket);

		}
	}
}

This line seems to be crashing the editor the first time it’s encountered:


UVehicalSocketComponent * VSocket = CreateDefaultSubobject<UVehicalSocketComponent>(TEXT("Vehicle Socket"));

UVehicalSocketComponent is a subclass of USceneComponent. Nothings been changed yet.

Running this same line in the constructor seems to be fine.

So I decided to run the editor in debug and put a breakpoint a few lines above this one, stepped through the code and when the editor crashed I was able to determine that this is what caused everything to go boom:


UObject Line 83: UE_CLOG(!CurrentInitializer, LogObj, Fatal, TEXT("No object initializer found during construction."));

No idea what that means.

Hey codysmith105-

CreateDefaultSubobject is only usable inside the class Constructor (rather than OnContruction()). Moving the code to the constructor should allow you to do what you’re trying to do. Alternatively, you can use NewObject as that will work outside of the constructor.

Cheers

Doug Wilson

Thanks so much Doug, that was driving me absolutely crazy.

So are all the FObjectInitializer functions only usable inside the constructor?

Yes, and as Doug said to dynamiclly create component use NewObject() and MyComponent->RegisterComponent()

And note that OnConstruction will be fired every time your object is updated

And to prevent accumulation of created components you can use this (if there is any error ,Sorry Im using my phone now )



If(VSockets.Num()>0)
{

   for(UVehicalSocketComponent * Sockets : VSockets)
       {
          SocketComp->DestroyComponent();
       }

   VSockets.Empty();

}


Awesome, I figured that’s how to handle the old components, but a solid example is great. I’m getting the hang of this, loving UE4.