I created an actor that procedural adds components in its constructor, but when I place it in the level I lose the ability to save the map. The components are added to an array of structs, with one of the members in the struct being the component pointer.
The relevant parts of the code follow
//Struct
USTRUCT(BlueprintType)
struct FGridVoxel
{
GENERATED_BODY()
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Voxels")
UPrimitiveComponent* Component;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Voxels")
int32 X;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Voxels")
int32 Y;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Voxels")
int32 Z;
//Constructor
FGridVoxel()
{
X = 0;
Y = 0;
Z = 0;
Component = NULL;
}
FGridVoxel(UPrimitiveComponent* Comp, int32 x, int32 y, int32 z)
{
X = x;
Y = y;
Z = z;
Component = Comp;
}
};
//From the constructor
int32 X = BoxExtent.X / VoxelExtent.X;
int32 Y = BoxExtent.Y / VoxelExtent.Y;
int32 Z = BoxExtent.Z / VoxelExtent.Z;
//Populate the volume with voxels
for (int32 x = -X; x <= X; x++)
{
for (int32 y = -Y; y <= Y; y++)
{
for (int32 z = -Z; z <= Z; z++)
{
int32 N = Grid->Voxels.Num();
FString Name = "Voxel_" + FString::FromInt(N);
UBoxComponent* NewVox = NewObject<UBoxComponent>(this, FName(*Name));
NewVox->SetBoxExtent(VoxelExtent);
NewVox->RegisterComponent();
NewVox->SetCollisionProfileName(VoxelCollisionProfile);
NewVox->SetCollisionEnabled(ECollisionEnabled::QueryAndPhysics);
NewVox->bGenerateOverlapEvents = true;
NewVox->SetHiddenInGame(true);
Grid->AddVoxel(NewVox, x, y, z);
}
}
}
//Add voxel function
int32 UVoxelGridComponent::AddVoxel(UPrimitiveComponent* Comp, int32 X, int32 Y, int32 Z)
{
FGridVoxel NewVox = FGridVoxel(Comp, X, Y, Z);
Comp->AttachToComponent(this, FAttachmentTransformRules::SnapToTargetIncludingScale);
Comp->SetRelativeLocation(RelativeFromAddress(X, Y, Z));
return Voxels.Add(NewVox);
}