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"

How do i use GetComponentsByClass() in C++

Could someone please explain to me how to properly use this. I need to get all the static mesh components from an actor. I found the documentation but it really doesn't explain much to me.

https://docs.unrealengine.com/latest/INT/API/Runtime/Engine/GameFramework/AActor/GetComponentsByClass/index.html

This is the closest I've got without causing errors. The only issue is that The array is empty when it should be returning something.

 TArray<UStaticMeshComponent*> StaticMeshComps;
 TArray<UStaticMeshComponent*>GetComponentsByClass(StaticMeshComps);
 
 for (int y = 0; y < StaticMeshComps.Num(); y++)
 {
     UE_LOG(LogTemp, Warning, TEXT("Static Mesh: %s"), *StaticMeshComps[y]->GetName());
 }
Product Version: UE 4.12
Tags:
more ▼

asked Feb 16 '17 at 09:45 AM in C++ Programming

avatar image

Djekko
14 8 10 10

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

1 answer: sort voted first
 // Disable all particle components so that they don't auto fire as soon as the actor is spawned. The particles should be triggered through the particle track.
 TArray<UActorComponent*> ParticleComponents = ObjectTemplate->GetComponentsByClass(UParticleSystemComponent::StaticClass());
 for (int32 ComponentIdx = 0; ComponentIdx < ParticleComponents.Num(); ++ComponentIdx)
 {
     ParticleComponents[ComponentIdx]->bAutoActivate = false;
 }

I got this from LevelSequenceSpawnRegister.cpp using grepWin, should help you out I hope :)

more ▼

answered Feb 16 '17 at 10:06 AM

avatar image

MonsOlympus
973 37 11 37

avatar image Djekko Feb 16 '17 at 10:37 AM

This method did work somewhat the array is no longer empty but the array is of type UActorComponent and not UStaticMeshComponent. This causes an issue because I need to be able to get the location and change the velocity of the components. As the class type is UActorComponent I can't do anything with it that I need. Is there a way around this or to change the array type to UStaticMeshComponent? This is my code now.

 TArray<UActorComponent*> StaticMeshComps;
 StaticMeshComps = actorHit->GetComponentsByClass(UStaticMeshComponent::StaticClass());
 
 for (int y = 0; y < StaticMeshComps.Num(); y++)
 {
     UE_LOG(LogTemp, Warning, TEXT("Static Mesh: %s"), *StaticMeshComps[y]->GetName());
 
     FVector MeshLocation = StaticMeshComps[y]->GetComponentLocation();
 }

avatar image MonsOlympus Feb 16 '17 at 01:34 PM

You probably need to Cast since GetComponentsByClass must by default return UActorComponents.

Just a note though that Im not sure UStaticMeshComponents have their own velocity only when you add a ProjectileComponents and associate those with Mesh Components will they probably have a Velocity (havent looked into it in detail).

  TArray<UStaticMeshComponent*> StaticMeshComps;
  StaticMeshComps = Cast<UStaticMeshComponent>(actorHit->GetComponentsByClass(UStaticMeshComponent::StaticClass()));

As a guess, my C++ is rusty because i avoid using it :)

avatar image Djekko Feb 16 '17 at 03:51 PM

I see where you are getting at with it but it doesn't work unfortunately I have opened another question about it as this is now slightly off topic but hopefully i can sort it soon. :)

avatar image Burnrate Dec 15 '17 at 01:44 AM

Where is the other question?

(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