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 [C++]

Trying to create a valuable that can be picked up by the player, however I can't get OnComponentBeginOverlap to work. VALUABLE.H:

    #include "GameFramework/Actor.h"
     #include "Valuable.generated.h"
     
     UCLASS()
     class FANTASY8BIT_API AValuable : public AActor
     {
         GENERATED_BODY()
         
     public:    
         // Sets default values for this actor's properties
         AValuable();
     
         // Called when the game starts or when spawned
         virtual void BeginPlay() override;
         
         // Called every frame
         virtual void Tick( float DeltaSeconds ) override;
     
     
     protected:
         // Overlap event that is called when player collets valuable
         UFUNCTION(BlueprintCallable, Category = "Valuable")
         void onPickUp(class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult);
     
         UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Valuable", meta = (AllowPrivateAcces = "true"))
         USphereComponent* collisionSphere;
     
     };
 
 

VALUABLE.CPP:

 #include "Fantasy8bit.h"
 #include "PaperFlipbookComponent.h"
 #include "Valuable.h"
 
 
 // Sets default values
 AValuable::AValuable()
 {
      // 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;
     collisionSphere = CreateDefaultSubobject<USphereComponent>(TEXT("Collision Sphere"));
     RootComponent = collisionSphere;
     
 
     // Bind a call to onPickUp function when sphere is being overlapped
     collisionSphere->OnComponentBeginOverlap.AddDynamic(this, &AValuable::onPickUp);
 }
 
 // Called when the game starts or when spawned
 void AValuable::BeginPlay()
 {
     Super::BeginPlay();
     
 }
 
 // Called every frame
 void AValuable::Tick( float DeltaTime )
 {
     Super::Tick( DeltaTime );
 
 }
 
 void AValuable::onPickUp(class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
 {
     if (OtherActor != nullptr && GetWorld() && OtherActor == UGameplayStatics::GetPlayerCharacter(GetWorld(), 0))
     {
         Destroy();
     }
 
 }


This code gives the following error when compiled:

 CompilerResultsLog:Error: Error D:\MyWork\UnrealEngine4_Projects\Fantasy8bit\Source\Fantasy8bit\Valuable.cpp(20) : error C2664: 'void TBaseDynamicMulticastDelegate<FWeakObjectPtr,void,UPrimitiveComponent *,AActor *,UPrimitiveComponent *,int32,bool,const FHitResult &>::__Internal_AddDynamic<AValuable>(UserClass *,void (__cdecl AValuable::* )(UPrimitiveCompone
 nt *,AActor *,UPrimitiveComponent *,int32,bool,const FHitResult &),FName)': cannot convert argument 2 from 'void (__cdecl AValuable::* )(AActor *,UPrimitiveComponent *,int32,bool,const FHitResult &)' to 'void (__cdecl AValuable::* )(UPrimitiveComponent *,AActor *,UPrimitiveComponent *,int32,bool,const FHitResult &)'
 
 CompilerResultsLog:Error: Error D:\MyWork\UnrealEngine4_Projects\Fantasy8bit\Source\Fantasy8bit\Valuable.cpp(20) : note: Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast

Any help is appreciated, thank you!

Product Version: UE 4.13
Tags:
more ▼

asked Nov 19 '16 at 02:20 PM in C++ Programming

avatar image

StenSven
45 8 12 18

avatar image NymKappa Nov 19 '16 at 02:47 PM

Seriously ? Isn't it enought ? :D

 cannot convert argument 2 from
     -> 'void (__cdecl AValuable::* )(AActor *,UPrimitiveComponent *,int32,bool,const FHitResult &)' to
     ->'void (__cdecl AValuable::* )(UPrimitiveComponent *,AActor *,UPrimitiveComponent *,int32,bool,const FHitResult &)'
avatar image StenSven Nov 19 '16 at 06:10 PM

I changed the function arguments to:

 void AValuable::onPickUp(class UPrimitiveComponent* Comp, class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)

It now compiles! However the function is for some reason not called when the collision sphere is overlapped.

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

2 answers: sort voted first

I used OnActorBeginOverlap instead, now everything works as expected

more ▼

answered Nov 21 '16 at 08:15 AM

avatar image

StenSven
45 8 12 18

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

Make sure Collision Profile Name is correctly set and Generating Overlap Events is enabled

     CollisionSphere->SetCollisionProfileName(TEXT("OverlapAll"));
     CollisionSphere->bGenerateOverlapEvents = true;
more ▼

answered Nov 19 '16 at 06:51 PM

avatar image

Mhousse1247
3.5k 152 29 318

avatar image StenSven Nov 19 '16 at 08:00 PM

Was hoping this would work, but nope.. Know that for a fact since I use: UE_LOG(LogTemp, Warning, TEXT("Function called")); But nothing is written to the output log.

(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