Setting Skeletal Mesh through C++

Partly for practice and partly because I feel that for the current character I am coding it would be beneficial to have it set through code rather than through blueprint, I am trying to set the skeletalmesh and other parts of the skeletalmeshcomponent of my main character through c++ rather than through blueprint. I reckon that by being able to do this, I should be able to use that knowledge for spawning other things through C++ as well. I am still rather new to the whole C++ thing, with my only experience so far being in Unrealscript.


CPP:

static ConstructorHelpers::FObjectFinder<USkeletalMesh> MeshContainer(TEXT("SkeletalMesh'/Game/ThirdPerson/Character/ThirdPersonSkeletalMesh.ThirdPersonSkeletalMesh'"));
	if (MeshContainer.Succeeded())
	{
		PlayerMesh = GetMesh();
		PlayerMesh->SetSkeletalMesh(MeshContainer);
	}	


H:

	UPROPERTY(VisibleAnywhere, Category = SkeletalMesh)
	class USkeletalMesh* MeshContainer;

	UPROPERTY(VisibleAnywhere, Category = SkeletalMesh)
	class USkeletalMeshComponent* PlayerMesh;

If I write it like this, I get the following error;


Error	1	error C2664: 'void USkeletalMeshComponent::SetSkeletalMesh(USkeletalMesh *)' : cannot convert argument 1 from 'ConstructorHelpers::FObjectFinder<USkeletalMesh>' to 'USkeletalMesh *'

I have looked around to find people with similar issues, but I couldn't find anything that addressed my issue or was enough to help me solve it. Doesn't really help either that most topics on that subject are rather old as well, using syntax that has since been deprecated. Can anyone explain what exactly I am doing wrong here?


BP Asset Link

I assume your main character class has a blueprint in the Editor that you are using with game mode to spawn your character.

If you do indeed have a blueprint of the related class, you can just add the asset link in your .h file directly rather than hardcoding the lookup, which can and frequently does fail during packaging.

Step 1:



UPROPERTY(EditAnywhere, BlueprintReadWrite, Category="Assets")
USkeletalMesh* AlternateMeshAsset;


then in cpp file

Step 2:
if()pollllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllll <~~~ cat on keyboard



if(AlternateMeshAsset)
{
  PlayerMesh->SetSkeletalMesh(AlternateMeshAsset);
}


Step 3:
Then you set the asset ptr in the Editor and you’re done!

I am a huge fan of doing stuff purely in C++, however asset references are one thing I always do via the editor because hardcoded asset references can break during packaging and make it a pain to move/rename c++ referenced assets.

:slight_smile:

Rama

4 Likes

Ah, the breaking part sounds like a pain. I will keep that in mind!

I was actually doing that for the character before I started with trying to set it directly in C++, so if the hardcoding can cause issues, I might as well just stick with my initial code :slight_smile: Thanks!

I removed this line and it works “static ConstructorHelpers::FObjectFinder<USkeletalMesh> MeshContainer(TEXT(“SkeletalMesh’/Game/ThirdPerson/Character/ThirdPersonSkeletalMesh.ThirdPersonSkeletalMesh’”));”

setting skeletalmesh crashes my editor?!

1 Like

Change “PlayerMesh->SetSkeletalMesh(MeshContainer);”
to “PlayerMesh->SetSkeletalMesh(MeshContainer.Object)”