Procedural Mesh Component Set To Nullptr after Being Originally Set in The Constructor

Hi there,

Struggling here to what seems like a simple problem. I’ve set up a function to collect the required variables to generate terrain mesh. But when i press play on the editor it always crashes on the CreateMeshSection() method from the procedural mesh component:

The error message highlights the line where the create method is called. As the error is an exception_access_violation, i thought to check whether the component the method is called on is set.

Screenshot 2024-09-04 070352

Turns out it isn’t since this is still called.

This is confusing to me as in the constructor I’ve set the component value (I’ve tried with or without setting up the attachment):

I’ve tried marking the variable as EditAnywhere in the Uproperty Section. I’ve also tried deleting and regenerating my project files.

Why is does this procedural mesh component variable have a value of nullptr when I’ve set the value of it above in the constructor? Is there something I’ve missed? The generate function this is contained in is called in blueprint, but all of the logic is done in C++.

Thanks for taking the time to read.

In your create mesh section you have the wrong if declaration.
You can’t be testing if equal to nullptr and then calling create mesh section. You need to test if it’s NOT nullptr, which means that it’s actually initialized and of type UProceduralMeshComponent.

So it’s just a typo on your end.

Try

if(_ProcTerrain != nullptr){
_ProcTerrain->CreateMeshSections( rest of parameters here )
}

That way the create mesh section will be called if the initialization in the cdo is correct :slight_smile:

Hi there, Thanks for your reply.

I’m not sure if i’ve misunderstood but the reason i tested to see if component was equal to nullptr was simply to check if it’s value had been assigned in the constructor. The main issue that I’m having is that the variable is not initialized when I think it should be.

If i do:

//Create mesh section
	if(_ProcTerrain != nullptr)
	{
		GEngine->AddOnScreenDebugMessage(-1, 10.f, FColor::Cyan, "Called here");
		_ProcTerrain->CreateMeshSection(MeshSectionIndex, subVerticies, subTriangles, subNormals, subUVs, TArray<FColor>(), subTangents, true);
	}

The code won’t run as the if condition isn’t met

Did you add the module ProceduralMeshComponent to your build file?

Where are you calling it? Try moving it to post load

void AWorldGenerator::PostLoad()
{
	Super::PostLoad();

if(_ProcTerrain != nullptr)
	{
		GEngine->AddOnScreenDebugMessage(-1, 10.f, FColor::Cyan, "Called here");
		_ProcTerrain->CreateMeshSection(MeshSectionIndex, subVerticies, subTriangles, subNormals, subUVs, TArray<FColor>(), subTangents, true);
	}

}

(just add the override in the header file for it)

Example blog showing it working

https://nerivec.github.io/old-ue4-wiki/pages/procedural-mesh-component-in-cgetting-started.html

Yeah I’ve added it to the build file a while back, as below:

PublicDependencyModuleNames.AddRange(new string[] { "Core", "CoreUObject", "Engine", "InputCore", "EnhancedInput", "ProceduralMeshComponent" });

I’ve also got no errors building from my IDE so it looks to be included correctly. I’ve the ProceduralMeshComponent.h at the top of the cpp and the class declared in the header.

The function is called in blueprint on begin play. It’s called within a nested for loop since i’m generating multiple sections of terrain.

Try and load the tobjectptr with the get command

_ProcTerrain.Get()->CreateMeshSection(MeshSectionIndex, subVerticies, subTriangles, subNormals, subUVs, TArray<FColor>(), subTangents, true);

I’m still not used to TObject pointers since the older versions were more straightforward. I sometimes forget that you need to call get to actually init them.

If the _ProcTerrain remains empty then try creating it with a NewObject command.

2 Likes

Used the .Get() method and that worked for me! Thanks for your help.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.