Folks,
I’m trying to place an InstancedStaticMeshComponent via C++. I’ve attached the component to the DefaultSceneRoot, but when I AddInstance for the ISMComponent, it appears relative to the World origin, as opposed to relative to the DefaultSceneRoot location.
This is the code I’m using:
.h
UPROPERTY(EditAnywhere)
USceneComponent* m_DefaultRoot = nullptr;// InstancedStaticMeshComponent for the decoration.
UPROPERTY(VisibleAnywhere, BlueprintReadOnly)
UInstancedStaticMeshComponent* m_DecorationInstancedStaticMeshComp;
.cpp in Constructor
m_DefaultRoot = CreateDefaultSubobject(TEXT(“DefaultRoot”));
SetRootComponent(m_DefaultRoot);FAttachmentTransformRules attach_rules =
FAttachmentTransformRules(EAttachmentRule::KeepRelative,
EAttachmentRule::KeepRelative,
EAttachmentRule::KeepRelative, true);m_DecorationInstancedStaticMeshComp =
CreateDefaultSubobject< UInstancedStaticMeshComponent >(TEXT(“Decoration Mesh”));
m_DecorationInstancedStaticMeshComp->RegisterComponent();
m_DecorationInstancedStaticMeshComp->SetMobility(EComponentMobility::Static);
m_DecorationInstancedStaticMeshComp->AttachToComponent( m_DefaultRoot, attach_rules );
.cpp in BeginPlay()
UStaticMesh* mesh = MyLoadDecorationMeshFunction( … );
m_DecorationInstancedStaticMeshComp->SetStaticMesh( mesh );
m_DecorationInstancedStaticMeshComp->SetFlags( RF_Transactional );
AddInstanceComponent( m_DecorationInstancedStaticMeshComp );FTransform xform;
xform.SetTranslation( FVector( 0, 0, m_Size.Z * 100 ) );
m_DecorationInstancedStaticMeshComp->AddInstance( xform, false );
Does anyone see anything wrong?