Get Component in C++

When you create component, function that create it usually return pointer to component, you set it to variable in you class and then you can reuse it. All existing classes that have components set it usually store them in variables. , so for those check API refrence.

if not you can still get any component from this:

keep in mind this is expensive function use it only once to get the pointer

In Blueprint, you can drag and drop a component directly from the components list into the window to “get” that component for use. I am trying to do the same thing in C++ but I am at a loss for how I would do that.

Blueprint example:

How would I store that same actor into a variable?


So I’ll have to create the component inside of the C++ class so that I can store it in a variable?

Usually you will add it as a property. In header:

UPROPERTY(VisibleDefaultsOnly, BlueprintReadWrite)
	USceneComponent* DefaultSceneRoot;

And create it in class constructor:

// Default root
DefaultSceneRoot = CreateDefaultSubobject<USceneComponent>(TEXT("DefaultSceneRoot"));
RootComponent = DefaultSceneRoot;
DefaultSceneRoot->Mobility = EComponentMobility::Static;
DefaultSceneRoot->bVisualizeComponent = true;