Problem when obtaining the location of my instances C++

I’m creating 2 Instance Static Mesh Component, the first one called “box” and I created 3 instances in different locations. Now what I want is to create an instance of my second Instance Static Mesh Component called “test” and place it in the same location as any of the 3 instances of my first Instance Static Mesh Component called “box”. I’m using a property called “GetInstanceTransform” which brings me the location. but when stablesco for example 1 or 2 in the “instanceindex” returns me another location, since the size of “box” and “test” is not equal, but if I set the “instanceindex” to 0 if I set the location of my instance “test” correctly.

my code:

AMyActor::AMyActor()
{
// 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;

dommyroot = CreateDefaultSubobject<USceneComponent>(TEXT("dommyroot"));
RootComponent = dommyroot;

struct ::ConstructorHelpers::FObjectFinder<UStaticMesh> MyCube(TEXT("/Game/StarterContent/Shapes/Shape_Cube.Shape_Cube"));

box = CreateDefaultSubobject<UInstancedStaticMeshComponent>(TEXT("box"));
box->SetStaticMesh(MyCube.Object);
box->SetupAttachment(dommyroot);
box->SetRelativeScale3D(FVector(1, 1, 1));
box->SetRelativeLocation(FVector(0, 0, 0));

struct ::ConstructorHelpers::FObjectFinder<UStaticMesh> MySphere(TEXT("/Game/StarterContent/Shapes/Shape_Sphere.Shape_Sphere"));

test = CreateDefaultSubobject<UInstancedStaticMeshComponent>(TEXT("test"));
test->SetStaticMesh(MySphere.Object);
test->SetupAttachment(dommyroot);
test->SetRelativeScale3D(FVector(0.5, 0.5, 0.5));
test->SetRelativeLocation(FVector(0, 0, 100));

}

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

const FVector PizoLocation = CajaPincel->GetActorLocation();
dommyroot->SetWorldLocation(FVector(PizoLocation.X - 500, PizoLocation.Y - 500, PizoLocation.Z + 300));

for (size_t X = 0; X < 3; X++)
{
	box->AddInstance(FTransform(FVector(X * 250, 0, 0)));
}

FTransform FTransformA; box->GetInstanceTransform(1, FTransformA, false);
const FVector FVectorA = FTransformA.GetLocation();

test->AddInstance(FTransform(FVector(FVectorA.X, FVectorA.Y, FVectorA.Z)));

}

now the following

for example: if I set the “instanceindex” to 0

FTransform FTransformA; box->GetInstanceTransform(0, FTransformA, false);
const FVector FVectorA = FTransformA.GetLocation();

test->AddInstance(FTransform(FVector(FVectorA.X, FVectorA.Y, FVectorA.Z)));

268075-captura.png

now if for example I set the “instanceindex” to 1

if I reset the “SetRelativeScale3D” with the same values it does it correctly, but when changing “SetRelativeScale3D” of “test” that happens. Any idea how to fix it? will it be that I am not establishing the correct function to do it?

PD: sorry but I do not speak English

The problem occurs when the 2 “InstanceStaticMeshComponent” have different values in “SetRelativeScale3D” it always correctly returns the location of the first instance, but not the remaining one. Now if both have the same “SetRelativeScale3D” it works correctly.

You have to change the scale of the instances, not the scale of the InstancedStaticMeshComponent.

If you change the scale of the component, the locations are being scaled as well. So instead of

 test->AddInstance(FTransform(FVector(FVectorA.X, FVectorA.Y, FVectorA.Z)));

do:

 test->AddInstance(FTransform(FRotator(0.0f, 0.0f, 0.0f), FVectorA, FVector(0.5f, 0.5f, 0.5f)));

And remove the scale for the component.

Thank you very much, if I am grateful.

By default you can not create an object of an abstract base class because the ABC acts like a blueprint on which you create your actual implementation shareit.