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"

C++ Overlap events don't work

Hi, I have some problems with c++. I tried to copy overlap events tutourials from various sources, but none of them worked. I copied this video 1:1: https://www.youtube.com/watch?v=NAbLfvTJLrw. The problem is this line of code: _collision->OnComponentBeginOverlap.AddDynamic(this, &AMyActor3::OnDelegateExampleOverlap); and the "." between BeginOverlap and AddDynamic is red underlined. Did something change between the versions or why doesn't it work? Here is my whole code:

HeaderFile:

 #pragma once
 
 #include "CoreMinimal.h"
 #include "GameFramework/Actor.h"
 #include "MyActor3.generated.h"
 
 UCLASS()
 class YOUTUBE_API AMyActor3 : public AActor
 {
     GENERATED_BODY()
     
 public:    
     // Sets default values for this actor's properties
     AMyActor3(const FObjectInitializer& objectInitializer);
 
 protected:
     // Called when the game starts or when spawned
     virtual void BeginPlay() override;
 
 public:    
     // Called every frame
     virtual void Tick(float DeltaTime) override;
 
     UPROPERTY(VisibleAnywhere, BlueprintReadWrite)
     class USphereComponent* _collision;
 
     void OnDelegateExampleOverlap(AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bfromSweep, const FHitResult& SweepResult);
 };


cpp file:

 #include "Youtube.h"
 #include "Components/SphereComponent.h"
 #include "MyActor3.h"
 
 
 // Sets default values
 AMyActor3::AMyActor3(const FObjectInitializer& objectInitializer)
     :Super(objectInitializer)
 {
      // Set this actor to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
     PrimaryActorTick.bCanEverTick = true;
 
     _collision = CreateDefaultSubobject<USphereComponent>(TEXT("RootCollision"));
 
     _collision->SetSphereRadius(250.f);
     _collision->SetHiddenInGame(false);
     _collision->OnComponentBeginOverlap.AddDynamic(this, &AMyActor3::OnDelegateExampleOverlap);
 
     RootComponent = _collision;
 
 }
 
 // Called when the game starts or when spawned
 void AMyActor3::BeginPlay()
 {
     Super::BeginPlay();
     
 }
 
 // Called every frame
 void AMyActor3::Tick(float DeltaTime)
 {
     Super::Tick(DeltaTime);
 
 }
 
 void AMyActor3::OnDelegateExampleOverlap(AActor * OtherActor, UPrimitiveComponent * OtherComp, int32 OtherBodyIndex, bool bfromSweep, const FHitResult & SweepResult)
 {
     GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, "Something overlapped");
 }

And I have another question I dont understand: I always have to include " #include "Components/SphereComponent.h"" to my cpp file to use sphere/box/capsule collisions. In the tutourials, the never have to implement this. Why?

Product Version: UE 4.19
Tags:
more ▼

asked Jul 31 '18 at 04:10 PM in C++ Programming

avatar image

Aneos
25 3 4 10

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

1 answer: sort voted first

Hi there,

The likely cause is that the delegate signature is incorrect. The tutorial you referenced looks to be outdated a bit and the new signature you're looking for, found in PrimitiveComponent.h still, includes an extra "OverlappedComponent" pointer like so:

 (UPrimitiveComponent*, OverlappedComponent, AActor*, OtherActor, UPrimitiveComponent*, OtherComp, int32, OtherBodyIndex, bool, bFromSweep, const FHitResult &, SweepResult)

So your function body should read

 void OnDelegateExampleOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bfromSweep, const FHitResult& SweepResult);

If that doesn't fix the issue then it could be a problem with where you're binding to the function (I've had some weird problems with binding it in the constructor in the past), so you could try binding it on begin play instead. If not that then it could be that the collision settings are such that overlap events aren't triggered, but if you're just using the default settings from a new project then that shouldn't be an issue. Or even having bGenerateOverlapEvents to true is necessary. But try it in that order first and see if it works.

I hope that helps!

To answer your second question though, as of 4.16 the engine now uses an "Include What You Use" system for compiling dependencies so compile times can be faster, which means you have to include what you need explicitly since many header files that used to be included by default are no longer included. The documentation explains it a lot better than I can. Link

more ▼

answered Jul 31 '18 at 04:56 PM

avatar image

poplockentaco
239 21 12 28

avatar image Aneos Jul 31 '18 at 08:37 PM

Thx for the reply. I could solve it and now I can contiue working on my game. :)

(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