Hello,
I am attempting to learn how to spawn actors from C++, and assign them a mesh and material. After making a change to pass the address of a UStaticMeshComponent into AActor::SetRootComponent, the editor crashed on the next compile. After this crash, the project crashes while trying to open.
I have tried the solutions offered here (link), but the project still crashes while opening.
I made a new blank project and started copying over the code and assets, while leaving the change I had made commented out - this worked for a while as I went through and changed the names of a few variables and such to match the new project, but then it suddenly crashed again and now I can’t open the new project either.
I’m new to UE4 and kind of new to C++, and I get that AActor::SetRootComponent receives a USceneComponent reference, not a UStaticMeshComponent reference, but I don’t know what I don’t know so I’m learning a lot through compile errors and some trial and error. I would like to at least open the project and go back to trying!
The code I had just written before the crash is in bold below:
void ABoard::AddPieces(TArray<FVector> positions, UMaterial* PlayerMaterial)
int32 NumberOfPositions = positions.Num();
**PieceComponent.SetStaticMesh(PieceMesh);
PieceComponent.SetMaterial(0, PlayerMaterial);**
UWorld* const World = GetWorld();
if (NumberOfPositions > 0)
{
for (int32 i = 0; i < NumberOfPositions; i++)
{
int32 row = FMath::FloorToInt(positions[i].X);
int32 col = FMath::FloorToInt(positions[i].Y);
if (World)
{
APieceCheckers* SpawnedPiece = (APieceCheckers*) World->SpawnActor<APieceCheckers>(positions[i], FRotator::ZeroRotator);
**SpawnedPiece->SetRootComponent(&PieceComponent);**
BoardGrid.AddItem(row, col, SpawnedPiece);
}
}
}
“PieceComponent” is a UStaticMeshComponent variable defined in the header, and “BoardGrid” is a 2-d array Struct defined in its own header. “APieceCheckers” is an AActor representing a checkers playing piece.
I have attached the log files from the crash, hopefully it helps!