物品组件

物品组件 (CraftItemComponents) 允许多个组件存在物品以及实体中, 这个系统旨在使游戏的自定义和扩展更加容易。

概述

引入了一个全新的组件系统,这个系统旨在使游戏的自定义和扩展更加容易。这个新系统主要包括以下几个方面

数据驱动

组件系统允许通过JSON文件定义物品和方块的行为。这使得玩家和开发者可以更容易地添加或修改游戏内容,而不需要直接修改游戏代码。

模块化

组件系统是模块化的,意味着不同的组件可以被组合在一起,为物品和方块赋予多种行为。这种设计使得定制和扩展变得非常灵活。

易用性

通过组件系统,创建新物品和方块的过程更加直观和易于理解。玩家只需要编辑一些配置文件,就可以实现复杂的游戏机制。

支持扩展

该系统为未来的扩展和更新奠定了基础。

物品组件结构

物品堆实例

  • 组件 TArray

    • 组件 1

    • 组件 2

    • 组件 3

    • ...

物品组件基类

NativeCraftComponent.h
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;
};

有物品组件的物品堆结构

ItemStack.h
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; 这是必须的

物品组件接口

ComponentizedObjectInterface.h
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