UCLASS()
class PROGRAMMER01_API ASolidBlock : public AActor
{
GENERATED_UCLASS_BODY()
public:
// Sets default values for this actor's properties
ASolidBlock();
// Called when the game starts or when spawned
virtual void BeginPlay() override;
// Called every frame
virtual void Tick( float DeltaSeconds ) override;
// static mesh
UStaticMeshComponent* MyBlock;
};
SolidBlock.cpp
// Sets default values
ASolidBlock::ASolidBlock(const class FObjectInitializer& PCIP)
: Super(PCIP)
{
// 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;
MyBlock = PCIP.CreateAbstractDefaultSubobject<UStaticMeshComponent>(this, TEXT("SolidBlock"));
static ConstructorHelpers::FObjectFinder <UStaticMesh>StaticMesh(TEXT("StaticMesh'/Game/StarterContent/Architecture/Wall_500x500'"));
static ConstructorHelpers::FObjectFinder<UMaterial> Material(TEXT("MaterialInstanceConstant'/Game/StarterContent/Materials/M_CobbleStone_Pebble'"));
MyBlock->SetStaticMesh(StaticMesh.Object);
MyBlock->SetMaterial(0, Material.Object);
}
// Called when the game starts or when spawned
void ASolidBlock::BeginPlay()
{
Super::BeginPlay();
}
// Called every frame
void ASolidBlock::Tick( float DeltaTime )
{
Super::Tick( DeltaTime );
}
I’m currently working on this problem as well, however I’m stuck at getting my object to spawn when BeginPlay occurs. My test class is setup the same as this one. I also have a Game Mode where I’m trying to spawn my test class when the game is run. When I load the editor with my project I can see my class in the side, I can then drag the class out into the scene and then run the game. My object will spawn where it is supposed to but I first have to place it out in the scene. I would like to have the object placed in the scene through code.
Are you getting your object to spawn in the middle of your level without having to manually click/ drag the class out into the scene view? I’d be happy if I could get to that point.
The actor class I’ve created has it’s static mesh and material assigned exactly the way yours is. When I drag the class into the scene I can see its properties with the correct static mesh/ material assigned. I’ve even turned physics on the asset through code so it will fall down to the ground. Still I cannot get it to automatically place in the scene when I run the game. What am I missing?
The code that was posted by Softfalcon is missing one major thing that is causing the transform and rotation of the SpawnActor function to not apply to the SolidBlock actor. A root component needs to be set. If you add the following line of code after the MyBlock staticmeshcomponent is made in the SolidBlock.cpp everything should work perfectly.