I’m running into an issue, where an actor crashes my editor once I drop it into the window. The script compiles without issue, its only once it starts running that it crashes, which makes me wonder if it might be something to do with referencing something thats out of scope or something. When I run debug I get the 0xC0000005 error code, specifically saying that its an access violation for reading the location. Below is the line that the debugger doesn’t like, and below that is all of the code in the script. I’d appreciate some input, I’m fairly new to programming, I also broke some of the code out so that it would be easier for me to debug, so apologies if it looks bad.
Line that the debugger has a problem with
EdgeVertex[i] = Interpolate(vertexValue1, vertexValue2, EdgeLoc, i);
include “MarchingCubesBasic.h”
include “Math/UnrealMathVectorCommon.h”
include “ProceduralMeshComponent.h”// Sets default values
AMarchingCubesBasic::AMarchingCubesBasic()
{
Voxels.SetNum((CubeAmount + 1) * (CubeAmount + 1) * (CubeAmount + 1));
MeshData.VertexCount = 0;Mesh = CreateDefaultSubobject(“Mesh”);
//Mesh Settings
Mesh->SetCastShadow(false);//Set Mesh As Root
SetRootComponent(Mesh);}
void AMarchingCubesBasic::OnConstruction(const FTransform& Transform)
{
GenerateHeightMap(GetActorLocation() / cubeSize);
March();
GenerateMesh();}
// Called when the game starts or when spawned
void AMarchingCubesBasic::BeginPlay()
{
Super::BeginPlay();}
void AMarchingCubesBasic::GenerateHeightMap(const FVector Position)
{
const int tempHeight = CubeAmount / 2;
for (int x = 0; x <= CubeAmount; ++x)
{
for (int y = 0; y <= CubeAmount; ++y)
{
for (int z = 0; z <= CubeAmount; ++z)
{
if (z <= tempHeight)
{
Voxels[GetIndex(x, y, z)] = 1.0;
}
else
{
Voxels[GetIndex(x, y, z)] = 0.0;
}
}
}
}
}void AMarchingCubesBasic::March()
{
float cube[8];
for (int x = 0; x < CubeAmount; ++x)
{
for (int y = 0; y < CubeAmount; ++y)
{
for (int z = 0; z < CubeAmount; ++z)
{
for (int i = 0; i < 8; ++i)
{
cube[i] = Voxels[GetIndex(x + VertexOffset[i].X, y + VertexOffset[i].Y, z + VertexOffset[i].Z)];
}
GetMeshData(x, y, z, cube);
}
}
}
}void AMarchingCubesBasic::GetMeshData(int X, int Y, int Z, const float Cube[8])
{
int vertexMask = 0;
for (int i = 0; i < 8; ++i)
{
if (Cube[i] <= isoLevel) vertexMask |=1<<i;
}const int edgeMask = CubeEdgeFlags[vertexMask];
if (edgeMask == 0) return;FVector EdgeVertex[12];
for (int i = 0; TriangleConnectionTable[edgeMask][i] != -1; ++i)
{
int EdgeLoc = TriangleConnectionTable[edgeMask][i];
float vertexValue1 = Cube[EdgeConnection[EdgeLoc][0]];
float vertexValue2 = Cube[EdgeConnection[EdgeLoc][1]];EdgeVertex[i] = Interpolate(vertexValue1, vertexValue2, EdgeLoc, i);
}
for (int i = 0; TriangleConnectionTable[edgeMask][i] != -1; i += 3)
{
auto V1 = EdgeVertex[i];
auto V2 = EdgeVertex[i + 1];
auto V3 = EdgeVertex[i + 2];auto normal = FVector::CrossProduct(V2-V1, V3-V1); normal.Normalize(); MeshData.Verticies.Add(V1); MeshData.Verticies.Add(V2); MeshData.Verticies.Add(V3); MeshData.Triangles.Add(MeshData.VertexCount + 0); MeshData.Triangles.Add(MeshData.VertexCount + 1); MeshData.Triangles.Add(MeshData.VertexCount + 2); MeshData.Normals.Add(normal); MeshData.Normals.Add(normal); MeshData.Normals.Add(normal); MeshData.VertexCount += 3;
}
}void AMarchingCubesBasic::GenerateMesh() const
{
Mesh->CreateMeshSection(0, MeshData.Verticies, MeshData.Triangles, MeshData.Normals, MeshData.UV0, TArray(), TArray (), true);
}int AMarchingCubesBasic::GetIndex(int X, int Y, int Z)
{
return Z * (CubeAmount + 1) * (CubeAmount + 1) + Y * (CubeAmount + 1) + X;
}FVector AMarchingCubesBasic::Interpolate(float vertexValue1, float vertexValue2, int edgeLocation, int interation)
{
//return FMath::Lerp(vertexValue1, vertexValue2, 0.5);
float vertexValue = FMath::Lerp(vertexValue1, vertexValue2, 0.5);
FVector vec1 = VertexOffset[EdgeConnection[edgeLocation][0]];
FVector vec2 = EdgeDirection[edgeLocation];
FVector vec3;
vec3.X = vec2.X * vertexValue;
vec3.Y = vec2.Y * vertexValue;
vec3.Z = vec2.Z * vertexValue;
return vec3 * vec1;
}