Change USkeletalMesh to UPoseableMesh in C++

Hello

In simple words, my aim is to simply change USkeletalMeshComponent into UPoseableMeshComponent. I need it to do in C++, not in BP.

Here I use First Person C++ Template and change code of

MyProjectCharacter.h file:

class AMyProjectCharacter : public ACharacter
{
	GENERATED_BODY()

	/** Pawn mesh: 1st person view (arms; seen only by self) */
	UPROPERTY(VisibleDefaultsOnly, Category=Mesh)
	class USkeletalMeshComponent* Mesh1P;
(...)
	
	public:
	/** Returns Mesh1P subobject **/
	FORCEINLINE class USkeletalMeshComponent* GetMesh1P() const { return Mesh1P; }
(...)
}

into:

class AMyProjectCharacter : public ACharacter
{
	GENERATED_BODY()

	/** Pawn mesh: 1st person view (arms; seen only by self) */
	UPROPERTY(VisibleDefaultsOnly, Category=Mesh)
	class UPoseableMeshComponent* Mesh1P;
(...)
	
	public:
	/** Returns Mesh1P subobject **/
	FORCEINLINE class UPoseableMeshComponent* GetMesh1P() const { return Mesh1P; }	
(...)
}

and MyProjectCharacter.cpp file:

AMyProjectCharacter::AMyProjectCharacter()
{
(...)
    // Create a mesh component that will be used when being viewed from a '1st person' view (when controlling this pawn)

   Mesh1P = CreateDefaultSubobject<USkeletalMeshComponent>(TEXT("CharacterMesh1P"));

(...)
}

into:

AMyProjectCharacter::AMyProjectCharacter()
 {
 (...)
     // Create a mesh component that will be used when being viewed from a '1st person' view (when controlling this pawn)

    Mesh1P = CreateDefaultSubobject<UPoseableMeshComponent>(TEXT("CharacterMesh1P"));

 (...)
 }

Unfortunately it’s not as simple as it might seem. That modification creates errors, that I can’t solve with my knowledge.

I’ll be very grateful for your help.

What you’re trying to do won’t work the way you’ve been trying to go about it.

Taking things back a step, it appears you’re trying to make a custom pawn actor that works like the ACharacter class but uses a poseable mesh instead of a skeletal mesh. Part of the problem is, the ACharacter class has its own USkeletalMeshComponent built in to it, which means if you derive your custom pawn from ACharacter, your subclass is going to have a USkeletalMeshComponent by default.

You’re probably going to need to disable the ACharacter’s USkeletalMeshComponent so you can add a UPoseableMeshComponent in its place.

To do that, you’re going to need to use the FObjectInitializer, along with the DoNotCreateDefaultSubobject function, in the constructor for your custom character class. Here’s how your updated constructor would look in your .cpp file:

AMyCustomCharacter::AMyCustomCharacter(const FObjectInitializer& ObjectInitializer)
	: Super(ObjectInitializer.DoNotCreateDefaultSubObject(ACharacter::MeshComponentName)))
{
    // Regular constructor stuff goes here
}

That will prevent the character’s default USkeletalMeshComponent from being created when your actor is constructed.

Now you just need to add back in your desired UPoseableMeshComponent, much like you were already doing. Define your UPoseableMeshComponent in your header file:

UPROPERTY(VisibleDefaultsOnly, Category=Mesh)
class UPoseableMeshComponent* PoseableMesh;

And then put something like this in your updated constructor:

PoseableMesh = CreateDefaultSubobject<UPoseableMeshComponent>(TEXT("CharacterPoseableMesh"))

(Like I said, you already had this part figured out, so it should look familiar.)

With that, your custom character should have a poseable mesh component instead of its default skeletal mesh component.

Of course, you can also add back in your getter functions as desired.

Hopefully this gets you closer to a working solution!

Thank you for the answer. Unfortunately, I still have problem with compilation and this is my log:

[1/3] MyProjectCharacter.cpp
 C:\UE_4.21\Engine\Source\Runtime\CoreUObject\Public\UObject/Object.h(84): error C2027: use of undefined type 'UPoseableMeshComponent'
 d:\myproject\source\myproject\MyProjectCharacter.h(22): note: see declaration of 'UPoseableMeshComponent'
 d:\MyProject\Source\MyProject\MyProjectCharacter.cpp(39): note: see reference to function template instantiation 'TReturnType *UObject::CreateDefaultSubobject<UPoseableMeshComponent>(FName,bool)' being compiled
            with
            [
                TReturnType=UPoseableMeshComponent
            ]
    C:\UE_4.21\Engine\Source\Runtime\CoreUObject\Public\UObject/Object.h(85): error C2440: 'static_cast': cannot convert from 'UObject *' to 'TReturnType *'
            with
            [
                TReturnType=UPoseableMeshComponent
            ]
    C:\UE_4.21\Engine\Source\Runtime\CoreUObject\Public\UObject/Object.h(85): note: Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast

Did you include Components/PoseableMeshComponent.h in your .cpp file?