Custom actor component detecting collision

Hi, thanks for reading!

Can you think of a way to have a custom UActorComponent which responds to collisions/overlaps detected by the parent actor, without touching the parent actor’s C++ code?

I want to be able to make a UActorComponent which is independent from the parent so that it can be attached to many kinds of actors. And for the UActorComponent to respond to overlap events of the parent actor.

At the moment I have experimented with coding an actor class which in its constructor makes a spherical component to detect collisions. If I can assume that the parent will have some collision mesh already, is there some way that a child actor component can respond to the same hit/overlap events?

-edit- A second idea, which I think might be better:
Instead, when attaching my UActorComponent to an actor, it creates a simple collision object, like a USphereComponent which can then do the collision detection. I’ve just tried this out by mostly copying the code I had from my actor class (described below) but can’t get the USphereComponent to attach to the actor component. I’m guessing that UActorComponents dont have a RootComponent in the same way that actors do.

Actor Constructor .cpp

USphereComponent* SphereComponent = CreateDefaultSubobject<USphereComponent>(TEXT("RootComponent"));
RootComponent = SphereComponent;
SphereComponent->InitSphereRadius(40.0f);
SphereComponent->SetCollisionProfileName(TEXT("Actor"));

SphereComponent->OnComponentBeginOverlap.AddDynamic(this, &AVolumeCollider::OnOverlapBegin);  

UActorComponent Constructor .cpp

USphereComponent* SphereComponent = CreateDefaultSubobject<USphereComponent>(TEXT("ActorComponentCollider"));
RootComponent = SphereComponent;
SphereComponent->InitSphereRadius(40.0f);
SphereComponent->SetCollisionProfileName(TEXT("Actor"));

SphereComponent->OnComponentBeginOverlap.AddDynamic(this, &UTestComponent::OnOverlapBegin);

Hey Jayae-

If you’re creating your actor component in code, you should be able to add it to the actor as you would another component (using CreateDefaultSubobject) and write a function in your actor class that is called when the overlap triggers. Inside this function you can call any functionality of the Actor component you want to run when the overlap triggers from the ActorComponent reference inside the actor class.

Cheers

Hi .

Thanks for responding! I’m trying though to not touch the actor’s C++ code as I want my actor component code to be independent and usable on actors which potentially just use blueprint.

Ideally, is there some way that when attaching my actor component, it can register its OnOverlapBegin/other collision functions with the parent actor?

This should still work for you. If you mark the ActorComponent functionality as a UFUNCTION with BlueprintCallable, you can add the the component to a blueprint and use the default Event ActorBeginOverlap node to call the function from the component reference.

Working great! Thanks, !

Hello!
I know it’s kinda late, but can we do it somehow in just C++ (not editing the actor’s code) and not via Blueprints?
THanks!