0

我对虚幻引擎和 C++ 很陌生。我正在虚幻中创建一个逃生室,但遇到了错误 C4458,我正在寻找是否有人可以帮助我看看我能做些什么来修复它。任何帮助表示赞赏!

Visual Studio 中未显示错误,仅在虚幻引擎编译器日志中显示。当我尝试编译 C++ 脚本时,会显示此错误:

Character_Controller.h(45):错误 C4458:'item' 的声明隐藏了类成员 Character_Controller.h(34):注意:请参阅 'FInventoryItem::item' 的声明

这是 Character_Controller.h 脚本:

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

#pragma once

#include "CoreMinimal.h"
#include "Components/ActorComponent.h"
#include "GameFramework/Actor.h"
#include "GameFramework/Character.h"
#include "Components/InputComponent.h"
#include "Engine/DataTable.h"
#include "Engine/World.h"
#include "EscapeRoomGameModeBase.h"
#include "Item.h"
#include "Components/SphereComponent.h"
#include "Character_Controller.generated.h"

USTRUCT(BlueprintType)
struct FInventoryItem: public FTableRowBase
{
    GENERATED_BODY()

public:

    FInventoryItem()
    {
        name = FText::FromString("item");
        isVisible = false;
    }

    UPROPERTY(EditAnywhere, BlueprintReadWrite)
        FName itemId;

    UPROPERTY(EditAnywhere, BlueprintReadWrite)
        TSubclassOf<class AItem> item; <--- LINE 34

    UPROPERTY(EditAnywhere, BlueprintReadWrite)
        FText name;

    UPROPERTY(EditAnywhere, BlueprintReadWrite)
        bool isVisible;

    UPROPERTY(EditAnywhere, BlueprintReadWrite)
        UTexture2D* image;

    bool operator==(const FInventoryItem& item) const <---- ERROR HERE
    {
        if (itemId == item.itemId)
            return true;
        else return false;
    }
};

UCLASS()
class ESCAPEROOM_API ACharacter_Controller : public ACharacter
{
    GENERATED_BODY()

public:
    // Sets default values for this character's properties
    ACharacter_Controller();

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

    UFUNCTION(BlueprintCallable, Category = "Items")
        void Collect();

    UFUNCTION()
        void InventoryPlus();

    UFUNCTION()
        void InventoryMinus();

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

    // Called to bind functionality to input
    virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override;
    void Horizontal_Movement(float axis);
    void Vertical_Movement(float axis);

    UFUNCTION(BlueprintCallable, Category = "Utilities")
        void AddToInventory(FName id);

    UFUNCTION(BlueprintCallable, Category = "Utilities")
        void RemoveFromInventory();

    UFUNCTION()
        void Wielding();

    UPROPERTY(VisibleAnywhere, BlueprintReadWrite)
        TArray<FInventoryItem> inventory;

    UPROPERTY(EditAnywhere, BlueprintReadWrite)
        int inventoryI;

    UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = Camera, meta = (AllowPrivateAccess = "true"))
        class USphereComponent* collectionRange;

    UPROPERTY(EditAnywhere, BlueprintReadWrite)
        FInventoryItem wield;

    UPROPERTY(EditAnywhere, BlueprintReadWrite)
        FInventoryItem empty;

    UPROPERTY(EditAnywhere, BlueprintReadWrite)
        TArray<FInventoryItem> pickupableObjects;

    UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Wield Items")
        TArray<AActor*> wieldObjects;

    FORCEINLINE class USphereComponent* GetCollectionRange() const { return collectionRange; }


};

这是 Character_Controller.cpp 脚本:

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


#include "Character_Controller.h"

// Sets default values
ACharacter_Controller::ACharacter_Controller()
{
    // Set this character to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
    PrimaryActorTick.bCanEverTick = true;


    inventoryI = 0;
    empty.itemId = "0";

    collectionRange = CreateDefaultSubobject<USphereComponent>(TEXT("collectionRange"));
    collectionRange->AttachToComponent(RootComponent, FAttachmentTransformRules(EAttachmentRule::SnapToTarget, true));
    collectionRange->AttachToComponent(RootComponent, FAttachmentTransformRules(EAttachmentRule::SnapToTarget, true));
    collectionRange->SetSphereRadius(100.0f);

}

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

    for (int i = 0; i < wieldObjects.Num(); i++)
    {
        if (wieldObjects[i])
        {
            wieldObjects[i]->SetActorHiddenInGame(true);
        }
    }
    
}

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

}

// Called to bind functionality to input
void ACharacter_Controller::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
    Super::SetupPlayerInputComponent(PlayerInputComponent);

    InputComponent->BindAxis("MoveX", this, &ACharacter_Controller::Vertical_Movement);
    InputComponent->BindAxis("MoveY", this, &ACharacter_Controller::Horizontal_Movement);

    InputComponent->BindAxis("CameraSide", this, &ACharacter_Controller::AddControllerYawInput);
    InputComponent->BindAxis("CameraUp", this, &ACharacter_Controller::AddControllerPitchInput);

    InputComponent->BindAction("Interaction", IE_Pressed, this, &ACharacter_Controller::Collect);
    InputComponent->BindAction("InteractionPlus", IE_Pressed, this, &ACharacter_Controller::InventoryPlus);
    InputComponent->BindAction("InteractionMinus", IE_Pressed, this, &ACharacter_Controller::InventoryMinus);
}

void ACharacter_Controller::Horizontal_Movement(float axis)
{
    if (axis)
    {
        AddMovementInput(GetActorRightVector(), axis);
    }
}

void ACharacter_Controller::Vertical_Movement(float axis)
{
    if (axis)
    {
        AddMovementInput(GetActorForwardVector(), axis);
    }
}

void ACharacter_Controller::Collect()
{
    TArray<AActor*> collectedItems;
    collectionRange->GetOverlappingActors(collectedItems);

    for (int i = 0; i < collectedItems.Num(); i++)
    {
        AItem* const testItem = Cast<AItem>(collectedItems[i]);
        if (testItem && testItem->GetActive())
        {
            testItem->Touched();
            AddToInventory(testItem->itemID);
            testItem->SetActive(false);
        }
    }
}

void ACharacter_Controller::InventoryPlus()
{
    inventoryI += 1;
    if (inventoryI >= 5)
    {
        inventoryI = 0;
    }
    Wielding();
}

void ACharacter_Controller::InventoryMinus()
{
    inventoryI -= 1;
    if (inventoryI < 0)
    {
        inventoryI = 4;
    }
    Wielding();
}

void ACharacter_Controller::AddToInventory(FName id)
{
    AEscapeRoomGameModeBase* gameMode = Cast<AEscapeRoomGameModeBase>(GetWorld()->GetAuthGameMode());
    UDataTable* itemTable = gameMode->GetItemsDB();

    if (gameMode)
    {
        if (itemTable)
        {
            FInventoryItem* itemAdded = itemTable->FindRow<FInventoryItem>(id, "");
            if (itemAdded)
            {
                inventory.Add(*itemAdded);
                Wielding();
            }
        }
    }
}

void ACharacter_Controller::RemoveFromInventory()
{
    if (inventory.Num() > inventoryI)
    {
        inventory.RemoveAt(inventoryI);
        Wielding();
    }
}

void ACharacter_Controller::Wielding()
{
    if (inventory.Num() > inventoryI)
    {
        wield = inventory[inventoryI];
        if (&wield)
        {
            FString wieldValue = wield.itemId.ToString();
            int wieldI = FCString::Atoi(*wieldValue);
            for (int i = 0; i < pickupableObjects.Num(); i++)
            {
                if (wield.itemId == pickupableObjects[i].itemId)
                {
                    if (wieldObjects[wieldI - 1])
                    {
                        wieldObjects[wieldI - 1]->SetActorHiddenInGame(false);
                    }
                }

                else
                {
                    if (wieldObjects[i])
                    {
                        wieldObjects[i]->SetActorHiddenInGame(true);
                    }
                }
            }
        }
        else
        {
            wield = empty;
        }

        if (wield == empty)
        {
            for (int i = 0; i < pickupableObjects.Num(); i++)
            {
                wieldObjects[i]->SetActorHiddenInGame(true);
            }
        }
    }
}
4

0 回答 0