Dynamically Assigning UStaticMeshComponents to Newly-Spawned Objects

I have an actor (‘AOverseerClass’ in the example below) responsible for placing and coordinating various other actors (which I’ll call ‘cell objects’ - an example is ‘AExample’ below). In the actual code, there are various types of cell classes, all inheriting from an ACell class. Each type of cell has its own behaviour, and each should have its own mesh. The number of cell objects varies, and some may need to be spawned or despawned dynamically during gameplay, as determined by AOverseerClass.

As a basic example, consider the classic 2D Snake game: each ‘cell’ would be one block of the snake, and the overseer class would be the class responsible for coordinating how the snake moves. My game is a bit more complicated, but the concept is effectively the same - during gameplay, I want to extend the body of the playable character more and more depending on what powerups the player collects. I need different types of cells to have their own mesh, and I do not need different meshes to be merged into a single mesh. Note that I eventually intend to have various overseer objects in the world, each working independently with their own cells.

It’s easy enough to give a single actor a mesh with the following code after dragging and dropping in Unreal:


UCLASS()
class TESTPROJECT_API AExampleClass : public AActor
{
	GENERATED_BODY()

public:
	// Sets default values for this actor's properties
	AExampleClass();

private:
	UPROPERTY(VisibleAnywhere, BlueprintReadWrite, meta = (AllowPrivateAccess = "true"))
		UStaticMeshComponent * exampleClassMesh;
};


// Sets default values
AExampleClass::AExampleClass()
{
 	// Set this actor to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
	PrimaryActorTick.bCanEverTick = true;

	exampleClassMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("CellHeadMesh"));
	RootComponent = exampleClassMesh;

}



Static Mesh.JPG

I want to do something very similar to the above, except I want to select the different kinds of meshes from a drop-down from the overseer object, not for each cell. I then want be able to assign these meshes to newly spawned cells. I don’t want the overseer to physically have its own mesh; the purpose of selecting its UStaticMeshComponents is only to then assign the same selected meshes to cells it spawns. I don’t intend to drag and drop a single cell into the game. Instead, I want to drag and drop an overseer, and let it handle the spawning of the cells.

Let me rewrite the above with my current attempt:


{
	GENERATED_BODY()

public:
	// Sets default values for this actor's properties
	AExampleClass();

private:
	// I removed UPROPERTY(), as this isn't where I want to select the mesh
	UStaticMeshComponent * exampleClassMesh;
};


// Sets default values
AExampleClass::AExampleClass()
{
 	// Set this actor to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
	PrimaryActorTick.bCanEverTick = true;

	exampleClassMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("CellHeadMesh"));
	RootComponent = exampleClassMesh;

}

UCLASS()
class TESTPROJECT_API AOverseerClass : public AActor
{
	GENERATED_BODY()
	
public:	
	// Sets default values for this actor's properties
	AOverseerClass();

	// Class to spawn cells
	void spawnCells();

private:
	// A potential mesh to give a cell after spawning
	UPROPERTY(VisibleAnywhere, BlueprintReadWrite, meta = (AllowPrivateAccess = "true"))
		UStaticMeshComponent * potentialMesh1;

	// Another potential mesh to give a cell after spawning
	UPROPERTY(VisibleAnywhere, BlueprintReadWrite, meta = (AllowPrivateAccess = "true"))
		UStaticMeshComponent * potentialMesh;
	
};

void AExampleClass::spawnCells() {
	// I'm skipping the lines where I check if the World object exists, and figuring out the location and rotation of the objects to be spawned
	AExampleClass * exampleClass1 = Cast<AExampleClass>(currentWorld->SpawnActor(AExampleClass::StaticClass(), &location1, &rotator1, SpawnParametres));

	AExampleClass * exampleClass2 = Cast<AExampleClass>(currentWorld->SpawnActor(AExampleClass::StaticClass(), &location2, &rotator2, SpawnParametres));

	// Here's the part that I can't figure out - how do I give the selected meshes to the spawned classes?
	exampleClass1->exampleMesh = potentialMesh1;
	exampleClass2->exampleMesh = potentialMesh2;
}


I can confirm that the cells are being spawned at the right location; but as it is the drop-down menus for the meshes in the overseer object only work if I use CreateDefaultSubobject to place the meshes in the world, which is not what I want to do. Ultimately, I can’t figure out how to do two things:

  1. Selecting the various meshes from drop-down menus in Unreal for the overseer without those meshes being for the overseer itself.
  2. Having the overseer give meshes to spawned cells after spawning, based on the options chosen from 1.

I’m not sure how to approach this. I’ve looked online, but I can’t find anyone trying to do something similar.
Thanks in advance.

An update - there was a simple solution I missed because I was stupidly going about it the wrong way. I created Blueprints based off my cell classes, and assigned meshes directly to them. Then for the overseer class, I simply had to select what Blueprint classes to spawn the same way I was previously trying to do with meshes. I don’t know why it took me so long to figure it out, but it works now.