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.
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.
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.
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?
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));
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.