Trying to create a physical material class in .h and reference a physical material from blueprint to c++

I am trying to reference a physical material from bps to c++. I am getting a unresolved external error. I don’t know what is wrong because at first class the debugger does not throw an error until compile.
I don’t know if it is because if I am declaring the class improperly or declaring the reference improperly or I did not include a .h. I have checked this: unreal 4 - UE4. How to create a reference to the physical material asset (which I create in editor) in C++ - Game Development Stack Exchange post and for some reason I still can’t get the build to not fault.

//.h
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Phys")
		class UPhysicalMaterial* Phys;
//.cpp
#include "Chaos/PhysicalMaterial.h"
//Constructor
AWeaponBase::AWeaponBase()
{
	PrimaryActorTick.bCanEverTick = true;
       // Here I am referencing the bp physical mat.
	static ConstructorHelpers::FObjectFinder<UPhysicalMaterial> physMat
	(TEXT("PhysicalMaterialReferencePath '/SpecifiedPathForPhysicalMat/' "));
        // Checking if the reference has succeeded. 
	if (physMat.Succeeded())
	{
		Phys = physMat.Object;
	}
	
}
//Fire function.
void AProjectileActor::Fire()
{
	FCollisionResponseParams ResponseParams;
		FCollisionQueryParams QueryParams = FCollisionQueryParams(SCENE_QUERY_STAT(WeaponTrace), false, this);
		FActorSpawnParameters SpawnParams;
               //Telling the Query params to return physical material.
		QueryParams.bReturnPhysicalMaterial = true;
               ///
		SpawnParams.SpawnCollisionHandlingOverride = ESpawnActorCollisionHandlingMethod::AlwaysSpawn;
		QueryParams.AddIgnoredComponent(BaseMesh);
		TArray<FHitResult> Hit;
		const FVector StartTrace = WeaponBaseMesh->GetSocketLocation(FName("Socket"));
		FRotator CurrentRotation = WeaponBaseMesh->GetSocketRotation(FName("Socket"));
		 FVector EndTrace = StartTrace + CurrentRotation.Vector() * 7000.0f;
		if (GetWorld()->LineTraceMultiByChannel(Hit, StartTrace, EndTrace, ECollisionChannel::ECC_TraceChannel, QueryParams, ResponseParams))
{
         for(&FHitResult Results : Hit)
         {
//Checking if actor was hit. 
      if (AActor* Actor = Cast<AActor>(Results.GetActor()))
					{
						FVector DecalSize(15.0f, 15.0f, 15.0f);
						FRotator RandomDecalRotation = Results.TraceEnd.Rotation();
						FVector DecalLocation = Results.Location;
                                                //Trying to get the material reference through a FHitResult
                                                //This line does not return an error, yet on compile the engine throws 
                                                //unresolved external errors. I don't know if it is because of how I am declaring the variable in the .h or...
						if (Results.PhysMaterial.Get() == Phys)
						{
						
						}
					}
                   }
        }
}


Unresolved external issues usually mean you’re missing library includes. Check your [PROJECTNAME].Build.cs file for missing modules in the PublicDependencyModuleNames and PrivateDependencyModuleNames arrays. My guess is something related to Chaos. We’d have to see the actual error messages.

1 Like

Here are the errors upon build.

What modules would I need to place? I sent a screen shot of the error messages.

I need to also ask… How I am creating the physical material in C++ is the syntax correct, and if I am making a form of error?

I found the modules in the build.cs I don’t know what to include into the modules in order to use the physical materials.

I am also using 4.26.2 I am not sure if chaos supports this version and how would I go about fixing it.

Does that mean I have to upgrade unreal engine to the chaos build? If possible could please tell me what I need to get chaos physical materials to work on 4.26 without the preview build?

It looks about correct to me, but it’s not the recommended way to do it. The reason is that whenever the path to the asset changes, you have to go through your code and hunt for the strings referencing the paths in order to update them. What you’re supposed to do is create a Blueprint asset that inherits from your C++ base class and set the Material in the editor. That’s what the Edit[XXX] specifiers on UPROPERTYs are for, after all.

I’m going to show you two ways to find the module name you need to include in the build files for any given header.

First: Do a Google search for the class. Click the result that takes you to the UE API documentation. You can also navigate straight to the docs if you know the URL. The top of the page will always look something like this:


Do you see where it says “Module”? That’s the module name you have to include in the .Build.cs file.

Second: In the IDE of your choice, do a global search for the class and find the header file where the class is defined. Pro tip for finding UClass definitions quickly is using search strings like uphysicalmaterial : p because it’s a pattern that is found almost exclusively for class definitions of the form
“class SOMETHINGSOMETHING_API UClassName : public UBaseClass”.

Once you found the file, walk up the hierarchy until you reach the module that owns it. You now know the name of the module to include.

I haven’t worked with Chaos a day in my life, so I can’t answer that. I’m pretty sure there are tons of tutorials on how to get Chaos working for UE4, though.

2 Likes

I have tried adding the module to my editor target and still the externals would not resolve… I have also added the libraries too. For some reason I still get errors upon build even though the syntax is correct is this a bug?

I did everything he said and it worked this post can now be closed.