x

Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

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

Product Version: UE 4.10
Tags:
more ▼

asked Feb 13 '16 at 10:22 AM in C++ Programming

avatar image

Saso222
28 2 4 6

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

1 answer: sort voted first

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?

more ▼

answered Feb 15 '16 at 02:53 PM

avatar image Saso222 Feb 15 '16 at 03:50 PM

Hello, Matthew, 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!

avatar image Matthew J Feb 15 '16 at 04:05 PM

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.

avatar image Saso222 Feb 15 '16 at 04:15 PM

That's nice to know. Many many thanks! :)

(comments are locked)
10|2000 characters needed characters left
Viewable by all users
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question