Announcement

Collapse
No announcement yet.

OnComponentBeginOverlap doesn't work in UE 4.20.3!

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    OnComponentBeginOverlap doesn't work in UE 4.20.3!

    Hello again, another noob question from a new-starter.

    I was following a tutorial from a site where you simply create bunch of static meshes moving along an axis.When you hit the meshes, you will be teleported to your started position.My Problem is with the onComponentBeginOverlap function in the UE 4 C++ api. I tried the exact steps and couldn't manage this to work.Can anybody help?

    MyActor.h
    Code:
    // Fill out your copyright notice in the Description page of Project Settings.
    
    #pragma once
    
    #include "CoreMinimal.h"
    #include "GameFramework/Actor.h"
    #include "Components/ShapeComponent.h"
    #include "Components/BoxComponent.h"
    #include "Components/PrimitiveComponent.h"
    #include "MyActor.generated.h"
    
    
    
    UCLASS()
    class TUT_FPSHORROR_API AMyActor : public AActor
    {
        GENERATED_BODY()
    
    public:    
        // Sets default values for this actor's properties
        AMyActor();
        UPROPERTY(EditAnywhere)
            UShapeComponent* SC_Root;
    
        UPROPERTY(EditAnywhere)
            UStaticMeshComponent* SM_MyMesh;
        UPROPERTY(EditAnywhere)
            float flSpeedScale;
        UPROPERTY(EditAnywhere)
            FVector Vec3dPlayerStart;
    
        UFUNCTION()
            void TriggerEnter(class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult);
    
    protected:
        // Called when the game starts or when spawned
        virtual void BeginPlay() override;
    
    public:    
        // Called every frame
        virtual void Tick(float DeltaTime) override;
        float FlTime;
    
    
    };
    MyActor.cpp
    Code:
    // Fill out your copyright notice in the Description page of Project Settings.
    
    #include "MyActor.h"
    
    
    // Sets default values
    AMyActor::AMyActor()
    {
         // 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;
        SC_Root = CreateDefaultSubobject<UBoxComponent>(TEXT("Root"));
    
        SC_Root->GetGenerateOverlapEvents();
        SC_Root->OnComponentBeginOverlap.AddDynamic(this, &AMyActor::TriggerEnter);
        RootComponent = SC_Root;
    
        SM_MyMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("MyMesh"));
        SM_MyMesh->AttachTo(RootComponent);
    
        Vec3dPlayerStart = FVector(-350.0f, -99.0f, 235.0f);
        flSpeedScale = 45.0f;
    
    
    
    }
    
    // Called when the game starts or when spawned
    void AMyActor::BeginPlay()
    {
        Super::BeginPlay();
    
    
    }
    
    // Called every frame
    void AMyActor::Tick(float DeltaTime)
    {
        Super::Tick(DeltaTime);
        FVector Loc = GetActorLocation();
        float DeltaHeight = FMath::Sin(FlTime + DeltaTime) - FMath::Sin(DeltaTime);
        Loc.Y += DeltaHeight * flSpeedScale;
        FlTime += DeltaTime;
        AMyActor::SetActorLocation(Loc);
    
    }
    
    //void AMyActor::TriggerEnter(class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
    //{
    //    OtherActor->SetActorLocation(Vec3dPlayerStart);
    //}
    Screenshot of the errors:

    Click image for larger version

Name:	BeginOverlapError.jpg
Views:	493
Size:	488.8 KB
ID:	1531873

    #2
    AttachTo() is deprecated. You need to use AttachToComponent() instead, as the compiler output tells you. You also need to change your OnComponentBeginOverlap delegate to match the signature. Quick fixes for your code below should work, although I suggest you look at the docs and understand the changes:

    Code:
    // use AttachToComponent instead of AttachTo
    SM_MyMesh->AttachToComponent(RootComponent, FAttachmentTransformRules::KeepRelativeTransform);
    
    // update the parameters for TriggerEnter in both the .h and .cpp
    void TriggerEnter(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult);
    Edit: AttachTo() is not yet deprecated but will be in a future release, so that was just a warning, not an error. The mismatched OnComponentBeginOverlap signature was causing the compile to fail.
    Last edited by Matt Jaques; 09-24-2018, 11:26 PM.

    Comment


      #3
      Thank you I was searching for the answer for like hours! By the way, I should add AMyActor:: to the Function in the .cpp file, otherwise the complier wouldn't recognize Vec3dPlayerStart

      Comment

      Working...
      X