Announcement

Collapse
No announcement yet.

Set Static Mesh in C++

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

    Set Static Mesh in C++

    I wont to Set my Mesh with C++ cood. The problem is, it don't work. I've already tried, to set the mesh, in the constructor, "BeginPlay", and in the Tick function.

    Code:
    static FConstructorStatics ConstructorStatics;
    
    DummyRoot = CreateDefaultSubobject<USceneComponent>(TEXT("Dummy0"));
    RootComponent = DummyRoot;
    
    GhostMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("GhostMesh0"));
    GhostMesh->SetupAttachment(DummyRoot);
    GhostMesh->SetStaticMesh(ConstructorStatics.TestBuildin_Finel.Get());
    If I create a Blueprint from my C++ Class, and try to set it in the Bluprints, it work.
    Click image for larger version

Name:	BP.PNG
Views:	1
Size:	39.1 KB
ID:	1220663

    I have no idea, why is don't work, in C++.

    #2
    You're not using Constructor Statics correctly, you need to actually find the object by it's string asset reference, tbh I'm not sure how that even compiles.

    Code:
    	static ConstructorHelpers::FObjectFinder<UStaticMesh>MeshAsset(TEXT("StaticMesh'/Game/MyMesh.MyMesh"));
    	UStaticMesh* Asset = MeshAsset.Object;
    
            GhostMesh->SetStaticMesh(Asset);
    You can 'get' the string reference of an asset by right-clicking it and hitting "Copy Reference".

    However, if that mesh never changes during gameplay, then why not just set it directly on the mesh component when you create a blueprint of that class. Other than for specific cases it's not a good workflow to reference assets this way.

    Comment


      #3
      Thank you, TheJamsh !!!!

      Comment


        #4
        Originally posted by therealjameswu View Post
        Thank you, TheJamsh !!!!
        Or you can use the way like this https://forums.unrealengine.com/deve...-runtime-via-c

        Comment

        Working...
        X