Download

Please help me ,How do create SkeletarMesh to SkeletarMeshComponent in C++?

I create a Character in C++,It’s has a GetMesh() Methods. So,I can get a SkeletarMeshComponent. I want to set a SkeletarMesh to SkeletarMeshComponent. How Do it???

USkeletarMesh * skeletarMesh = newObject<UskeletarMesh>();// I want to init wiht skeletarMesh .How do it?
GetMesh()->skeletarMesh=skeletarMesh ;

Normally, you create the skeletal mesh component in C++ and assign which skeletal mesh it uses in the editor using a Blueprint version of the C++ class.

If you want to assign the skeletal mesh in C++, you will need to knows its path and then use ConstructorHelpers::FObjectFinder() in constructor or LoadObject() during runtime.

After that, yes, its just GetMesh()->SkeletalMesh = NewSkeletalMeshToUse;

The first person template has this… I would strongly advise creating some projects from the template C++ projects to learn more



// CONSTRUCTOR
ACleanerCharacter::ACleanerCharacter()
{
	// ... etc
	// 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"));
	Mesh1P->SetOnlyOwnerSee(true);
	Mesh1P->AttachParent = FirstPersonCameraComponent;
	// ... etc
}


These meshes use properties, so the references are set in the editor.

If you want to load the mesh from code as well, the easiest way is to use ConstructorHelpers as explained above. Some example code using a static mesh and material instance…



// CONSTRUCTOR
{
	struct FConstructorStatics
	{
		ConstructorHelpers::FObjectFinderOptional<UStaticMesh> SkyMesh;
		ConstructorHelpers::FObjectFinderOptional<UMaterialInstance> SkyMaterial;
		FConstructorStatics()
			: SkyMesh(TEXT("/Game/Tutorial/Mesh/skybox.skybox"))
			, SkyMaterial(TEXT("/Game/Tutorial/Material/MI_Sky.MI_Sky")) //MI_Sky
		{
		}
	};
	static FConstructorStatics cs;
	SkyMesh = cs.SkyMesh.Get();
	SkyMaterial = cs.SkyMaterial.Get();
}