Trouble adding component at 0,0,0 relative to parent

I have this code here:



int32 hismCount = TileMeshInstances.Num();
FString hismName = FString::Printf(TEXT("TileMeshInstance_%d"), hismCount);
UHierarchicalInstancedStaticMeshComponent* hism = NewObject<UHierarchicalInstancedStaticMeshComponent>(this, UHierarchicalInstancedStaticMeshComponent::StaticClass(), *hismName);
hism->RegisterComponent();
hism->AttachTo(this->GetRootComponent());
hism->SetRelativeLocation(FVector(0, 0, 0));
//hism->SetWorldLocation(this->GetActorLocation());
hism->SetStaticMesh(tileMesh);
UE_LOG(LogTemp, Display, TEXT("Using transform for instance: %s"), *tile->Transform.ToString());
hism->AddInstance(tile->Transform);
TileMeshInstances.Add(tileMesh, hism);


I want the created Hierarchical Instanced Static Mesh component (HISM) to be at 0,0,0 relative to the parent class, but not matter what I do it always ends up at 0,0,0 in World space.

Hmm I wonder if the problem is actually with the instances I am adding to the HISM. Is HISM->AddInstance in local or world space? It doesn’t say and I am assuming it is in local space, but maybe not.

EDIT: Nevermind I just looked it up in the docs and it is indeed local space. So my problem still remains.

Anyone have any ideas? This is driving me crazy and it should be super simple. I’m not sure what I am doing wrong.

Have you tried:



hism->AttachTo(this->GetRootComponent(), NAME_None, EAttachLocation::SnapToTarget);


Attach to defaults to KeepRelativeOffset. The above is what I use to set the position of an object to match another exactly. (You could also use SnapToTargetIncludingScale if you want to match the scale too)

Well that sounded most logical, however no luck :frowning: It still isn’t showing up in the right place.

EDIT: Ok this is odd. Apparently the actor doesn’t actually know where it is when I am setting up. I am doing this in a function called during OnConstruction(). The strange thing is that the call to OnConstruction passes in a transform and that transform has the actor’s correct position. But apparently the actor itself doesn’t know that yet, because if you ask the actor where it is, it thinks it is at 0,0,0.