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"

How to detect triggerbox collision with actor

I need a way of checking if my player is colliding with a triggerbox in ue4

Something like this:

if(player->isTouching(myTriggerBox)) { // do something } void AMyBox::ExitTrigger(class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult & SweepResult) { if (GEngine) { GEngine->AddOnScreenDebugMessage(0, 10.0f, FColor::White, TEXT("Exit overlap")); } } and header definition: void ExitTrigger(class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult & SweepResult);

Product Version: UE 4.8
Tags:
more ▼

asked Jun 21 '15 at 10:16 PM in C++ Programming

avatar image

ReubenKun
11 1 2 4

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

1 answer: sort voted first

In your trigger box constructor specify the function you want called like this:

 MyBox->OnComponentBeginOverlap.AddDynamic(this, &AMyTriggerActor::TriggerEnter);  
 // I call it TriggerEnter, call it whatever  you like

and then have a function that does what you want to have happen:

 void AMyTriggerActor::TriggerEnter(class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult & SweepResult)
 {
 //do stuff
 return;
 }

more ▼

answered Jun 21 '15 at 10:45 PM

avatar image

pagancyc
135 17 13 23

avatar image ReubenKun Jun 22 '15 at 12:31 AM

Thanks so much man, I really appreciate it!

avatar image pagancyc Jun 22 '15 at 01:08 AM

That's odd, AddDynamic doesn't show up in my dropdown either, but it compiles and works. No idea what it doesn't show up, kind of bizarre.

Here is a larger code dump, maybe it will help...

MyStuffDoer.h:

 class STUFFDEMO_API AMyStuffDoer : public AActor
 {
     GENERATED_BODY()
     
     AMyStuffDoer(const FObjectInitializer& ObjectInitializer);
 
     UShapeComponent* Box;
 
     UFUNCTION()
         void TriggerEnter(class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult & SweepResult);
 }

MyStuffDoer.cpp:

 AMyStuffDoer::AMyStuffDoer(const FObjectInitializer& ObjectInitializer)
 : Super(ObjectInitializer)
 {
     Box = ObjectInitializer.CreateDefaultSubobject<UBoxComponent>(this, TEXT("Box"));
     Box->bGenerateOverlapEvents = true;
     Box->SetRelativeScale3D(Stretch);
     RootComponent = Box;
     Box->OnComponentBeginOverlap.AddDynamic(this, &AMyStuffDoer::TriggerEnter);
 }











avatar image pagancyc Jun 22 '15 at 12:34 AM

I've gotten help here, only fair that I pass it along.

If you upvote the answer and mark your question resolved, it will be helpful for the next person searching this topic.

And I will also get some sweet sweet internet karma! ;-)

avatar image ReubenKun Jun 22 '15 at 12:52 AM

@pagancyc I've tried using your advice, but when I try to call the adddynamic function I cannot find it, only Add, AddUnique and __Internal_AddDynamic. The class is of type actor, what am I doing wrong?

avatar image ReubenKun Jun 22 '15 at 01:39 AM

@pagancyc Thanks! Just one more thing, I tried adding a OnComponentEndOverlap dynamic like so: Box->OnComponentEndOverlap.AddDynamic(this, &AMyBox::TriggerExit); Doing so gave me the error no instance of function template matches the argument list. Any ideas?

avatar image pagancyc Jun 22 '15 at 01:41 AM

Did you declare it in your header file?

avatar image ReubenKun Jun 22 '15 at 01:47 AM

@pagancyc Yes, I sure did

avatar image pagancyc Jun 22 '15 at 01:50 AM

Well, then you've got me stumped. I've never seen any difference in declaration/syntax between the two other than 'Begin' and 'End'.

avatar image ReubenKun Jun 22 '15 at 01:52 AM

Heres my function definition: void AMyBox::ExitTrigger(class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult & SweepResult) { if (GEngine) { GEngine->AddOnScreenDebugMessage(0, 10.0f, FColor::White, TEXT("Exit overlap")); } } and header definition: void ExitTrigger(class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult & SweepResult);

I could swear nothing is wrong, anyways let me know if you see whats wrong

avatar image pagancyc Jun 22 '15 at 02:24 AM

I misspoke earlier, there is a difference besides Begin/End. The End function has different arguments:

 void AMyStuffDoer::TriggerExit(class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex)
 {
 ...
 }

(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