0
\$\begingroup\$

Using the tutorial: Components and Collision as a guide, to dynamically load a SphereShape the following is done:

AClass::AClass()
{
    // Create USphereComponent
    USphereComponent *sphere = NULL;
    sphere = CreateDefaultSubobject<USphereComponent>(TEXT("Root"));
    sphere->InitSphereRadius(1.0f);
    sphere->SetCollisionProfileName(TEXT("PhysicsActor"));
    sphere->SetSimulatePhysics(true);
    sphere->WakeRigidBody();

    // Create UStaticMeshComponent
    UStaticMeshComponent *mesh = NULL;
    mesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("Mesh"));
    mesh->SetupAttachment(RootComponent);

    // Load asset from filesystem
#define ASSET TEXT("/Game/MobileStarterContent/Shapes/Shape_Sphere.Shape_Sphere")
    static ConstructorHelpers::FObjectFinder<UStaticMesh> asset(ASSET);
#undef ASSET
    if (asset.Succeeded())
        mesh->SetStaticMesh(asset.Object);

    RootComponent = sphere;
}

Now, when I try the same set of steps, but this time for an Actor that has a UDestructibleComponent instead of a USphereComponent:

AClass::AClass()
{
    // Create our UDestructibleComponent
    UDestructibleComponent *destructible = NULL;
    destructible = CreateDefaultSubobject<UDestructibleComponent>(TEXT("Root"));
    destructible->SetCollisionEnabled(ECollisionEnabled::PhysicsOnly);
    destructible->SetSimulatePhysics(true);
    destructible->SetEnableGravity(false);
    destructible->WakeRigidBody(NAME_None);

    // Load asset from filesystem
#define ASSET TEXT("/Game/MobileStarterContent/Shapes/Shape_Cube_DM")
    static ConstructorHelpers::FObjectFinder<UDestructibleMesh> asset(ASSET);
#undef ASSET
    if (asset.Succeeded())
        UE_LOG(LogTemp, Warning, TEXT("It worked!"));

    RootComponent = destructible;
}

I receive the following errors:

ConstructorHelpers.h:105:20: error: cannot initialize a parameter of type 'UObject *' with an lvalue of type 'UDestructibleMesh *'
ValidateObject( Object, PathName, ObjectToFind );
                ^~~~~~

ConstructorHelpers.h:29:19: error: incomplete type 'UDestructibleMesh' named in nested name specifier
UClass* Class = T::StaticClass();
                ^~~

I've also tried adding .Shape_Cube_DM to the end of the string, similar to how the first asset was imported, but no luck. What is the correct way to load a DestructibleMesh from the filesystem and apply it to the UDestructibleComponent?

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

Figured it out - the assets needs to be loaded as a USkeletalMesh, and then casted to a UDestructibleMesh whenever that type is needed.

// Sets default values
ADestructibleBox::ADestructibleBox()
{
    PrimaryActorTick.bCanEverTick = true;

    // Create our UDestructibleComponent
    UDestructibleComponent *destructible = NULL;
    destructible = CreateDefaultSubobject<UDestructibleComponent>(TEXT("Destructible"));
    destructible->SetCollisionEnabled(ECollisionEnabled::PhysicsOnly);
    destructible->SetSimulatePhysics(true);
    destructible->SetEnableGravity(false);
    destructible->WakeRigidBody(NAME_None);

#define ASSET TEXT("/Game/MobileStarterContent/Shapes/Shape_Cube_DM.Shape_Cube_DM")
    static ConstructorHelpers::FObjectFinder<USkeletalMesh> asset(ASSET);
#undef ASSET
    if (asset.Succeeded())
    {
        UE_LOG(LogTemp, Log, TEXT("Asset loading succeeded."));

        meshSkel = Cast<USkeletalMesh>(asset.Object);
        meshDest = Cast<UDestructibleMesh>(meshSkel);

        destructible->SetSkeletalMesh(meshSkel, true);
    }

    RootComponent = destructible;
}
\$\endgroup\$
1
  • 1
    \$\begingroup\$ @MarsonMao Oh yeah, I forgot to come back to it. Waiting 48 hours before being allowed to accept your own answer is stupid. \$\endgroup\$ Feb 2, 2017 at 15:21

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .