GetComponentBounds returns 0 in constructor

Hello everyone,

I’m programming a unit in C++ from which every unit of my game will extend. This is only c++ and it has no BP component.

It has a Mesh and a Decal Component that can be adjusted via editor using UPROPERTIES, but right now I’m trying to attach it a default mesh and a default decal component material. The DecalComponent is actually a child of the MeshComponent.

So, in the constructor, I get the MeshComponent and attach to it a default mesh, referencing it via its path. Then, I get the Decal Component and attach to it a default material. The DecalComponent’s rotation, position and scale depend on those of the MeshComponent, so I set RelativePositionAndOrientation and, talking about the scale, I try to get the SphereRadius of the Mesh using the GetComponentBounds function, but it simply returns 0 for everything. Code of this:

/*
 * Default mesh and Decal materials
 */
static ConstructorHelpers::FObjectFinder<USkeletalMesh> DefaultMesh(TEXT("/Game/Units/Character/TopDownSkeletalMesh"));
GetMesh()->SetSkeletalMesh(DefaultMesh.Object);

static ConstructorHelpers::FObjectFinder<UMaterial> DefaultDecalMat(TEXT("/Game/Materials/General/M_SelectionDecal"));
DecalComponent->SetMaterial(0, DefaultDecalMat.Object);

//Set decal properties after setting the mesh because it's relative (It doesn't work, since GetComponentBounds is not getting the Mesh measurements as it can be seen in the logging. I don't know why.)
DecalComponent->SetRelativeLocationAndRotation(FVector(0.0f, 0.0f, 0.0f), FRotator(90.0f, 0.0f, 0.0f));

float SphereRadius;
FVector Origin, BoxExtents;
UKismetSystemLibrary::GetComponentBounds(GetMesh(), Origin, BoxExtents, SphereRadius);

UE_LOG(LogTemp, Warning, TEXT("SphereRadius: %f"), SphereRadius);

SphereRadius -= 200;
DecalComponent->SetRelativeScale3D(FVector(SphereRadius, SphereRadius, SphereRadius));

I think it has to be something about when the mesh is loaded, since I have bound an event that fires when the DecalMaterial is changed and then do the same, get the sphere radius and scale the material with it, and it works that way. But that’s not the default behavior I want it to have.

Any ideas on how to get the sphere radius of a mesh in the constructor or how to know WHEN is loaded in order to be able to do it?

Thanks a lot in advance

Hello Saso222,

OnConstruction may be a better place to try to do this, but what exactly are you calling GetMesh() on? Are you sure you’re getting the mesh that you’re looking for?

Hello, ,
I’m sure that I’m getting the mesh I want, I’m calling GetMesh of the class, which is child of ACharacter, that was not the problem.

I found really useful the OnConstruction function you talked about, whose existence I wasn’t aware of and made this exact example work.

But making a quick research of this function, I found something that can also be interesting, it’s called virtual void PostInitializeComponents(), do you reckon that in this case (initializing default values) this function will be a better place to do this?

Thanks!

PostInitializeComponents is called at runtime, so it would be called around the same time as BeginPlay (Shortly before it, if I remember correctly.) You can find more information about these types of functions at this forum post.

That’s nice to know. Many many thanks! :slight_smile: