Announcement

Collapse
No announcement yet.

Setting Skeletal Mesh through C++

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

    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.

    Code:
    CPP:
    
    static ConstructorHelpers::FObjectFinder<USkeletalMesh> MeshContainer(TEXT("SkeletalMesh'/Game/ThirdPerson/Character/ThirdPersonSkeletalMesh.ThirdPersonSkeletalMesh'"));
    	if (MeshContainer.Succeeded())
    	{
    		PlayerMesh = GetMesh();
    		PlayerMesh->SetSkeletalMesh(MeshContainer);
    	}
    Code:
    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;
    Code:
    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?

    #2
    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:
    Code:
    UPROPERTY(EditAnywhere, BlueprintReadWrite, Category="Assets")
    USkeletalMesh* AlternateMeshAsset;
    then in cpp file

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

    Code:
    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.



    Rama
    Last edited by Rama; 06-02-2015, 11:56 PM.
    100+ UE4 C++ Tutorials on the UE4 Code Wiki, including UE4 Multi-Threading!

    UE4 Marketplace: Melee Weapon Plugin & Compressed Binary Save System Plugin | Rama's C++ AI Jumping Videos | Vertex Snap Editor Plugin

    Visit www.ue4code.com to see lots of videos about my C++ Creations! ♥ Rama

    Comment


      #3
      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 Thanks!

      Comment


        #4
        I removed this line and it works "
        static ConstructorHelpers::FObjectFinder<USkeletalMesh> MeshContainer(TEXT("SkeletalMesh'/Game/ThirdPerson/Character/ThirdPersonSkeletalMesh.ThirdPersonSkeletalMesh'"));"

        Comment


          #5
          setting skeletalmesh crashes my editor?!

          Comment

          Working...
          X