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"

AnswerHub Maintenance

Background maintenance is scheduled to occur between 9 - 11am EDT on Tuesday, May 21. Site operation may be slower than normal during this time and a brief interruption in operation may be observed

[C++] How to get/pass reference to bluprint's component

How to get a reference/pointer to some bluprint's added component? I already now that when I put this:

 UPROPERTY(EditDefaultsOnly, Category = "AI")
 class UComponent* NewComp;

And then in class constructor put this:

 NewComp = CreateDefaultSubobject<UComponent>(TEXT("NewComp"));

The new component is being created and it can't even be deleted from the blueprint editor - which is somehow nice. But this is an easy one. The question is, how to pass already made component in the editor to the c++ code. Especially not simple one.

Let's say, I've nested another blueprint in that blueprint, and I want to have and access to it in my code. So I need to grab it's reference somehow and pass it to c++ script.

Dealing with it in blueprint script is super easy - drag'n'drop and you can do whatever you want on your nested Actor. How to do it in code?

Thanks in advance!

Product Version: UE 4.13
Tags:
more ▼

asked Oct 28 '16 at 10:52 AM in C++ Programming

avatar image

Krzysiek_TATE
219 4 13 18

(comments are locked)
10|2000 characters needed characters left

3 answers: sort voted first

You can use a handy utility:

 static ConstructorHelpers::FObjectFinder<TypeOfComponent> TheObjectName(TEXT("<Insert Path here>"));
 
 if ( TheObjectName.Succeeded())
 {
   NewComp = TheObjectName.Object;
 }

To get the Path: Right click on a blueprint, and select Copy reference.

more ▼

answered Oct 28 '16 at 12:00 PM

avatar image

Holybreath
378 10 7 19

avatar image Krzysiek_TATE Oct 28 '16 at 03:08 PM

Thanks for replay. But since it's a blueprint reference, I can't cast it to my original class which this blueprint was based on - my reference is Blueprint'/My/Very/Long/Path.Path'

So in the end, I've got my UObject loaded without any way to cast it.

And this is actualy not answering my problem. I was asking if it's possible to pass referance of an actor/component/other blueprint that is already attached to a new blueprint.

avatar image Holybreath Oct 28 '16 at 07:08 PM

you want to get the component that you added to blueprint from editor into c++?

avatar image Krzysiek_TATE Nov 02 '16 at 09:09 AM

Yes. That's exactly what I want. No matter what kind of Actor it is.

(comments are locked)
10|2000 characters needed characters left

Hey Krzysiek_TATE-

You can use the GetComponents() function to get an array of all of the components attached to the calling actor. You can then use a for loop to cast each component to the type you're looking for to find the specific component necessary. If your components are listed with tags, you can use the GetComponetsByTag() function to get the components from the given class that also use the given tag. For more information on how to get the actor's components, you can check the Actor class documentation (https://docs.unrealengine.com/latest/INT/API/Runtime/Engine/GameFramework/AActor/index.html ).

Cheers

Doug Wilson

more ▼

answered Nov 02 '16 at 02:23 PM

avatar image Krzysiek_TATE Nov 02 '16 at 02:37 PM

GetComponents() is not what I want. I want to pass reference precisely. Let's say that I've got a couple of subobjects ( or components ) of thesame type - actually exactly same objects, but to me each one is totaly different by it's purpose. Peeking them runtime by GetComponents() is a nonsense to me then, bacause I'll be never sure if I have the right ones. As I said, it can be done by drag and drop in blueprint script, so there must be a way to pass it to c++ code.

P.S. Why was this answer tagged as accepted - if I'm the one who should accept it or not, and I didn't. This is awkward...

avatar image Doug E ♦♦ STAFF Nov 02 '16 at 02:53 PM

Staff answers will on occasion be marked as the accepted answer upon posting, however adding a new comment will unaccept the answer. If you have two of the same components, then it would probably be best to add a tag to each of them. That way in you can search by Tag in code to find the specific component necessary.

avatar image Krzysiek_TATE Nov 02 '16 at 03:03 PM

So there is no option to perform something simillar to Unity's approach, so that I make an exposed variable ( Actor in this case ) and set it in the blueprint manually? There are at least two disadvantages that came to my mind of using GetComponents() : 1. This solution is not explicit, so the blueprint can be easly broken, and it'll be hard to find the problem. 2. GetComponents() has to be performed on the startup, so if there is more of this, it'll increase the initialization phase time.

avatar image Doug E ♦♦ STAFF Nov 02 '16 at 03:57 PM

Just to make sure I understand exactly what you're attempting to you, you want to add your component to your blueprint through the editor, and then get a reference to that component to use in code, correct? You can create a UPROPERTY pointer to the component type and then set that variable in your blueprint construction script or the BeginPlay event. For example, if you have a Static Mesh Component added through the editor, creating a UStaticMeshComponent* MeshCompRef that is declared as BlueprintReadWrite will allow you to use a Set MeshCompRef node and set it to your editor component. Then in code you can use MeshCompRef. If you do decide to do this, it would be best to check that MeshCompRef is not null before using it in code.

avatar image Krzysiek_TATE Nov 03 '16 at 09:54 AM

Yes. This is what I want to do - add component to the blueprint through the editor and get it's reference/pointer in the code. I actualy wrote this in my very first post. According to your solution, I also knew about that option, but it doesn't seem to be clean too. It looks hacky. It seems to me that there is some functionality missing on this. To me it's quite odd that you can set every exposed UPROPERTY manualy through the interface, but you can not set the Actor's value. It's strange, especially because the drop down menu actualy appears but after peeking anything the value isn't being set ( even with EditAnywhere set ). And the second thing is, that the subobject / component of this particular Actor does not appear on that list - while it shares thesame class.

avatar image MirrorNext Feb 12 '17 at 12:17 AM

I'm trying to do the exact same thing and no luck. =/

avatar image Doug E ♦♦ STAFF Nov 03 '16 at 02:19 PM

I just wanted to clarify that I was attempting to answer the correct question that you were asking. You're correct that creating a component variable in the class and then setting the variable through the blueprint is not the standard way to reference components in code. As I mentioned the normal way to get an actor's components is to use GetComponents(). If you have more than one of a component type (multiple static mesh components for example), giving each one a different tag will allow you to obtain a reference to the specific component using the GetComponentsByTag() call instead.

avatar image Krzysiek_TATE Nov 03 '16 at 04:38 PM

It's not a standard way? I can imagine dozens of examples that this approach would be much better, and sometimes the only one possible. My argument on this is that you don't use GetComponents() on blueprint script - you just pass the reference explicitly - so as it should be done on c++ version. To me it's a missing feature. And messing arround with blueprint script to make c++ code working? That's awful to me.

avatar image j_zeitler Jan 31 '17 at 01:30 PM

I have also tried to get around this using a couple of tricks but ultimately, being able to set/link references to components inline would be a very useful feature.

(comments are locked)
10|2000 characters needed characters left

I'm so glad I found this thread. I'm used to unity c# where you can simply get a component reference by typing one simple line of code and get the explicit reference to the exact component you set up in the inspector before that. I'm blown away that unreal doesn't have a similar feature. Having to get all components with tag just to find one of the components attached to the same actor just seems mediocre at best.

I guess I'll have to venture back into the Stone Age to access a simple component on my actor.

more ▼

answered Jul 11 '17 at 07:58 AM

avatar image

Trynn
56 2 5 7

avatar image Waverunner Jul 12 '17 at 03:09 AM

Inside any method you can use the below line. In your case BeginPlay is probably best so you can initialize it as a variable for the class. Obviously, you have to replace the class name with the class name of the added component:

 UAddedBPComponent* AddedComponent = GetOwner->FindComponentByClass <UAddedBPComponent>();
(comments are locked)
10|2000 characters needed characters left
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