Spawn UStaticMesh from TMap

Hi There,

Please consider the following:

I have a UActorComponent with the following member & function:

UPROPERTY(BlueprintReadWrite, Category = Meshes, EditAnywhere)
TMap<int32, UStaticMesh*> MeshMap;

void SpawnGhost();

when the SpawnGhost is called I would like to spawn a UStaticMesh using this for example:

MeshMap.Find(1)

Where 1 is the index of the currently selected option.

What would be the correct and most efficient way to do this?

Do I need to use this: World->SpawnActor<AStaticMeshActor>(AStaticMeshActor::StaticClass(), Location, Rotation);

And if so how do I go about setting the UStaticMesh from the Selected TMap??

I have tried the following and I noticed if I Eject that the StaticMeshActor’s is indeed being spawned. All I need now is a way to set actual Mesh…

void UBuildSystemComponent::SpawnGhost()
{
this->BuildTrace();

UWorld* World = this-&gt;GetWorld();
if (World)
{
	FVector Location(0.0f, 0.0f, 0.0f);
	FRotator Rotation(0.0f, 0.0f, 0.0f);
	AStaticMeshActor* result = World-&gt;SpawnActor&lt;AStaticMeshActor&gt;(AStaticMeshActor::StaticClass(), Location, Rotation);
	if (result)
	{
		UE_LOG(LogTemp, Warning, TEXT("%s spawned"), *result-&gt;GetName())
		UStaticMeshComponent* comp = result-&gt;GetStaticMeshComponent();
		if (comp)
		{
			UE_LOG(LogTemp, Warning, TEXT("%s found"), *comp-&gt;GetName())
			auto mesh = this-&gt;MeshMap.Find(1);
			if (mesh)
			{
				UE_LOG(LogTemp, Warning, TEXT("found tmap entry"))
			}
			// comp-&gt;SetStaticMesh(mesh-&gt;Object);
		}
	}
}

}

Based on the above I have managed to get a working solution using the following code:



void UBuildSystemComponent::SpawnGhost()
{
	UE_LOG(LogTemp, Warning, TEXT("TRY SPAWN SOMETHING"))

	// this->BuildTrace();

	UWorld* World = this->GetWorld();
	if (World)
	{
		FVector Location(0.0f, 0.0f, 0.0f);
		FRotator Rotation(0.0f, 0.0f, 0.0f);

		FActorSpawnParameters SpawnParams;
		AStaticMeshActor* result = World->SpawnActor<AStaticMeshActor>(AStaticMeshActor::StaticClass(), Location, Rotation, SpawnParams);
		if (result)
		{
			result->SetMobility(EComponentMobility::Movable);

			UE_LOG(LogTemp, Warning, TEXT("%s spawned"), *result->GetName())
			UStaticMeshComponent* comp = result->GetStaticMeshComponent();
			if (comp)
			{
				UE_LOG(LogTemp, Warning, TEXT("%s found"), *comp->GetName())
				UStaticMesh** mesh = this->MeshMap.Find(1);
				if (mesh)
				{
					UE_LOG(LogTemp, Warning, TEXT("found tmap entry"))
					UStaticMesh* test = (*mesh);

					UE_LOG(LogTemp, Warning, TEXT("%s found"), *test->GetName())
					comp->SetStaticMesh(test);
				}				
			}
		}
	}
}


I would really appreciate some expert feedback on this, a mini code review of sorts as I am not super pro at C++ so not sure if this is the prefered way to do what I am trying to do.