首页 > 解决方案 > 虚幻 C++ 错误:错误 C2248:“UPrimitiveComponent::bGenerateOverlapEvents”:无法访问在“UPrimitiveComponent”类中声明的私有成员

问题描述

我对 C++ 和虚幻引擎 4 非常陌生。

我正在使用盒子对撞机创建一个拾取脚本,但我的拾取脚本出现了一个错误。错误 C2248:“UPrimitiveComponent::bGenerateOverlapEvents”:无法访问在“UPrimitiveComponent”类中声明的私有成员:

我真的很想解决这个问题,但我找不到我需要的解决方法!

这是我的标题代码:

// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "Pickup.generated.h"

UCLASS()
class XAVIER_CPP_TUT_API APickup : public AActor
{
    GENERATED_BODY()
    
public:
    // Sets default values for this actor's properties
    APickup();

//  protected:
    // Called when the game starts or when spawned
    virtual void BeginPlay() override;

public:
// Called every frame
virtual void Tick(float DeltaSeconds) override;


//*********************************************

UPROPERTY(EditAnywhere)
    USceneComponent* PickupRoot;

//The static mesh for the pick up
UPROPERTY(EditAnywhere)
    UStaticMeshComponent* PickupMesh;

UPROPERTY(EditAnywhere)
    UShapeComponent* PickupBox;

UFUNCTION()
    void OnPlayerEnterPickupBox(UPrimitiveComponent* OverlappedComp, AActor* OtherActor, UPrimitiveComponent* otherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult);

};

这是我的 .cpp 代码:

// Fill out your copyright notice in the Description page of Project Settings.

#include "Pickup.h"
#include "Classes/Components/ShapeComponent.h"
#include "Classes/Components/BoxComponent.h"
#include "Components/StaticMeshComponent.h"

// Sets default values
APickup::APickup()
{
    // 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;

#pragma region Declaratie_Variabelen
    PickupRoot = CreateDefaultSubobject<USceneComponent>(TEXT("PickupRoot"));
    RootComponent = PickupRoot;

    PickupMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("PickupMesh"));
    PickupMesh->AttachToComponent(PickupRoot, FAttachmentTransformRules::SnapToTargetNotIncludingScale);

    // * Maak een Colliderbox -->
    PickupBox = CreateDefaultSubobject<UBoxComponent>(TEXT("PickupBox"));
    PickupBox->SetWorldScale3D(FVector(1.0f, 1.0f, 1.0f));
    PickupBox->bGenerateOverlapEvents = true;
    PickupBox->OnComponentBeginOverlap.AddDynamic(this, &APickup::OnPlayerEnterPickupBox);
    PickupBox->AttachToComponent(PickupRoot, FAttachmentTransformRules::SnapToTargetNotIncludingScale);
    // <-- Maak een Colliderbox *

#pragma endregion Declaratie_Variabelen
}

// Called when the game starts or when spawned
void APickup::BeginPlay()
{
    Super::BeginPlay();

}

// Called every frame
void APickup::Tick(float DeltaTime)
{
    Super::Tick(DeltaTime);

}

#pragma region Custom Functions
void APickup::OnPlayerEnterPickupBox(UPrimitiveComponent * OverlappedComp, AActor * OtherActor, UPrimitiveComponent * otherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult & SweepResult)
{
    Destroy();
}

#pragma endregion Custom Functions

标签: unreal-engine4unreachable-code

解决方案


尝试PickupBox->SetGenerateOverlapEvents(true);代替PickupBox->bGenerateOverlapEvents = true;

https://api.unrealengine.com/INT/API/Runtime/Engine/Components/UPrimitiveComponent/index.html


推荐阅读