How to use C++ Components/Actor Components with blueprints?

Is it possible to attach a C++ Component/Actor Component to a blueprint, so that anytime the blueprint object overlaps with another blueprint object, it runs some code?

Like say I have a cube blueprint, I want it so that anytime an a cube hits another cube, it runs the C++ component code. How can I do this? I’m having a lot of issues with finding details on this because it seems most tutorials either just use all blueprints or all C++.

In this video, I tried to follow the tutorial, except I’m putting the C++ code into an Actor Component that’s attached to my blueprint object. But I’m having an issue at 3:33, where in the BeginPlay function they add the OnComponentBeginOverlap to “BoxComp”. What would I put in place of “BoxComp” in a scenario where I am using trying use an Actor Component to run some code whenever the blueprint hits something?

Apologies if this question doesn’t make much sense, I don’t have a good grasp on how blueprints, Actors, Components and stuff interact in Unreal.

You can get other components from a component with GetOwner()->FindComponentByClass().

Assuming the collider will always be a box:

void UCppComponent::BeginPlay()
{
	Super::BeginPlay();
	UBoxComponent *Box = GetOwner()->FindComponentByClass<UBoxComponent>();
	if (Box) {
		Box->OnComponentBeginOverlap.AddDynamic(this, &UCppComponent::Overlapping);
	} else {
		UE_LOG(LogFooBar, Warning, TEXT("%hs() => Could not find box collision component on parent!"), __func__);
	}
}

You need to know what is on the BP of interest of course, if you wanted to support other collision shapes for example, you would need multiple FindComponentByClass() calls (or possibly there is a base class you can use to “catch-all”, I’m not super familiar with the collision shape class zoo).

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.