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.
// 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;
};
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
#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();
};