Download

Instanced static mesh not showing from component in world space.

Hi

I have c++ code that should create a actor, create a static mesh using instanced static mesh, and display it. The primary code in BP worked but I’m having problems with c++. There is a worker and spawner c++ code. The spawner creates a worker then the worker create a instance object or add more. I’m not sure why it’s not appeaing at the world space location. The worker spawner BP is https://blueprintue.com/blueprint/m7tbe-eb/.
The worker header is Worker.h - Pastebin.com and cpp is Worker CPP - Pastebin.com.

Vivienne



CPP file
// Fill out your copyright notice in the Description page of Project Settings.

#include"Populate_CPP_Worker.h"

// Sets default values
APopulate_CPP_Worker::APopulate_CPP_Worker()
{
// 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;
}
// Called when the game starts or when spawned
voidAPopulate_CPP_Worker::BeginPlay()
{
Super::BeginPlay();

}
// Called every frame
voidAPopulate_CPP_Worker::Tick(floatDeltaTime)
{
Super::Tick(DeltaTime);
}
// Called every frame
voidAPopulate_CPP_Worker::CreateWorkerInstance()
{   
// Get world to spawn
    UWorld* world =GetWorld();
// Spawn Parameter
    FActorSpawnParameters spawnParams;
// Set Owner to This
//spawnParams.Owner = this;
spawnParams.SpawnCollisionHandlingOverride=ESpawnActorCollisionHandlingMethod::AdjustIfPossibleButAlwaysSpawn;
// User Passed transformation
    InstancedActor =world->SpawnActor<AActor>(Transformation.GetLocation(), FRotator::ZeroRotator, spawnParams);
if(InstancedActor)
    {
// Create Object
            InstancedStaticMeshComponent =NewObject<UInstancedStaticMeshComponent>(GetWorld()->GetCurrentLevel());
// Attach to actor
InstancedStaticMeshComponent->AttachToComponent(InstancedActor->GetRootComponent(), FAttachmentTransformRules::KeepWorldTransform);
// Set Static Mesh
InstancedStaticMeshComponent->SetStaticMesh(StaticMesh);
// Register
InstancedStaticMeshComponent->RegisterComponentWithWorld(GetWorld());
if(InstancedStaticMeshComponent)
            {
PopulateSpawner->HandleWorkerResponse(this, true, InstancedActor);
            }else
            {
PopulateSpawner->HandleWorkerResponse(this, false, nullptr);
            }
    }
}
voidAPopulate_CPP_Worker::AddInstance()
{    
// If Instance is created
if(InstancedStaticMeshComponent)
    {   
//if (GEngine)
//{    
//float TimeToDisplay = 1.0f;
//
//     GEngine->AddOnScreenDebugMessage(-1, 2.0f, FColor::Red, TEXT("TEXT %s"), *(Transformation.Location.ToString()));
//}

// Add Instance to World Space

//Transformation.DebugPrint();
InstancedStaticMeshComponent->AddInstanceWorldSpace(Transformation);

// Handle Worker Response
PopulateSpawner->HandleWorkerResponse(this, true, InstancedActor);
    }
}




The worker header file
// Fill out your copyright notice in the Description page of Project Settings.
    #pragma once
    #include "CoreMinimal.h"
  #include "GameFramework/Actor.h"
  #include "Engine/Classes/Components/StaticMeshComponent.h"
  #include "Engine/Classes/Components/InstancedStaticMeshComponent.h"
  #include "Populate_CPP_Spawner.h"
  #include "Populate_CPP_Worker.generated.h"
    UCLASS()
  class ORVILLETREK_API APopulate_CPP_Worker : public AActor
  {
      GENERATED_BODY()
      
  public: 
      // Sets default values for this actor's properties
      APopulate_CPP_Worker();
        // Spawner
      UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Populate")  
      APopulate_CPP_Spawner* PopulateSpawner;
        // Set Mesh     
      UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Populate")
      UStaticMesh * StaticMesh;
        // Transform
      UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Populate")
      FTransform Transformation;
        // Transform
      UFUNCTION(BlueprintCallable, Category = "Populate")
      virtual void CreateWorkerInstance();
        // Transform
      UFUNCTION(BlueprintCallable, Category = "Populate")
      virtual void AddInstance();
    protected:
      // Called when the game starts or when spawned
      virtual void BeginPlay() override;
    public: 
      // Called every frame
      virtual void Tick(float DeltaTime) override;
 
private:
        //private
      AActor * InstancedActor;
        // Instance Static Mesh Component
      UInstancedStaticMeshComponent* InstancedStaticMeshComponent;
      
  };
 





It looks like the error is in here. The transformation is password correctly.




// Called every frame
  void APopulate_CPP_Worker::CreateWorkerInstance()
  {   
      // Get world to spawn
      UWorld* world = GetWorld();
        // Spawn Parameter
      FActorSpawnParameters spawnParams;
        // Set Owner to This
      //spawnParams.Owner = this;
      spawnParams.SpawnCollisionHandlingOverride=ESpawnActorCollisionHandlingMethod::AdjustIfPossibleButAlwaysSpawn;
        // User Passed transformation
      InstancedActor = world->SpawnActor<AActor>(Transformation.GetLocation(), FRotator::ZeroRotator, spawnParams);
        if(InstancedActor)
      {
              // Create Object
              InstancedStaticMeshComponent = NewObject<UInstancedStaticMeshComponent>(GetWorld()->GetCurrentLevel());
                // Attach to actor
              InstancedStaticMeshComponent->AttachToComponent(InstancedActor->GetRootComponent(), FAttachmentTransformRules::KeepWorldTransform);
                // Set Static Mesh
              InstancedStaticMeshComponent->SetStaticMesh(StaticMesh);
                // Register
              InstancedStaticMeshComponent->RegisterComponentWithWorld(GetWorld());
                if(InstancedStaticMeshComponent)
              {
                  PopulateSpawner->HandleWorkerResponse(this, true, InstancedActor);
              }else
              {
                  PopulateSpawner->HandleWorkerResponse(this, false, nullptr);
              }
      }
  }
 



I found the problem but a new one appears. After the instances is created everything dissappears.

https://www.youtube.com/watch?v=tpyowArEzbg