Beginner here, but ill give it a go:
Create a new project, then extract the static mesh from the box brush as detailed here.
Save it somewhere
Go File->AddCodeToProject
Add a new actor, call it ‘CubeActor’
Exit from the unreal editor
change CubeActor.h to the following:
UCLASS()
class SOMEPROJECT_API ACubeActor : public AActor
{
GENERATED_UCLASS_BODY()
UPROPERTY(Category = Meshes, VisibleAnywhere)
TSubobjectPtr<UStaticMeshComponent> CubeMesh;
};
Change CubeActor.cpp to the following:
ACubeActor::ACubeActor(const class FPostConstructInitializeProperties& PCIP)
: Super(PCIP)
{
CubeMesh = PCIP.CreateDefaultSubobject<UStaticMeshComponent>(this, TEXT("CubeMesh"));
}
Recompile your project, should build fine.
Back in the editor , select new from the content browser in the bottom left, select blueprint - select your newly created CubeActor:
save your blueprint as ‘BP_Cube’
Open up your new blueprint.
Under ‘Defaults’, choose the static mesh you saved eariler:
Compile and save your blueprint.
Close the editor
Now thats done, you’ll want to change your ACubeActor to the following:
UCLASS()
class SOMEPROJECT_API ACubeActor : public AActor
{
GENERATED_UCLASS_BODY()
UPROPERTY(Category = Meshes, VisibleAnywhere)
TSubobjectPtr<UStaticMeshComponent> CubeMesh;
public:
TSubclassOf<class ACubeActor> CubeBlueprint;
};
ACubeActor.cpp:
ACubeActor::ACubeActor(const class FPostConstructInitializeProperties& PCIP)
: Super(PCIP)
{
CubeMesh = PCIP.CreateDefaultSubobject<UStaticMeshComponent>(this, TEXT("CubeMesh"));
static ConstructorHelpers::FObjectFinder<UBlueprint> CubeBP(TEXT("Blueprint'/Game/BP_Cube.BP_Cube'"));
if (CubeBP.Object){
CubeBlueprint = (UClass*)CubeBP.Object->GeneratedClass;
}
}
Now, you’ll want to add a new header library (this is so we can have a reference to cube blueprint anywhere)
Create ‘BP_Libs.h’ (if you build it straight in VS, it’ll put it by default in intermediate/projectfiles/, you’ll need to move this to source/someproject/ to be able to reference it)
then add the following:
#include "SomeProject.h"
static TSubclassOf<class ACubeActor> GetCubeBP()
{
static ConstructorHelpers::FObjectFinder<UBlueprint> CubeBP(TEXT("Blueprint'/Game/BP_Cube.BP_Cube'"));
if (CubeBP.Object){
return (UClass*)CubeBP.Object->GeneratedClass;
}
}
template <typename ObjectType>
static FORCEINLINE ObjectType* SpawnBP(
UWorld* TheWorld,
UClass* TheBP,
const FVector& Loc,
const FRotator& Rot,
const bool bNoCollisionFail = true,
AActor* Owner = NULL,
APawn* Instigator = NULL
){
if (!TheWorld) return NULL;
if (!TheBP) return NULL;
//~~
FActorSpawnParameters SpawnInfo;
SpawnInfo.bNoCollisionFail = bNoCollisionFail;
SpawnInfo.Owner = Owner;
SpawnInfo.Instigator = Instigator;
SpawnInfo.bDeferConstruction = false;
return TheWorld->SpawnActor<ObjectType>(TheBP, Loc, Rot, SpawnInfo);
}
Credits to rama: A new, community-hosted Unreal Engine Wiki - Announcements - Epic Developer Community Forums
and the guys over here: Spawn Blueprint from C++ - Editor Scripting - Epic Developer Community Forums
Then you can create the cube like this:
(I just used my gamemode constructor)
#include "BP_Libs.h"
ASomeProjectGameMode::ASomeProjectGameMode(const class FPostConstructInitializeProperties& PCIP)
: Super(PCIP)
{
FVector Position = FVector(0, 0, 0);
FRotator Rotation = FRotator(0);
SpawnBP<ACubeActor>(GetWorld(), GetCubeBP(), Position, Rotation);
}
I also added the full source here, if you want to pick through it: