Crash on compilation, now editor crashes on project open

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!

Hello cgardner,

I’m surprised that line is even compiling as it isn’t on my side. You shouldn’t need to get the address of the PieceComponent. Placing the pointer itself in there should be fine if you’re using the same one it was declared with. Can I assume that your declaration for it looks something like this?:

UStaticMeshComponent* PieceComponent;

When not using the reference (the only way I could get it to compile) the SetRootComponent function worked for me but you can also try setting it directly and see if that works for you like so:

SpawnedPiece->RootComponent = PieceComponent;

We haven’t heard from you in a while, cgardner. Are you still experiencing this issue? If so, have you tried the solution from my previous comment? Did it work out for you? In the meantime I’ll be marking this issue as resolved for tracking purposes.

It seems I have a lot to learn, I went back and read a lot of the documentation and C++ tutorials, and I have taken a new approach to my project! Thank you for the help though!