0
\$\begingroup\$

Oftenly, when we want to assign asset to the particular component in C++ we write code like that:

.h

UPROPERTY(BlueprintReadWrite)
UStaticMeshComponent* Body = nullptr;

.cpp

Body = CreateDefaultSubobject<UStaticMeshComponent>("Mesh Component");
// find mesh asset
auto MeshForBody = ConstructorHelpers::FObjectFinder<UStaticMesh>(TEXT
                        ("StaticMesh'/Game/StarterContent/Shapes/Shape_Sphere.Shape_Sphere'"));
if (MeshForBody.Object != nullptr)
{// if asset is finded -> set to the component
    Body->SetStaticMesh(MeshForBody.Object);
}

Question: how can I set the PhysicalMaterial in C++ through the reference to the asset in editor? Is it possible?

.h
    UPROPERTY(EditDefaultsOnly)
    UPhysicalMaterial* MyPhysicalMaterial = nullptr;

.cpp

    ???

Find this, but still waiting maybe the best solution without casting exists:

auto PhysicalMaterialAsset = ConstructorHelpers::FObjectFinder<UObject>(TEXT(
         "PhysicalMaterial'/Game/PhysicsMaterials/MyPhysicsMaterial.MyPhysicsMaterial'"));

if (PhysicalMaterialAsset.Object) 
{ 
    Sphere->BodyInstance.SetPhysMaterialOverride((UPhysicalMaterial*)PhysicalMaterialAsset.Object); 
} 

THNX for any tips in advance ;)

\$\endgroup\$
4
  • \$\begingroup\$ Find this, but still waiting maybe the best solution without casting: auto PhysicalMaterialAsset = ConstructorHelpers::FObjectFinder<UObject>(TEXT( "PhysicalMaterial'/Game/PhysicsMaterials/MyPhysicsMaterial.MyPhysicsMaterial'")); if (PhysicalMaterialAsset.Object) { Sphere->BodyInstance.SetPhysMaterialOverride((UPhysicalMaterial*)PhysicalMaterialAsset.Object); } \$\endgroup\$
    – Anton
    Mar 19 '20 at 12:29
  • \$\begingroup\$ Please consider adding this information into your question. Assume no one reads comments. ;) \$\endgroup\$
    – DMGregory
    Mar 19 '20 at 13:21
  • 1
    \$\begingroup\$ Why are you using FObjectFinder<UObject> rather than FObjectFinder<UPhysicalMaterial>? \$\endgroup\$
    – Rotem
    Apr 7 '20 at 3:10
  • \$\begingroup\$ Due to the second option gives an error. I wrote before, you can find more about the same problem here: reddit thread. I think, eventually, this error relates with FObjectFinder<T> template implementation. \$\endgroup\$
    – Anton
    Apr 8 '20 at 10:30
0
\$\begingroup\$

The easiest way is what you've done, then set the material in the Blueprint of whatever the material is attached to. Back in your .cpp file, you can then access the material.

A word of warning though, if you're trying to change any settings and you haven't assigned anything in the Blueprint, Unreal will return the nullptr and crash the editor. To avoid this, always start with:

if (MyPhysicalMaterial) {

This will check if there is something assigned to the material reference before attempting any changes.

\$\endgroup\$
2
  • \$\begingroup\$ It is not the answer to the question. Or maybe I have a bad description. I know how to use it via Blueprint. The question is how to create reference to the physical asset, which was created in the editor? FObjectFinder is not useful for that as an example \$\endgroup\$
    – Anton
    Mar 19 '20 at 11:42
  • \$\begingroup\$ Finded the same problem here: reddit.com/r/unrealengine/comments/45lvmh/… But solution with casting seems not the best \$\endgroup\$
    – Anton
    Mar 19 '20 at 11:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.