# 物品组件

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

## 概述

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

<details>

<summary><strong>数据驱动</strong></summary>

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

</details>

<details>

<summary><strong>模块化</strong></summary>

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

</details>

<details>

<summary><strong>易用性</strong></summary>

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

</details>

<details>

<summary><strong>支持扩展</strong></summary>

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

</details>

## 物品组件结构

物品堆实例

* 组件 TArray
  * 组件 1
  * 组件 2
  * 组件 3
  * ...

## 物品组件基类

{% code title="NativeCraftComponent.h" %}

```cpp
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;
};
```

{% endcode %}

## 有物品组件的物品堆结构

{% code title="ItemStack.h" %}

```cpp
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;
	
};
```

{% endcode %}

{% hint style="warning" %}
注意: 如果想使用物品组件相关功能请让该类实现接口 `IComponentizedObjectInterface` 该接口包含了物品组件和其对应的物品对象基本操作函数

\
另外, 你需要在实现该接口的对象中提供 `TArray<UNativeCraftComponent*> Components;` 这是必须的
{% endhint %}

## 物品组件接口

{% code title="ComponentizedObjectInterface.h" %}

```cpp
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);
};
```

{% endcode %}

<table><thead><tr><th width="347">基本格式</th><th width="212">参数描述</th><th>作用</th></tr></thead><tbody><tr><td><code>GetObjectComponents()</code></td><td>无</td><td>获取该UObject所有组件</td></tr><tr><td><code>GetObjectComponentsByClass(TSubclassOf ComponentsClass)</code></td><td><code>ComponentsClass</code> 为期望搜索组件的类</td><td>按照组件类型获取该UObject所有组件</td></tr><tr><td><code>AddComponents(UNativeCraftComponent* AddedComponents)</code></td><td><code>AddedComponents</code> 添加到UObject的组件</td><td>添加组件到UObject</td></tr><tr><td><code>RemoveComponents(UNativeCraftComponent* RemovededComponents)</code></td><td><code>RemovededComponents</code> 从UObject移除的组件</td><td>从UObject移除指定组件</td></tr></tbody></table>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://charmingcraft.caizii.org/you-xi-nei-rong/wu-pin-yu-ji-neng/wu-pin-zu-jian.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
