Set Mesh from ACharacter in child class

Hello.
ACharacter class contains USkeletalMeshComponent by default.

UPROPERTY(Category=Character, VisibleAnywhere, BlueprintReadOnly, meta=(AllowPrivateAccess = "true"))
TObjectPtr<USkeletalMeshComponent> Mesh;

My APlayerCharacter class inherits from ACharacter. I want to set Mesh (inherited from ACharacter) in APlayerCharacter.

PlayerMesh in PlayerCharacter.h:

// Player mesh component
UPROPERTY(EditAnywhere, Category = "Components")
USkeletalMeshComponent* PlayerMesh;

Setting PlayerMesh in PlayerCharacter.cpp:

// Set Player mesh
PlayerMesh = GetMesh();
PlayerMesh->SetupAttachment(PlayerCapsule);

static ConstructorHelpers::FObjectFinder<USkeletalMesh> PlayerMeshAsset(TEXT
("/Game/PlayerCharacter/SKM_PlayerCharacter"));

if (PlayerMeshAsset.Succeeded())
{
	PlayerMesh->SetSkeletalMesh(PlayerMeshAsset.Object);
	PlayerMesh->SetRelativeLocation(FVector(0.0f, 0.0f, -90.0f));
	PlayerMesh->SetRelativeRotation(FQuat(FRotator(0.0f, -90.0f, 0.0f)));
	PlayerMesh->SetRelativeScale3D(FVector(1.0f, 1.0f, 1.0f));
}

This is the only idea I had, but it doesn’t work. Is there a way to do this without using blueprints?

You should right click on your mesh from your content browser, and select Copy Reference, then paste the copied text inside your TEXT macro

Other than that, you don’t need the PlayerMesh variable on your child class, you have full access to it from ACharacter through GetMesh()

Lastly, what’s the reason behind not wanting to use blueprints? It can be very convenient for cases like this, especially when you probably want to see what your actor looks like without having to recompile every time you make a change.
A blueprint will also prevent issues caused by for example renaming the mesh asset or moving it around, since the reference will be kept track of and updated in such a case

1 Like

I am still learning Unreal. Recently I’ve started working on new project with C++ only, but I can already see many disadvantages of that approach. Thank you for solution and tips, I guess I will eventually create some BPs based on cpp classes.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.