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