十二、RPGInventoryInterface.h & Interface

为了不使用cast,ActionRPG项目定义了一个背包接口类,让Controller继承并实现。

/**
 * Interface for actors that provide a set of RPGItems bound to ItemSlots
 * This exists so RPGCharacterBase can query inventory without doing hacky player controller casts
 * It is designed only for use by native classes
 */

在代码中定义接口主要有两个步骤:

  1. 定义继承自UInterface的接口类: 命名为 “U + 接口名”
  2. 定义名字为“I + 接口名”的类,在这个类中定义接口方法

更多关于Interface的信息,请查看:
Interfaces in C++

// Copyright 1998-2019 Epic Games, Inc. All Rights Reserved.

#pragma once

#include "ActionRPG.h"
#include "RPGInventoryInterface.generated.h"

/**
 * Interface for actors that provide a set of RPGItems bound to ItemSlots
 * This exists so RPGCharacterBase can query inventory without doing hacky player controller casts
 * It is designed only for use by native classes
 */
UINTERFACE(MinimalAPI, meta = (CannotImplementInterfaceInBlueprint))
class URPGInventoryInterface : public UInterface
{
    GENERATED_BODY()
};

class ACTIONRPG_API IRPGInventoryInterface
{
    GENERATED_BODY()

public:
    /** Returns the map of items to data */
    virtual const TMap& GetInventoryDataMap() const = 0;

    /** Returns the map of slots to items */
    virtual const TMap& GetSlottedItemMap() const = 0;

    /** Gets the delegate for inventory item changes */
    virtual FOnInventoryItemChangedNative& GetInventoryItemChangedDelegate() = 0;

    /** Gets the delegate for inventory slot changes */
    virtual FOnSlottedItemChangedNative& GetSlottedItemChangedDelegate() = 0;

    /** Gets the delegate for when the inventory loads */
    virtual FOnInventoryLoadedNative& GetInventoryLoadedDelegate() = 0;
};

你可能感兴趣的:(十二、RPGInventoryInterface.h & Interface)