I am trying to make a code that creates a grid system. In blueprint that works but has performence issuses. So I tried to make a C++ one but I can’t even test it because whenever I compile it unreal crashes. Sometimes I even lose my project files too. I dont know what to do any more so I would take any kind of help.
#include "GridGenerator.h"
#include "Components/StaticMeshComponent.h"
#include "ConstructorHelpers.h"
// Sets default values
AGridGenerator::AGridGenerator()
{
// Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = false;
size = 16;
sum = size * size;
offsetAmount = 65;
for (int i = 0; i < sum; i++) {
if (i % size == 0) {
FVector temp;
temp.X = size+offsetAmount;
temp.Y = offsetAmount;
temp.Z = 0;
offsetVector = offsetVector+temp;
Mesh = SpawnMesh(offsetVector,i);
ReactiveFloor(worldLocation);
MeshArray[i] = Mesh;
offsetVector.X = offsetVector.X + offsetAmount;
}
else {
Mesh = SpawnMesh(offsetVector,i);
ReactiveFloor(worldLocation);
MeshArray[i] = Mesh;
offsetVector.X = offsetVector.X + offsetAmount;
}
}
}
UStaticMeshComponent* AGridGenerator::SpawnMesh(FVector offsetVector,int i)
{
FString name = FString(TEXT("StaticMeshComponentCOMP"));
name.AppendChar('-');
name.AppendInt(i);
FName temp = FName(*name);
UStaticMeshComponent* Grid = CreateDefaultSubobject<UStaticMeshComponent>(temp);
Grid->SetupAttachment(RootComponent);
static ConstructorHelpers::FObjectFinder<UStaticMesh>GridAsset(TEXT("/Game/Models-Textures/StaticMeshes/Plane_64x64"));
Grid->SetStaticMesh(GridAsset.Object);
Grid->SetRelativeLocation(offsetVector);
Grid->SetWorldScale3D(FVector(1.f));
worldLocation = offsetVector;
return Grid;
}
void AGridGenerator::ReactiveFloor(FVector worldLocation) {
FCollisionQueryParams RV_TraceParams = FCollisionQueryParams(FName(TEXT("RV_Trace")), true, this);
RV_TraceParams.bTraceComplex = true;
RV_TraceParams.bTraceAsyncScene = true;
RV_TraceParams.bReturnPhysicalMaterial = false;
//Re-initialize hit info
FHitResult RV_Hit(ForceInit);
//call GetWorld() from within an actor extending class
GetWorld()->LineTraceSingleByChannel(RV_Hit, worldLocation + (FVector(0, 0, 2000)), worldLocation - (FVector(0, 0, 2000)), ECC_EngineTraceChannel1);
FVector temp2;
temp2.X = worldLocation.X;
temp2.Y = worldLocation.Y;
temp2.Z = RV_Hit.ImpactPoint.Z;
FRotator tempR;
tempR.Pitch = (RV_Hit.ImpactNormal.X * 45);
tempR.Roll = (RV_Hit.ImpactNormal.Y * 45);
SetActorLocationAndRotation(temp2, tempR);
}
// Called when the game starts or when spawned
void AGridGenerator::BeginPlay()
{
Super::BeginPlay();
}
// Called every frame
void AGridGenerator::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}