[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!

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.

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.

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

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

Hey -

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 (AActor | Unreal Engine Documentation ).

Cheers

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.

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…

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.

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.

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.

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.

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.

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.

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

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.

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>();