How do you spawn meshes from an array?

Hi guys,

So, I have the below code:

ALevelGen::ALevelGen(const FObjectInitializer& ObjectInitializer)
	: Super(ObjectInitializer)

{
	TArray<UStaticMeshComponent*> PlatformsArray;

	Platform = ObjectInitializer.CreateAbstractDefaultSubobject<UStaticMeshComponent>(this, TEXT("Floor_StaticMesh"));

	static ConstructorHelpers::FObjectFinder <UStaticMesh>StaticMesh(TEXT("StaticMesh'/Game/Meshes/Floor_StaticMesh.Floor_StaticMesh'"));

	Platform->SetStaticMesh(StaticMesh.Object);

	PlatformsArray.Add(Platform);

	RootComponent = Platform;
}

(I’m going to add more meshes later). So how do I spawn a mesh from that code in my GameMode class or is there a better way to do this?

Currently spawning the mesh without the array :

World->SpawnActor<ALevelGen>(ALevelGen::StaticClass(), FVector(0, 0, -400), FRotator(0,0,0));

Regards,

Hi,

I think you can only spawn an actor. Like you already do, this is done by calling the SpawnActor function with a child class of AActor.

However, your actor can have multiple meshes. A UStaticMeshComponent can be attached to another one. You could have:

// set a root component
rootComponent = ObjectInitializer.CreateDefaultSubobject<UStaticMeshComponent>(this, TEXT("staticRootComponent"));
RootComponent = rootComponent;
// first mesh
MyMesh1 = ObjectInitializer.CreateDefaultSubobject<UStaticMeshComponent>(this, TEXT("MyMesh1"));
static ConstructorHelpers::FObjectFinder<UStaticMesh> StaticMesh1(TEXT("StaticMesh'/Game/Meshes/MyAsset1.MyAsset1'"));
MyMesh1->SetStaticMesh(StaticMesh1.Object);
// seconds mesh
MyMesh2 = ObjectInitializer.CreateDefaultSubobject<UStaticMeshComponent>(this, TEXT("MyMesh2"));
static ConstructorHelpers::FObjectFinder<UStaticMesh> StaticMesh2(TEXT("StaticMesh'/Game/Meshes/MyAsset2.MyAsset2'"));
MyMesh2->SetStaticMesh(StaticMesh2.Object);
// add both meshed to the root component
MyMesh1->AttachTo(RootComponent);
MyMesh2->AttachTo(RootComponent);

Does this help?

Hi,

Thanks for your reply! That does help a bit but, unfortunately, I should have mentioned that I don’t want to spawn them all at once as I will be looking to move the platforms towards the screen and then spawning a random one after the previous one has gone too far past the player’s view and then destroy that particular actor as it has gone out of view.

Regards,