Help me create a fully working crafting and slot based stackable inventory in unreal…
- User
Help me create a fully working crafting and slot based stackable inventory in unreal engine using c++
- Assistant
Help me create a fully working crafting and slot based stackable inventory in unreal engine using c++
Creating a fully functional crafting and slot-based stackable inventory system in Unreal Engine using C++ involves several steps. Below, I'll guide you through the process, breaking it down into manageable parts. This guide assumes you have a basic understanding of Unreal Engine and C++ programming.
Create a base class that represents all items in your game.
UObject.// ItemBase.h
#pragma once
#include "CoreMinimal.h"
#include "UObject/NoExportTypes.h"
#include "ItemBase.generated.h"
UCLASS(Blueprintable)
class YOURGAME_API UItemBase : public UObject
{
GENERATED_BODY()
public:
UItemBase();
// The display name of the item
UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category="Item")
FText ItemName;
// Max stack size for this item
UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category="Item")
int32 MaxStackSize;
// Thumbnail or icon for UI
UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category="Item")
UTexture2D* Thumbnail;
};
Define how each slot in the inventory behaves, including the item it contains and the quantity.
USTRUCT to represent each inventory slot.// InventorySlot.h
#pragma once
#include "CoreMinimal.h"
#include "ItemBase.h"
#include "InventorySlot.generated.h"
USTRUCT(BlueprintType)
struct YOURGAME_API FInventorySlot
{
GENERATED_USTRUCT_BODY()
public:
FInventorySlot();
// The item stored in this slot
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category="Inventory")
UItemBase* Item;
// Quantity of the item
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category="Inventory")
int32 Quantity;
};
The inventory component will manage all slots and provide functionality to add, remove, and query items.
UActorComponent.// InventoryComponent.h
#pragma once
#include "CoreMinimal.h"
#include "Components/ActorComponent.h"
#include "InventorySlot.h"
#include "InventoryComponent.generated.h"
UCLASS(ClassGroup=(Custom), meta=(BlueprintSpawnableComponent))
class YOURGAME_API UInventoryComponent : public UActorComponent
{
GENERATED_BODY()
public:
UInventoryComponent();
// Maximum number of slots in the inventory
UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category="Inventory")
int32 MaxSlots;
// The array of inventory slots
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category="Inventory")
TArray<FInventorySlot> Slots;
// Add item to inventory
UFUNCTION(BlueprintCallable, Category="Inventory")
bool AddItem(UItemBase* Item, int32 Quantity);
// Remove item from inventory
UFUNCTION(BlueprintCallable, Category="Inventory")
bool RemoveItem(UItemBase* Item, int32 Quantity);
// Check if item exists in inventory
UFUNCTION(BlueprintCallable, Category="Inventory")
bool HasItem(UItemBase* Item, int32 Quantity) const;
protected:
virtual void BeginPlay() override;
};
// InventoryComponent.cpp
#include "InventoryComponent.h"
UInventoryComponent::UInventoryComponent()
{
MaxSlots = 20; // Default inventory size
}
void UInventoryComponent::BeginPlay()
{
Super::BeginPlay();
// Initialize inventory slots
Slots.SetNum(MaxSlots);
}
bool UInventoryComponent::AddItem(UItemBase* Item, int32 Quantity)
{
if (!Item || Quantity <= 0)
{
return false;
}
// First, try to stack item in existing slots
for (FInventorySlot& Slot : Slots)
{
if (Slot.Item == Item && Slot.Quantity < Item->MaxStackSize)
{
int32 SpaceLeft = Item->MaxStackSize - Slot.Quantity;
int32 AmountToAdd = FMath::Min(SpaceLeft, Quantity);
Slot.Quantity += AmountToAdd;
Quantity -= AmountToAdd;
if (Quantity <= 0)
{
return true;
}
}
}
// Next, add to empty slots
for (FInventorySlot& Slot : Slots)
{
if (Slot.Item == nullptr)
{
int32 AmountToAdd = FMath::Min(Item->MaxStackSize, Quantity);
Slot.Item = Item;
Slot.Quantity = AmountToAdd;
Quantity -= AmountToAdd;
if (Quantity <= 0)
{
return true;
}
}
}
// Inventory is full
return false;
}
bool UInventoryComponent::RemoveItem(UItemBase* Item, int32 Quantity)
{
if (!Item || Quantity <= 0)
{
return false;
}
// Remove items from slots
for (FInventorySlot& Slot : Slots)
{
if (Slot.Item == Item)
{
int32 AmountToRemove = FMath::Min(Slot.Quantity, Quantity);
Slot.Quantity -= AmountToRemove;
Quantity -= AmountToRemove;
if (Slot.Quantity <= 0)
{
Slot.Item = nullptr;
Slot.Quantity = 0;
}
if (Quantity <= 0)
{
return true;
}
}
}
// Not enough items to remove
return false;
}
bool UInventoryComponent::HasItem(UItemBase* Item, int32 Quantity) const
{
int32 TotalQuantity = 0;
for (const FInventorySlot& Slot : Slots)
{
if (Slot.Item == Item)
{
TotalQuantity += Slot.Quantity;
if (TotalQuantity >= Quantity)
{
return true;
}
}
}
return false;
}
Stackable items are handled by checking MaxStackSize in UItemBase. The AddItem and RemoveItem functions already consider stacking logic.
MaxStackSize to 1 for items that shouldn't stack.MaxStackSize per slot.Implement a system where players can combine items to create new ones.
// CraftingRecipe.h
#pragma once
#include "CoreMinimal.h"
#include "ItemBase.h"
#include "CraftingRecipe.generated.h"
USTRUCT(BlueprintType)
struct YOURGAME_API FCraftingRecipe
{
GENERATED_USTRUCT_BODY()
public:
// Inputs required for crafting
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category="Crafting")
TMap<UItemBase*, int32> Ingredients;
// Output item of crafting
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category="Crafting")
UItemBase* Result;
// Quantity of the result
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category="Crafting")
int32 ResultQuantity;
};
// CraftingComponent.h
#pragma once
#include "CoreMinimal.h"
#include "Components/ActorComponent.h"
#include "CraftingRecipe.h"
#include "InventoryComponent.h"
#include "CraftingComponent.generated.h"
UCLASS( ClassGroup=(Custom), meta=(BlueprintSpawnableComponent) )
class YOURGAME_API UCraftingComponent : public UActorComponent
{
GENERATED_BODY()
public:
UCraftingComponent();
// List of all crafting recipes
UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category="Crafting")
TArray<FCraftingRecipe> CraftingRecipes;
// Reference to the inventory component
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category="Crafting")
UInventoryComponent* InventoryComponent;
// Attempt to craft an item
UFUNCTION(BlueprintCallable, Category="Crafting")
bool CraftItem(UItemBase* ItemToCraft);
};
// CraftingComponent.cpp
#include "CraftingComponent.h"
UCraftingComponent::UCraftingComponent()
{
}
bool UCraftingComponent::CraftItem(UItemBase* ItemToCraft)
{
if (!ItemToCraft || !InventoryComponent)
{
return false;
}
// Find the recipe for the item
FCraftingRecipe* FoundRecipe = nullptr;
for (FCraftingRecipe& Recipe : CraftingRecipes)
{
if (Recipe.Result == ItemToCraft)
{
FoundRecipe = &Recipe;
break;
}
}
if (!FoundRecipe)
{
// No recipe found
return false;
}
// Check if the player has all ingredients
for (const auto& Ingredient : FoundRecipe->Ingredients)
{
if (!InventoryComponent->HasItem(Ingredient.Key, Ingredient.Value))
{
return false; // Missing ingredients
}
}
// Remove ingredients from inventory
for (const auto& Ingredient : FoundRecipe->Ingredients)
{
InventoryComponent->RemoveItem(Ingredient.Key, Ingredient.Value);
}
// Add crafted item to inventory
InventoryComponent->AddItem(FoundRecipe->Result, FoundRecipe->ResultQuantity);
return true;
}
UCLASS(Blueprintable) and UFUNCTION(BlueprintCallable) to make classes and functions usable in Blueprints.UInventoryComponent and UCraftingComponent to your player character or another actor.// In your PlayerCharacter.h
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category="Components")
UInventoryComponent* InventoryComponent;
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category="Components")
UCraftingComponent* CraftingComponent;
// In your PlayerCharacter.cpp
APlayerCharacter::APlayerCharacter()
{
InventoryComponent = CreateDefaultSubobject<UInventoryComponent>(TEXT("InventoryComponent"));
CraftingComponent = CreateDefaultSubobject<UCraftingComponent>(TEXT("CraftingComponent"));
// Set references
CraftingComponent->InventoryComponent = InventoryComponent;
}
To visualize the inventory and crafting system:
By following these steps, you've set up a basic but functional inventory and crafting system in Unreal Engine using C++. You can expand upon this foundation by adding features like inventory sorting, equipment systems, advanced crafting mechanics, and more detailed UI interactions.
UItemBase for different item behaviors (e.g., consumables, equipment).Feel free to ask if you need further assistance or clarification on any of the steps!
Not yet included in a published release.