x

Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

c++ Set Mesh and Material For Hierarchical Instanced Static Mesh

Hi, I'm spawning HISM's and can set my mesh vie the Main Editor (details) in the engine by manually selecting the mesh.

What I'm after is just being able to set the mesh in the c++ code.

.cpp

         MeshInstancesGroundTile50cm = ObjectInitializer.CreateDefaultSubobject<UHierarchicalInstancedStaticMeshComponent>(this, TEXT("MeshInstancesGroundTile50cm"));

......... ......

                                 FVector resSpawnLocation = FVector(   ((resj / 3) *10)+ (((j / 3) * 1000)-1500), ((resheight - resi)*10)+ (((height - i) * 1000)-1500), 200.0f    );
                                 //FRotator SpawnRotation = FRotator(90.0f, 90.0f, 90.0f);
 
                                 ResInstanceTransform.SetLocation(resSpawnLocation);
                                 ResInstanceTransform.SetRotation(FQuat::MakeFromEuler({ 0.0f, 0.0f,0.0f }));
                                 
                                 //MeshInstancesGroundTile50cm->SetStaticMesh(GroundTile50cm);
                                 MeshInstancesGroundTile50cm->AddInstance(ResInstanceTransform);


.h

     UPROPERTY(EditAnywhere, BlueprintReadWrite)
         class UHierarchicalInstancedStaticMeshComponent* MeshInstancesGroundTile50cm;


Unfortunately I get identifier GroundTile50cm is undefined.

I just want to be ablt to set the mesh to use and the material instance, anyone know how to set this correctly?

Cheers

Product Version: UE 4.21
Tags:
more ▼

asked Mar 03 '19 at 06:13 PM in C++ Programming

avatar image

Jim Clow
44 2 10 10

avatar image Jim Clow Mar 03 '19 at 06:57 PM

Within .cpp I've tried adding

 #include "Runtime/CoreUObject/Public/UObject/ConstructorHelpers.h"

and using..

 auto MeshAsset = ConstructorHelpers::FObjectFinder<UStaticMesh>(TEXT("StaticMesh'/Game/Meshes/Terrain/GroundTile50cm.GroundTile50cm'"));
 
         if (MeshAsset.Object != nullptr)
         {
             MeshInstancesGroundTile50cm->SetStaticMesh(MeshAsset.Object);
         }

But I can't see my spaned tiles, suggesting that the mesh isn't getting set.

I've right clicked on the asset and copied the reference, so I know the path should be ok.

When I run this I can see my array of HISM is created, 30+ of them, but the mesh element is still empty.

         if (MeshAsset.Object != nullptr)
         {
             GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Yellow, TEXT("Mesh found!"));
             MeshInstancesGroundTile50cm->SetStaticMesh(MeshAsset.Object);
         }

The text "Mesh found!" appears.

If I run..

         if (MeshAsset.Object == nullptr)
         {
             GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Yellow, TEXT("Mesh not found!"));
                   //MeshInstancesGroundTile50cm->SetStaticMesh(MeshAsset.Object);
         }

No text appears. So it seems pretty conclusive that it finds the floor tile mesh

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

1 answer: sort voted first
 UStaticMesh* Mesh = LoadObject<UStaticMesh>(nullptr, TEXT("StaticMesh'/Game/Meshes/Terrain/GroundTile50cm.GroundTile50cm'"));
                                 MeshInstancesGroundTile50cm->SetStaticMesh(Mesh);

This method works fine, the issue with the other method seems to be that the variable isn't accessible outside of the constructor.

more ▼

answered Mar 03 '19 at 09:24 PM

avatar image

Jim Clow
44 2 10 10

(comments are locked)
10|2000 characters needed characters left
Viewable by all users
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question