Download

Strange Behavior with UStaticMeshComponents

So I’m running Engine v4.0.0 still, but I don’t think this is a bug, I’m probably just trying to do it wrong. I’m trying to create an actor that creates 6 or however many instances of a UStaticMesh, and then line them up in a row. The way I went about it was with



for (int i = 0; i < 10; i++)
	{
		TempMesh = PCIP.CreateDefaultSubobject<UStaticMeshComponent>(this, TEXT("ModelComp1"));
		if (TempMesh != NULL && TempMesh->IsValidLowLevel())
		{
			TempMesh->BodyInstance.SetCollisionProfileName("PhysicsActor");			// Collision profiles are defined in DefaultEngine.ini
			TempMesh->StaticMesh = Mesh.Object;
			TempMesh->SetRelativeScale3D(FVector(0.25f, 0.25f, 0.25f));
			//TempMesh->SetSimulatePhysics(true);
			TempMesh->SetRelativeLocation(FVector(250*i, 0, 0));
			MeshArray.Add(TempMesh);
		}
	}


but this just crashes the game as soon as I try to put the actor into the scene. I’m pretty sure I’m just doing some C++'ing wrong. The error that’s returned is like “SubobjectInits[Index].Sub Object != Subobject”. However if I remove the for loop, and just leave the code that’s inside it to run once, it creates a second model at (250,0,0) in the world, that stays static there, not moving with the root component. Any ideas or things you’d need more detail on?
My actor’s .h file: http://pastebin.com/EzPUMaCh
My actor’s .cpp file: http://pastebin.com/SHeUYvVf

Yes, you are indeed C++'ing wrong. First of all get rid of TempMesh, then declare your mesh components array like so:



UPROPERTY()
TSubobjectPtr<UStaticMeshComponent> MeshArray[10];


Then change your loop to:



for (int i = 0; i < 10; i++)
{
    MeshArray* = PCIP.CreateDefaultSubobject<UStaticMeshComponent>(this, *FString::Printf(TEXT("ModelComp%d"), i));

    if (MeshArray* != NULL && MeshArray*->IsValidLowLevel())
    {
        ...

        MeshArray*->AttachParent = MeshComp;
    }
}


That should get rid of that crash of yours, and fix the “not moving with the root component” issue.

Thanks for the help dude. I tried changing the UStaticMeshComponent to a UCustomMeshComponent as per your code, but unfortunately your code doesn’t compile with either of them. I’m not sure how to fix this error:

Err, that was a typo, obviously I should’ve written UStaticMeshComponent not UCustomMeshComponent, fixed it above. You’ve only shown me the Intellisense error, what error does the compiler give you when you try to use the array of UStaticMeshComponent?

I got it to compile now. But it instantly crashes with this error.

Sorry that its in image form, I couldn’t attach the Visual Studio debugger to the process.

Can you pastebin the current version of your source please?

KillerCube.h - http://pastebin.com/H8AmhECR
KillerCube.cpp - http://pastebin.com/9hHqZeHN

Yep. I appreciate your patience btw :slight_smile:

You need to pass in unique names for your components to CreateDefaultSubobject() (they need to be unique within the context of the Outer object which is the first argument to that method), otherwise what happens is an existing component with the same name gets returned instead of a new one being created. Note that the code I’ve posted previously generates a unique name for each array element to avoid this problem.