What wrong in my program Hierarchical instance Mesh c++ 4.20

i create Hierarchical instance Mesh and now for accelarate i’m use c++
on the 10th try to perform o better nothing is displayed at the worst it crash

.h of class
// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "CoreMinimal.h"
#include "PickFolliage.h"
#include "Runtime/Engine/Classes/Components/HierarchicalInstancedStaticMeshComponent.h"
#include "GameFramework/Actor.h"
#include "TestingFoliageClass.generated.h"


UCLASS()
class FOLIAGETESTCPP420_API ATestingFoliageClass : public AActor
{
	GENERATED_BODY()
		//UFUNCTION(BlueprintPure, Category = "SunShine")
		UFUNCTION(BlueprintCallable, Category = "SunShine")
		void ActivateSunShine(TArray<FName>& FnameArray);

	

public:	
	// Sets default values for this actor's properties
	ATestingFoliageClass();

protected:
	UPROPERTY(EditDefaultsOnly, BlueprintReadWrite, Category = "Part Properties")
	UStaticMesh * Mesh;
	
	// Called when the game starts or when spawned
	virtual void BeginPlay() override;
	UHierarchicalInstancedStaticMeshComponent* hismc;
	
public:	
	// Called every frame
	virtual void Tick(float DeltaTime) override;

	
	
};

.cpp:

#include "TestingFoliageClass.h"
#include "Runtime/Engine/Classes/Components/SkeletalMeshComponent.h"
#include "Kismet/KismetSystemLibrary.h"
#include "Runtime/Engine/Classes/Kismet/GameplayStatics.h" 

 void ATestingFoliageClass::ActivateSunShine(TArray<FName>& FnameArray)
{
	TSubclassOf<APickFolliage> classToFind;
	classToFind = APickFolliage::StaticClass();
	TArray<AActor*> foundEnemies; 
	UGameplayStatics::GetAllActorsOfClass(GetWorld(), classToFind, foundEnemies);
	FString IntAsString = FString::FromInt(foundEnemies.Num());
	
		
		hismc->ClearInstances();
		

	for (int32 i = 0; i < foundEnemies.Num(); ++i) {
		APickFolliage* mypickf=	Cast<APickFolliage>(foundEnemies[i]);


		
		hismc->AddInstance(mypickf->GetActorTransform());
	

		const TArray<FName>& FnameArrayBone  =  mypickf->GetRootComponent()->GetAllSocketNames();
		
		for (int32 j = 0; j <FnameArrayBone.Num(); ++j) {

			
			if (FnameArrayBone[j].ToString().Contains("_end")) {
				FnameArray.Add(((USkeletalMeshComponent*)mypickf->GetRootComponent())->GetParentBone(FnameArrayBone[j]));

			}
			
		}

	}

	//return FnameArray;
}

// Sets default values
ATestingFoliageClass::ATestingFoliageClass()
{
 	

	CreateDefaultSubobject<UHierarchicalInstancedStaticMeshComponent>(TEXT("hismc001"));
	hismc->SetupAttachment(GetRootComponent());
	hismc->RegisterComponent();
	
	hismc->SetStaticMesh(Mesh);
	hismc->SetHiddenInGame(false);
	hismc->SetSimulatePhysics(false);
	hismc->SetFlags(RF_Transactional);
	hismc->SetCollisionEnabled(ECollisionEnabled::NoCollision);

	

}

// Called when the game starts or when spawned
void ATestingFoliageClass::BeginPlay()
{
	Super::BeginPlay();
	
}

// Called every frame
void ATestingFoliageClass::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);

}

what am I doing wrong or what am I not doing that needs to be done thanks for help

error of copy
the ligne is
hismc = CreateDefaultSubobject(TEXT(“hismc001”));
i have clean the code for paste

Well the return value of your CreateDefaultSubobject() is never assigned to your var.

hismc->SetStaticMesh(Mesh); this is wrong as Mesh has never being initialized; You need to Mesh = CreateDefaultSubobject<UStaticMesh>(TEXT("Mesh")); before you can pass it as an argument.

the ligne is

hismc = CreateDefaultSubobject<UHierarchicalInstancedStaticMeshComponent>(TEXT("hismc001"));

UPROPERTY(EditDefaultsOnly, BlueprintReadWrite, Category = “Part Properties”)
UStaticMesh * Mesh;
the mesh is configured in blueprint

ok my constructor

ATestingFoliageClass::ATestingFoliageClass()
{
	PrimaryActorTick.bCanEverTick = true;

	Mesh = CreateDefaultSubobject<UStaticMesh>(TEXT("Mesh"));
	hismc = CreateDefaultSubobject<UHierarchicalInstancedStaticMeshComponent>(TEXT("hismc001"));
	hismc->SetupAttachment(GetRootComponent());
	

	hismc->SetStaticMesh(Mesh);
	hismc->RegisterComponent();
	hismc->SetHiddenInGame(false);
	
	
	hismc->ClearInstances();
	

	hismc->AddInstance(this->GetActorTransform());
	


}

and in my .h

public:	
	// Called every frame
	virtual void Tick(float DeltaTime) override;
	UHierarchicalInstancedStaticMeshComponent* hismc;
	UPROPERTY(EditDefaultsOnly, BlueprintReadWrite, Category = "Part Properties")
		UStaticMesh * Mesh;

this objet in the word editor position all 0rotation all 0 scale all 1

no static mesh show

RootComponent = hismc; instead of hismc->SetupAttachment(GetRootcomponent());

with changement always not fonctional
RootComponent = hismc;

i have tested a new project 4.20 just for hierarchical instance mesh without other code and i just crash

im here :
cpp

// Fill out your copyright notice in the Description page of Project Settings.

#include "HismcTest.h"


// Sets default values
AHismcTest::AHismcTest()
{
 	// Set this actor to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
	PrimaryActorTick.bCanEverTick = true;
	Mesh = CreateDefaultSubobject<UStaticMesh>(TEXT("Mesh"));
	hismc = CreateDefaultSubobject<UHierarchicalInstancedStaticMeshComponent>(TEXT("hismc001"));
	RootComponent = hismc;

	hismc->SetStaticMesh(Mesh);
	hismc->RegisterComponent();
	hismc->SetHiddenInGame(false);


	hismc->ClearInstances();
	hismc->AddInstance(this->GetActorTransform());

}

// Called when the game starts or when spawned
void AHismcTest::BeginPlay()
{
	Super::BeginPlay();
	
}

// Called every frame
void AHismcTest::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);

}

.h:

#pragma once

#include "CoreMinimal.h"
#include "Runtime/Engine/Classes/Components/HierarchicalInstancedStaticMeshComponent.h"
#include "Runtime/Engine/Classes/Engine/StaticMesh.h"
#include "GameFramework/Actor.h"
#include "HismcTest.generated.h"

UCLASS()
class HIERACHICALIMC420CPP_API AHismcTest : public AActor
{
	GENERATED_BODY()
	
public:	
	// Sets default values for this actor's properties
	AHismcTest();

protected:
	// Called when the game starts or when spawned
	virtual void BeginPlay() override;

public:	
	// Called every frame
	virtual void Tick(float DeltaTime) override;
	UHierarchicalInstancedStaticMeshComponent* hismc;
	UPROPERTY(EditDefaultsOnly, BlueprintReadWrite, Category = "Part Properties")
		UStaticMesh * Mesh;
	
	
};

So I was able to launch without crashing by removing hismc->RegisterComponent()

I’m supposed to do what to make it work in OnConstruction ?

i have add in cpp:

void AHismcTest::OnConstruction(const FTransform& transform) {
	
	hismc->ClearInstances();
	hismc->AddInstance(this->GetActorTransform());
}

and in public in .h

virtual void OnConstruction(const FTransform& transform) override;

but no show on editor or in play

Actually I haven’t try but you might have to use RegisterAllComponents() instead of RegisterComponent()

i add RegisterAllComponents() on onconstruction = Crash

does it work with 4.20 in c++ ?

in my blueprint I see now the objects created by one construct but when I put the blueprint in the window of the editor to add it to the world it crash

Assertion failed: (void*)((&ElementData[InstanceIndex]) + 1) <= (void*)(InstanceOriginDataPtr + CurrentSize) [File:D:\Build++UE4\Sync\Engine\Source\Runtime\Engine\Public\StaticMeshResources.h] [Line: 1231]

UE4Editor_Core!FDebug::AssertFailed() [d:\build\++ue4\sync\engine\source\runtime\core\private\misc\assertionmacros.cpp:425]
UE4Editor_Engine!FStaticMeshInstanceBuffer::UpdateFromCommandBuffer_RenderThread() [d:\build\++ue4\sync\engine\source\runtime\engine\private\instancedstaticmesh.cpp:189]
UE4Editor_Engine!TGraphTask<TEnqueueUniqueRenderCommandType<`FStaticMeshInstanceBuffer::UpdateFromCommandBuffer_Concurrent'::`2'::InstanceBuffer_UpdateFromPreallocatedDataName,<lambda_51d1f312d94e4dda751c0a57412a06da> > >::ExecuteTask() [d:\build\++ue4\sync\engine\source\runtime\core\public\async\taskgraphinterfaces.h:829]
UE4Editor_Core!FNamedTaskThread::ProcessTasksNamedThread() [d:\build\++ue4\sync\engine\source\runtime\core\private\async\taskgraph.cpp:665]
UE4Editor_Core!FNamedTaskThread::ProcessTasksUntilQuit() [d:\build\++ue4\sync\engine\source\runtime\core\private\async\taskgraph.cpp:574]
UE4Editor_RenderCore!RenderingThreadMain() [d:\build\++ue4\sync\engine\source\runtime\rendercore\private\renderingthread.cpp:333]
UE4Editor_RenderCore!FRenderingThread::Run() [d:\build\++ue4\sync\engine\source\runtime\rendercore\private\renderingthread.cpp:467]
UE4Editor_Core!FRunnableThreadWin::Run() [d:\build\++ue4\sync\engine\source\runtime\core\private\windows\windowsrunnablethread.cpp:76]

im here :
cpp

#include "test2.h"
#include "Runtime/CoreUObject/Public/UObject/ConstructorHelpers.h"
#include "Runtime/Core/Public/Math/UnrealMathUtility.h"



// Sets default values
Atest2::Atest2()
{
 	// Set this actor to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
	PrimaryActorTick.bCanEverTick = true;
	//Mesh = CreateDefaultSubobject<UStaticMesh>(TEXT("Mesh"));

	static ConstructorHelpers::FObjectFinder<UStaticMesh>cube_shape(TEXT("/Game/Test/EditorCube"));
	hismc = CreateDefaultSubobject<UHierarchicalInstancedStaticMeshComponent>(TEXT("hismc001"));
	//hismc->SetupAttachment(GetRootComponent());
	RootComponent = hismc;
	
	hismc->SetStaticMesh(cube_shape.Object);
	
	//hismc->RegisterComponent();
	//hismc->RegisterComponent();
	//hismc->SetHiddenInGame(false);
}

// Called when the game starts or when spawned
void Atest2::BeginPlay()
{
	Super::BeginPlay();
	
}
void Atest2::OnConstruction(const FTransform& transform) {
	//hismc->RegisterComponent();
	//RegisterAllComponents();
	hismc->ClearInstances();
	int32 g = 0;
	int32 h = 0;
	int32 i = 0;
	for (size_t x = 0; x < 2; ++x) {
		for (size_t y = 0; y < 2; ++y) {
			for (size_t z = 0; z < 2; ++z) {

				 g= FMath::RandRange( 0,  50);
				 h = FMath::RandRange(0, 50);
				 i = FMath::RandRange(0, 50);
				
				hismc->AddInstance(FTransform(FVector(x * g, z * h, y * i)));
				
				}

			}
		}
	hismc->RegisterComponent();
	//uint32 m = hismc->GetInstanceCount();
	//FString s = FString::FromInt(m);
	//UE_LOG(LogTemp, Warning, TEXT("%s"), *s);
}
void Atest2::test() {



}
// Called every frame
void Atest2::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);

}

.h

#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "Runtime/Engine/Classes/Components/HierarchicalInstancedStaticMeshComponent.h"
#include "Runtime/Engine/Classes/Engine/StaticMesh.h"
#include "test2.generated.h"

UCLASS()
class HIERACHICALIMC420CPP_API Atest2 : public AActor
{
	GENERATED_BODY()
	
public:	
	// Sets default values for this actor's properties
	Atest2();
	virtual void OnConstruction(const FTransform& transform) override;
protected:
	// Called when the game starts or when spawned
	virtual void BeginPlay() override;
	
public:	
	// Called every frame
	virtual void Tick(float DeltaTime) override;
	UHierarchicalInstancedStaticMeshComponent* hismc;
	
	UFUNCTION(BlueprintCallable, Category = "SunShine")
		void test();
	
	
};

if i drop my blueprint into the world the crash editor?
who would there be if I miss something?

OK it works …
I do not know this is bizarre but it’s normal that the c ++ classes that I create public … are public for the .h and private for the .cpp?
by putting the 2 class in public …
oddly, it does not crash anymore …

i post the class code for my future self
cpp:

// Fill out your copyright notice in the Description page of Project Settings.
#include "test2.h"
#include "Runtime/CoreUObject/Public/UObject/ConstructorHelpers.h"
#include "Runtime/Core/Public/Math/UnrealMathUtility.h"



// Sets default values
Atest2::Atest2()
{
 	// Set this actor to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
	PrimaryActorTick.bCanEverTick = true;
	//Mesh = CreateDefaultSubobject<UStaticMesh>(TEXT("Mesh"));

	static ConstructorHelpers::FObjectFinder<UStaticMesh>cube_shape(TEXT("/Game/Test/EditorCube"));
	mesh = cube_shape.Object;
	hismc = CreateDefaultSubobject<UHierarchicalInstancedStaticMeshComponent>(TEXT("hismc001"));
	//hismc->SetupAttachment(GetRootComponent());
	RootComponent = hismc;
	
	
	//hismc->RegisterComponent();
	//hismc->SetHiddenInGame(false);
}

// Called when the game starts or when spawned
void Atest2::BeginPlay()
{
	Super::BeginPlay();
	
}
void Atest2::OnConstruction(const FTransform& transform) {
	//hismc->RegisterComponent();
	//RegisterAllComponents();

	hismc->SetStaticMesh(mesh);
	//RegisterComponentWithWorld(this->getworld());
	hismc->RegisterComponentWithWorld(this->GetWorld());

	hismc->ClearInstances();
	int32 g = 0;
	int32 h = 0;
	int32 i = 0;
	for (size_t x = 0; x < 2; ++x) {
		for (size_t y = 0; y < 2; ++y) {
			for (size_t z = 0; z < 2; ++z) {

				 g= FMath::RandRange( 0,  50);
				 h = FMath::RandRange(0, 50);
				 i = FMath::RandRange(0, 50);
				
				hismc->AddInstance(FTransform(FVector(x * g, z * h, y * i)));
				
				}

			}
		}
	//hismc->RegisterComponent();
	//uint32 m = hismc->GetInstanceCount();
	//FString s = FString::FromInt(m);
	//UE_LOG(LogTemp, Warning, TEXT("%s"), *s);
}
void Atest2::test() {



}
// Called every frame
void Atest2::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);

}




// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "Runtime/Engine/Classes/Components/HierarchicalInstancedStaticMeshComponent.h"
#include "Runtime/Engine/Classes/Engine/StaticMesh.h"
#include "test2.generated.h"

UCLASS()
class HIERACHICALIMC420CPP_API Atest2 : public AActor
{
	GENERATED_BODY()
	
public:	
	// Sets default values for this actor's properties
	Atest2();
	virtual void OnConstruction(const FTransform& transform) override;
protected:
	// Called when the game starts or when spawned
	virtual void BeginPlay() override;
	
public:	
	// Called every frame
	virtual void Tick(float DeltaTime) override;
	UHierarchicalInstancedStaticMeshComponent* hismc;
	UStaticMesh* mesh;
	UFUNCTION(BlueprintCallable, Category = "SunShine")
		void test();
	
	
};