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.