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"

Add a delegate to OnComponentBeginOverlap in C++

Hi guys, I am trying to attach a delegate to the OnComponentBeginOverlap event.

Here is the header:

 UCLASS()
 class ASlidingDoorTrigger : public AActor
 {
     GENERATED_UCLASS_BODY()
 
 public:
     UPROPERTY(VisibleAnywhere, Category = "Triggers")
     TSubobjectPtr<UBoxComponent> TriggerBox;
 
     UPROPERTY(EditAnywhere, Category = "Static Meshes")
     AStaticMeshActor* SlidingDoor;
 
 private:
     void OnBeginOverlap();
     void OnEndOverlap();
 };

And the source file:

 ASlidingDoorTrigger::ASlidingDoorTrigger(const class FPostConstructInitializeProperties& PCIP)
     : Super(PCIP)
 {
     this->TriggerBox = PCIP.CreateDefaultSubobject<UBoxComponent>(this, TEXT("BoxTrigger_SlidingDoor"));
     this->TriggerBox->OnComponentBeginOverlap.Add(&ASlidingDoorTrigger::OnBeginOverlap);
     this->TriggerBox->OnComponentEndOverlap.Add(&ASlidingDoorTrigger::OnBeginOverlap);
     this->RootComponent = this->TriggerBox;
 }
 
 void ASlidingDoorTrigger::OnBeginOverlap()
 {
     if (GEngine)
     {
         GEngine->AddOnScreenDebugMessage(0, 10.0f, FColor::White, TEXT("Begin Overlap"));
     }
 }
 
 void ASlidingDoorTrigger::OnEndOverlap()
 {
     if (GEngine)
     {
         GEngine->AddOnScreenDebugMessage(0, 10.0f, FColor::White, TEXT("End Overlap"));
     }
 }

The problem is that I need to pass a TSciptDelagate to the Add function, but I don't know it's signature. Also I want to pass a delegate with the "void Function()" signature. Can you help me out?

Product Version: Not Selected
Tags:
more ▼

asked Jun 25 '14 at 05:47 AM in C++ Programming

avatar image

dbrizov
334 21 22 43

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

2 answers: sort voted first

For those reading this with later engine versions (4.7+) here is the now correct method:

Function definitions should look like this:

     UFUNCTION()
         void OnXXXOverlapBegin(class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult & SweepResult);
 
     UFUNCTION()
         void OnXXXOverlapEnd(class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex);

Then in the constructor or wherever you desire you set it up with this:

     XXXComponent = ObjectInitializer.CreateDefaultSubobject<USphereComponent>(this, TEXT("XXX"));
     XXXComponent->SetSphereRadius(2000.0f);
     XXXComponent->AttachParent = RootComponent;
     XXXComponent->SetCollisionResponseToAllChannels(ECollisionResponse::ECR_Ignore);
     XXXComponent->SetCollisionResponseToChannel(ECC_XXX, ECollisionResponse::ECR_Overlap);

     XXXComponent->OnComponentBeginOverlap.AddDynamic(this, &AXXXCharacter::OnXXXOverlapBegin);
     XXXComponent->OnComponentEndOverlap.AddDynamic(this, &AXXXCharacter::OnXXXOverlapEnd);



more ▼

answered May 24 '15 at 04:16 AM

avatar image

Kory
383 8 133 45

avatar image PersnicketyGareth May 31 '15 at 05:35 PM

Beautiful. Thanks.

By the way, is this kind of stuff documented, or are we just expected to figure it out ourselves?

avatar image Kory May 31 '15 at 05:38 PM

I figured it out, not sure if it is documented or not, but it probably is. It is just these things change with every version. At least since I figured it out, no one else has to for this version.

avatar image 5Kwatts Jun 04 '15 at 02:15 AM

For better documentation, even though it is 4.6, see this URL. It still has the same setup.

https://docs.unrealengine.com/latest/INT/Gameplay/ClassCreation/CodeOnly/index.html

avatar image jonimake Sep 09 '16 at 10:07 PM

In 4.13 there seems to be an additional param in front of the param list:

 void OnOverlapBegin(class UPrimitiveComponent* OverlappedComp,....
(comments are locked)
10|2000 characters needed characters left
Viewable by all users

First of, you have to declare your OnBeginOverlap and OnEndOverlap functions as follow:

 UFUNCTION()
 void OnBeginOverllap(AActor* Other);
 UFUNCTION()
 void OnEndOverlap(AActor* Other);

Delegates must be UFUNCTIONs, and must match delegate signature as well, so you can't pass void Function() into this delegate.

Also, since it's dynamic multicast you have to add it like this:

 OnComponentBeginOverlap.AddDynamic(this, &ASlidingDoorTrigger::OnBeginOverlap);

First parameter is the pointer to the object that will execute the function passed as second parameter.

P.S. You don't need to use this in the constructor in this case, it's just excess.

more ▼

answered Jun 25 '14 at 09:28 AM

avatar image

BiggestSmile
1.6k 54 38 105

avatar image dbrizov Jun 25 '14 at 09:51 AM

Thanks mate. I am at work now. Will try it out when I get back home :)

avatar image dbrizov Jun 25 '14 at 10:40 AM

BTW mate, where I can see the signatures of all delegates? I am currently watching the documentation for the TScriptDelegate and there nothing that tells me what the signature is?

avatar image BiggestSmile Jun 25 '14 at 02:03 PM

That is actually base-class for delegates.

You can find signatures list and where they are declared in the docs, for example - https://docs.unrealengine.com/latest/INT/API/Runtime/Engine/Components/index.html, lists all the delegates used in components. Check marshaling category to find out delegate parameters.

There should be something like Go to definition in Visual Studio, just highlight type, right click and look for something like find declaration/go to definition. I use Visual Assist X add-on for Visual Studio, there is such feature, but i'm not sure about the stock VS; though it should have it too.

P.S. Inside your (Begin/End)Overlap function you check for GEngine, i believe that if delegate happened to trigger, there is no way GEngine could be null, something really weird must happen, but if that happened, engine will crash much earlier before your delegate :)

avatar image dbrizov Jun 25 '14 at 02:20 PM

Thanks for the info mate, you really helped. I use Visual Assist X too, it's a very nice add-on, especially for C++

avatar image dbrizov Jun 25 '14 at 04:38 PM

It worked mate. The signature of the function is actually this:

 UFUNCTION()
 void OnOverlap(class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex);

Thank you very much.

avatar image BiggestSmile Jun 25 '14 at 05:14 PM

Oh, indeed. I've been referring to OnActorBeginOverlap delegate apparently :)

You're welcome, feel free to ask more :)

(comments are locked)
10|2000 characters needed characters left
Viewable by all users
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