为了不使用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
*/
在代码中定义接口主要有两个步骤:
- 定义继承自
UInterface
的接口类: 命名为 “U + 接口名” - 定义名字为“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<URPGItem*, FRPGItemData>& GetInventoryDataMap() const = 0;
/** Returns the map of slots to items */
virtual const TMap<FRPGItemSlot, URPGItem*>& 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;
};
网友评论