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 ;)
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:29FObjectFinder<UObject>
rather thanFObjectFinder<UPhysicalMaterial>
? \$\endgroup\$ – Rotem Apr 7 '20 at 3:10