How to SpawnActor with differnet staticmesh?

Hej,

I’d like to Spawn the actor for the first instance with CUBE mesh, second instance with SPHERE mesh. Is there any method to call SpawnActor function with static mesh parameter. I’ve been using the below code for spawn the actor. Any ideas/samples would be great helpful.

In BeginPlay()

GetWorld()->SpawnActor<MyActor>(Location, FRotator(0,0,0));

Hi,

I Give you 3 ways to do this, but i don’t know if one is better than the others:

1/ You spawn your actor then call an init function with your shape in parameter

2/ You have 2 child class of MyActor: one with cube and the second with a sphere. And you spawn the one you want.

3/ In the BeginPlay of MyActor you call a function that ask what shape it should create. You can set the Owner of the object with SpawnInfo before spawn.

Thanks for your suggestions. I think that the third way would match my logic. Could you please share some sample for better understanding. That would be really helpful for me.

Where i spawn my Projectile

FActorSpawnParameters SpawnInfo;
SpawnInfo.bNoCollisionFail = true;
SpawnInfo.Owner = MotherObject;
SpawnInfo.Instigator = MotherObject;
AProjectile* Projectile = GetWorld()->SpawnActor<AProjectile>(ProjectileType, ProjectileLocation, ProjectileRotation, SpawnInfo);
if (Projectile)
{
	Projectile->MyVariable = MyValue;
}

In Projectile.cpp

BeginPlay ()
{
   Owner->GetSomething ();
}

Could you please explain more about “MotherObject” and its initialization. I’ve tried as below

StaticRootcomp = CreateDefaultSubobject(TEXT("Mesh0"));
    UObject* staticmesh = StaticRootcomp;

        FActorSpawnParameters MyParams;
	MyParams.Owner = staticmesh;

But, getting an ERROR as Cannot be assigned to an entity of type “AActor*”. As a beginner, I couldn’t understand properly. Please help me or any tutorials will be great.

Owner is a reference on an AActor and your staticMesh is not an AActor*.

So if you are beginner i will give you a good method but i don’t know if it is exactly what you want.

I suppose you have an Actor named MyWeapon and this weapon can fire 2 types of Projectile

You have 3 Class of projectile:

  • MyCPPClassOfProjectile (Abstract class / you can’t instantiate it / it has a UPROPERTY staticMesh)

    UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = Projectile)
    UStaticMeshComponent* YourMesh

  • MyBluePrintProjectile1 (subClass of MyCPPClassOfProjectile )

  • MyBluePrintProjectile2 (subClass of MyCPPClassOfProjectile )

With BluePrint Interface you set the staticMesh of your choice in both MyBluePrintProjectile

In your class MyWeapon:
You have a list of MyCPPClassOfProjectile that can be spawn.

UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = Weapon)
    TArray<TSubclassOf<MyCPPClassOfProjectile> > List_ProjectileType

In BluePrintInterface you add to the list both of your MyBluePrintProjectile

In MyWeapon where you want spawn a projectile:

GetWorld()->SpawnActor<AProjectile>(List_ProjectileType[TheTypeYouWant], ProjectileLocation, ProjectileRotation);

I dont think it is really clear, ask more question if you need.

Good Luck

Thanks for your quick reply, I will try this solution and report you soon.

This is working. But I’m trying to instantiate the actor including “SetStaticMesh” property. The Reason behind this is; to save time and avoid creating ActorClass/BluePrint for every mesh. Do you think is it possible?

I think it is possible. It should be looking like:

In MyProjectile.cpp:
In Constructor:

YourMesh = ObjectInitializer.CreateDefaultSubobject<UStaticMeshComponent>(this, TEXT("YourMesh"));

And then set the mesh you want in a function

void SetMesh (UStaticMeshComponent* theMeshYouWant)

ABaseClass::ABaseClass(const FObjectInitializer& ObjectInitializer)
: Super(ObjectInitializer)
{
// 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;

	// Create dummy root scene component
	DummyRoot = CreateDefaultSubobject<USceneComponent>(TEXT("Dummy0"));
	RootComponent = DummyRoot;

 	// Structure to hold one-time initialization
	struct FConstructorStatics
	{
		ConstructorHelpers::FObjectFinderOptional<UStaticMesh> PlaneMesh;
		ConstructorHelpers::FObjectFinderOptional<UMaterialInstance> BlueMaterial;
		ConstructorHelpers::FObjectFinderOptional<UMaterialInstance> OrangeMaterial;
		FConstructorStatics()
			: PlaneMesh(TEXT("/Game/StarterContent/Meshes/Shape_Cube.Shape_Cube"))
			, PlaneMesh(TEXT("/Game/StarterContent/Meshes/Shape_Sphere.Shape_Sphere"))
		{
		}
	};
	static FConstructorStatics ConstructorStatics;

	// Create dummy root scene component
	DummyRoot = CreateDefaultSubobject<USceneComponent>(TEXT("Dummy0"));
	RootComponent = DummyRoot;
	
	// Create static mesh component
	BlockMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("BlockMesh0"));
	BlockMesh->SetStaticMesh(ConstructorStatics.PlaneMesh.Get());
   	BlockMesh->AttachTo(DummyRoot);
    }

// Called when the game starts or when spawned
void ABaseClass::BeginPlay()
{
	Super::BeginPlay();
	// Loop to spawn each block
	for (int32 Index = 0; Index<2; ++)
	{
		 // Spawn a block
		MyActor* NewBlock = GetWorld()->SpawnActor<MyActor>(BlockLocation, FRotator(0, 0, 0));

}
}

How I can Spawn MyActor with DummyRoot ??

This is done in MyProjectile.cpp, But I need to SetMesh in Spawn(ABaseClass) Cpp. Please check the below comment. Please help me. If I solve this will be grateful.