How can i implement this blueprint in C++ ?
Here’s what I use for my project:
Header:
#include "GameFramework/Actor.h"
#include "BuildingMod_Defines.h"
#include "BuildingModTile.generated.h"
/**
*
*/
UCLASS(Blueprintable, BlueprintType)
class RPGSYSTEM_DEMO_API ABuildingModTile : public AActor
{
GENERATED_BODY()
UPROPERTY()
TSubobjectPtr<USphereComponent> BaseRoot;
//Instance Holders
UPROPERTY()
UInstancedStaticMeshComponent * Floor;
UPROPERTY()
UInstancedStaticMeshComponent * Wall;
UPROPERTY()
UInstancedStaticMeshComponent * InternalMesh;
//Inital Create Panel
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Setup")
TArray<UStaticMesh*> PartType_Meshes;
//Same as the constrution script
void OnConstruction (const FTransform & Transform) override;
Constructor:
ABuildingModTile::ABuildingModTile(const FObjectInitializer& ObjectInitializer)
: Super(ObjectInitializer)
{
BaseRoot = ObjectInitializer.CreateDefaultSubobject<USphereComponent>(this, TEXT("BaseSphereComponent"));
RootComponent = BaseRoot;
Floor = ObjectInitializer.CreateDefaultSubobject < UInstancedStaticMeshComponent >(this, TEXT("InstancedMeshComponent0"));
Wall = ObjectInitializer.CreateDefaultSubobject < UInstancedStaticMeshComponent >(this, TEXT("InstancedMeshComponent1"));
InternalMesh = ObjectInitializer.CreateDefaultSubobject < UInstancedStaticMeshComponent >(this, TEXT("InstancedMeshComponent2"));
Floor->AttachTo(BaseRoot, NAME_None, EAttachLocation::KeepRelativeOffset);
Wall->AttachTo(BaseRoot, NAME_None, EAttachLocation::KeepRelativeOffset);
InternalMesh->AttachTo(BaseRoot, NAME_None, EAttachLocation::KeepRelativeOffset);
C++ equivalent of the construction script:
void ABuildingModTile::OnConstruction(const FTransform & Transform){
Super::OnConstruction(Transform);
BuildFloorAndWall();
}
void ABuildingModTile::BuildFloorAndWall(){
//Here we initalize the build of meshes to a default value.
if (PartType_Meshes.Num() > 0){
//Floor setup
if (init_meshes && FloorIndex == BuildPart_Types::BT_NONE){
FloorIndex = BuildPart_Types::BT_FLOOR;
}
if (FloorIndex == BuildPart_Types::BT_NONE){
Floor->ClearInstances();
}
else{
if (PartType_Meshes.Num() > FloorIndex - 1)
{
if (PartType_Meshes[FloorIndex - 1] != NULL){
Floor->SetStaticMesh(PartType_Meshes[FloorIndex - 1]);
FTransform temp = FTransform();
temp.SetLocation(FVector(0, 0, 0));
Floor->ClearInstances();
Floor->AddInstance(temp);
}
else{
UE_LOG(LogTemp, Log, TEXT("Error 04: Mesh is NULL"));
init_meshes = true;
return;
}
}
else{
UE_LOG(LogTemp, Log, TEXT("Error 01: Index Exceeds the avaliavle meshes Please add mesh place holders for these mesh types"));
init_meshes = true;
return;
}
}
//Wall Setup
if (WallType == BuildPart_Types::BT_NONE){
Wall->ClearInstances();
}else{
if (PartType_Meshes.Num() > WallType - 1)
{
if (PartType_Meshes[WallType - 1] != NULL){
Wall->SetStaticMesh(PartType_Meshes[WallType - 1]);
FTransform temp = FTransform();
temp.SetLocation(FVector(0, 0, 0));
Wall->ClearInstances();
Wall->AddInstance(temp);
}
else{
UE_LOG(LogTemp, Log, TEXT("Error 05: Mesh is NULL"));
init_meshes = true;
return;
}
}else{
UE_LOG(LogTemp, Log, TEXT("Error 02: Index Exceeds the avaliavle meshes Please add mesh place holders for these mesh types"));
init_meshes = true;
return;
}
}
//Wall Setup
if (InternalType == BuildPart_Types::BT_NONE){
InternalMesh->ClearInstances();
}
else{
if (PartType_Meshes.Num() > InternalType - 1)
{
if (PartType_Meshes[InternalType - 1] != NULL){
InternalMesh->SetStaticMesh(PartType_Meshes[InternalType - 1]);
FTransform temp = FTransform();
temp.SetLocation(FVector(0, 0, 0));
InternalMesh->ClearInstances();
InternalMesh->AddInstance(temp);
}
else{
UE_LOG(LogTemp, Log, TEXT("Error 05: Mesh is NULL"));
init_meshes = true;
return;
}
}
else{
UE_LOG(LogTemp, Log, TEXT("Error 02: Index Exceeds the avaliavle meshes Please add mesh place holders for these mesh types"));
init_meshes = true;
return;
}
}
init_meshes = false;
}else{
UE_LOG(LogTemp, Log, TEXT("Error 00: Index Exceeds the avaliavle meshes Please add mesh place holders for these mesh types"));
init_meshes = true;
}
}