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"

OnComponentBeginOverlap not working

What I'm trying to create is an obstacle that the player will pass through, but get damaged when he does (think of crashing head first into some glass). I have added a new collision profile for it in DefaultEngine.ini like this:

 [/Script/Engine.CollisionProfile]
 +Profiles=(Name="ObstacleMesh",CollisionEnabled=QueryAndPhysics,ObjectTypeName="WorldDynamic",CustomResponses=((Channel="Pawn",Response=ECR_Overlap),(Channel="Vehicle",Response=ECR_Overlap),(Channel="Camera",Response=ECR_Ignore)),HelpMessage="WorldDynamic object that overlaps Pawn, Camera, and Vehicle. All other channels will be set to default. ",bCanModify=False)

My obstacle class is set up like this:

 UCLASS()
 class AObstacle : public AActor
 {
     GENERATED_UCLASS_BODY()

     UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Mesh)
         TSubobjectPtr<UStaticMeshComponent> OriginalMesh;

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

And the definitions are:

 AObstacle ::AObstacle(const class FPostConstructInitializeProperties& PCIP)
 : Super(PCIP)
 {
     OriginalMesh = PCIP.CreateDefaultSubobject<UStaticMeshComponent>(this, "OriginalMesh");
     OriginalMesh->BodyInstance.SetInstanceSimulatePhysics(false);
     OriginalMesh->SetCollisionProfileName(TEXT("ObstacleMesh"));
     OriginalMesh->OnComponentBeginOverlap.AddDynamic(this, &AObstacle::OnOverlap);
     RootComponent = OriginalMesh;
 }
     
 void AObstacle::OnOverlap(class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex)
 {
     if (GEngine)
         GEngine->AddOnScreenDebugMessage(-1, 5, FColor::Red, "Overlapped in C++");
 }

But the event won't fire when the pawn enters the mesh. I tried adding the overlap event in the Blueprint subclass I'm placing in the scene, and it fires there, which means the problem is probably not with the setup of collision profiles and channels, but not in C++. Any ideas why this is happening?

Product Version: Not Selected
Tags:
more ▼

asked Apr 20 '14 at 10:30 AM in C++ Programming

avatar image

Arshia001
549 32 36 164

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

2 answers: sort voted first

Answering my own question here, but anyway:

The problem was that I had created the blueprint BEFORE adding the code for the overlap part, and for some reason it wasn't working anymore. So I deleted the blueprint and re-created it, and now everything is working fine. Leaves me wondering though, why should this happen at all?

more ▼

answered Apr 20 '14 at 11:22 AM

avatar image

Arshia001
549 32 36 164

avatar image jpgdallas May 23 '15 at 05:27 AM

Thanks for update. I created my BP after C++ class but made a change around my SphereComponent and while it showed up in BP editor it wasn't until I recreated blue print that it worked so you saved me. Then wasted an hour trying to figure out a crash until realized my Hud class got reset on GameMode bp. But hey I'm having a ball learning to develop with UE :)

avatar image giorgk Nov 08 '15 at 09:20 PM

I wish I had found your answer few hours earlier

avatar image umitcel Jan 03 '16 at 08:33 PM

great, i found this answer after 5 hours, but i think there must be a way to synhcronize the code?

avatar image kingcrimson Sep 10 '16 at 07:17 PM

still happening on Sept 10, 2016 :o(

avatar image Arshia001 Sep 10 '16 at 07:32 PM

This kind of thing really makes me appreciate Unity's decision to base their engine on the .net framework. You just can't beat C#'s simplicity, ease of use and all the existing open source code.

avatar image ballerbaer Apr 03 '17 at 04:44 PM

still happening on March 2017!

Two years now and i spend also an hour finding the problem. I'm just starting with UE but those issues I have, make me overthink my decision to choose UE!

I have just 3 BPs derived from my c++ class, so I "only" have to re-implement those. But I really wonder what happens when I have about 20-30 derived from such a class, that's a nightmare.

Since umitcel's question is still unanswered now it doesn't seem there is a way of synchronizing i guess??

avatar image headstep Jan 13 '18 at 09:45 PM

2018 still happening.

But I have seen that moving the OriginalMesh->OnComponentBeginOverlap.AddDynamic line from the constructor to BeginPlay seemed to solve the issue. Not sure what the other implications of this are.

avatar image Arshia001 Jan 14 '18 at 05:23 AM

I still don't really know what to put in the constructor and what to put in BeginPlay. There are also cases that should work but don't. For example, last I checked, you couldn't assign a physics asset in the constructor as it would crash the engine at startup.

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

As @headstep pointed out on the selected answer's comment, do the event binding on BeginPlay instead of the constructor.

In the constructor only do variable initialisation, and subobject creations. The constructor is executed even in edit mode which is why you see the components you attach to your RootComponent while in the editor. This is similar to how Construction Scripts work on blueprints.

In begin play do things like event bindings, bones attachment, or things that you want done while play time.

more ▼

answered May 16 '18 at 11:22 PM

avatar image

AstonishingLabs
3 1 3

(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