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 to use GetComponentByClass / FindComponentByClass?

I'm trying to get a reference of a component (UCameraComponent) from an actor but everytime I use GetComponentByClass or FindComponentByClass and I compile the solution, ue4.5.1 crashes.

I tried this and UE4 didn't crash:

 this->GetComponentByClass(UCameraComponent::StaticClass());

but then I tried this and UE4 did crash again:

(.h)

 UPROPERTY(EditAnywhere, BlueprintReadWrite, Category=Components)
 AActor* cameraActor;

I want to set the reference to the cameraActor with the inspector.

(.cpp)

 cameraActor->GetComponentByClass(UCameraComponent::StaticClass());
Product Version: Not Selected
Tags:
more ▼

asked Oct 31 '14 at 11:51 AM in C++ Programming

avatar image

Reisn7CC
20 4 5 9

avatar image ogrewizardninja Oct 31 '14 at 12:04 PM

Have you initialised cameraActor? You can't use a pointer unless it is pointing at something valid or it will crash.

avatar image Reisn7CC Oct 31 '14 at 12:17 PM

Ah thanks, I did: cameraActor = nullptr; but I guess it's the same as not intitialising it. What would you suggest if I want to change it in the inspector anyway?

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

1 answer: sort voted first

Solution

Code For You

Here's the code I use to get actor components of a specific class:

 TArray<UStaticMeshComponent*> Comps;
 
 GetComponents(Comps);
 if(Comps.Num() > 0)
 {
     UStaticMeshComponent* FoundComp = Comps[0];
        //do stuff with FoundComp
 }

You can easily adapt this code to your own needs

:)

Rama

more ▼

answered Nov 01 '14 at 02:53 AM

avatar image

Rama
10.7k 446 346 1090

(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