物品组件
概述
引入了一个全新的组件系统,这个系统旨在使游戏的自定义和扩展更加容易。这个新系统主要包括以下几个方面
物品组件结构
物品堆实例
组件 TArray
组件 1
组件 2
组件 3
...
物品组件基类
UCLASS()
class CHARMINGCRAFT_API UNativeCraftComponent : public UObject
{
GENERATED_BODY()
public:
UFUNCTION(BlueprintCallable)
bool IsAllowMultipleInstance() const;
UFUNCTION(BlueprintCallable)
void SetIsAllowMultipleInstance(bool bIsAllowMultipleInstance);
UFUNCTION(BlueprintCallable)
bool IsActive() const;
UFUNCTION(BlueprintCallable)
void SetIsActive(bool bIsActive);
UPROPERTY(BlueprintReadWrite, EditAnywhere)
FString Description;
public:
UNativeCraftComponent();
private:
// Whether allow item or entity have multiple same class of components
UPROPERTY(BlueprintReadWrite, EditAnywhere)
bool bIsAllowMultipleInstance = true;
UPROPERTY(BlueprintReadWrite, EditAnywhere)
bool bIsActive = false;
};
有物品组件的物品堆结构
UCLASS(ClassGroup=(Custom), meta=(BlueprintSpawnableComponent))
class CHARMINGCRAFT_API UItemStack : public UPersistentDataContainer, public IComponentizedObjectInterface
{
GENERATED_BODY()
public:
// Sets default values for this component's properties
UItemStack();
UPROPERTY(BlueprintReadWrite, EditAnywhere, Category= "ItemStack Fields")
int32 Amount;
UPROPERTY(BlueprintReadWrite, EditAnywhere, Category= "ItemStack Fields")
EMaterial Material; //
UPROPERTY(BlueprintReadOnly, VisibleAnywhere, Category="ItemStack InternalData")
TSubclassOf<UItem> ItemClass;
UPROPERTY(BlueprintReadWrite, EditAnywhere, Category= "ItemStack Fields")
UItemMeta* ItemMeta;
UPROPERTY()
TArray<UNativeCraftComponent*> Components;
virtual UNativeCraftComponent* AddComponents_Implementation(UNativeCraftComponent* AddedComponents) override;
virtual bool RemoveComponents_Implementation(UNativeCraftComponent* RemovededComponents) override;
virtual TArray<UNativeCraftComponent*> GetObjectComponentsByClass_Implementation(TSubclassOf<UNativeCraftComponent> ComponentsClass) override;
virtual TArray<UNativeCraftComponent*> GetObjectComponents_Implementation() override;
};
注意: 如果想使用物品组件相关功能请让该类实现接口 IComponentizedObjectInterface
该接口包含了物品组件和其对应的物品对象基本操作函数
另外, 你需要在实现该接口的对象中提供 TArray<UNativeCraftComponent*> Components;
这是必须的
物品组件接口
UINTERFACE()
class UComponentizedObjectInterface : public UInterface
{
GENERATED_BODY()
};
class CHARMINGCRAFT_API IComponentizedObjectInterface
{
GENERATED_BODY()
public:
UFUNCTION(BlueprintCallable, BlueprintNativeEvent)
TArray<UNativeCraftComponent*> GetObjectComponents();
UFUNCTION(BlueprintCallable, BlueprintNativeEvent)
TArray<UNativeCraftComponent*> GetObjectComponentsByClass(TSubclassOf<UNativeCraftComponent> ComponentsClass);
UFUNCTION(BlueprintCallable, BlueprintNativeEvent)
UNativeCraftComponent* AddComponents(UNativeCraftComponent* AddedComponents);
UFUNCTION(BlueprintCallable, BlueprintNativeEvent)
bool RemoveComponents(UNativeCraftComponent* RemovededComponents);
};
基本格式
参数描述
作用
GetObjectComponents()
无
获取该UObject所有组件
GetObjectComponentsByClass(TSubclassOf ComponentsClass)
ComponentsClass
为期望搜索组件的类
按照组件类型获取该UObject所有组件
AddComponents(UNativeCraftComponent* AddedComponents)
AddedComponents
添加到UObject的组件
添加组件到UObject
RemoveComponents(UNativeCraftComponent* RemovededComponents)
RemovededComponents
从UObject移除的组件
从UObject移除指定组件
Last updated