Announcement

Collapse
No announcement yet.

Spawn UStaticMesh from TMap

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    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??

    Always Active Technologies
    {Binary_Shepherd}
    /** the only losers are those who give up. */

    #2
    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->GetWorld();
    if (World)
    {
    FVector Location(0.0f, 0.0f, 0.0f);
    FRotator Rotation(0.0f, 0.0f, 0.0f);
    AStaticMeshActor* result = World->SpawnActor<AStaticMeshActor>(AStaticMeshActor::StaticClass(), Location, Rotation);
    if (result)
    {
    UE_LOG(LogTemp, Warning, TEXT("%s spawned"), *result->GetName())
    UStaticMeshComponent* comp = result->GetStaticMeshComponent();
    if (comp)
    {
    UE_LOG(LogTemp, Warning, TEXT("%s found"), *comp->GetName())
    auto mesh = this->MeshMap.Find(1);
    if (mesh)
    {
    UE_LOG(LogTemp, Warning, TEXT("found tmap entry"))
    }
    // comp->SetStaticMesh(mesh->Object);
    }
    }
    }
    }
    Last edited by Shepherd; 02-14-2017, 06:28 AM. Reason: typo

    Always Active Technologies
    {Binary_Shepherd}
    /** the only losers are those who give up. */

    Comment


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

      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.

      Always Active Technologies
      {Binary_Shepherd}
      /** the only losers are those who give up. */

      Comment

      Working...
      X