Appearance
First inventory in C++
This walkthrough creates one authoritative 6 × 4 grid with an ammunition stack.
1. Configure the item
Create a URicItemDefinition asset named ID_Ammo. Add:
- Text (Name, Description): readable name and description.
- Shape: Jigsaw (
URicItemFragmentJigsawItemShape): 1 column and 1 row. - Stats (
URicItemFragmentSetStats):InitialItemStatswithRic.Item.Stat.MaxStackSize = 20.
Icon 2D is optional presentation data.
2. Configure the container
Create a URicContainerDefinition asset named CD_Main. Set the main ContainerFragment to URicContainerFragmentJigsawFixedSize.
Set ContainerTag to Ric.Container.Type.Main. Set Shape to 6 columns and 4 rows, with all intended cells enabled. The mask determines usable cells.
The main fragment chooses the runtime class. Additional filter fragments belong in the separate Fragments array.
3. Create an inventory owner
Replace YOURGAME_API with your module's API macro.
InventoryOwner.h
cpp
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "InventoryOwner.generated.h"
class URicContainerManagerComponent;
class URicContainerDefinition;
class URicItemDefinition;
UCLASS()
class YOURGAME_API AInventoryOwner : public AActor
{
GENERATED_BODY()
public:
AInventoryOwner();
protected:
virtual void BeginPlay() override;
UPROPERTY(VisibleAnywhere, Category = "Inventory")
TObjectPtr<URicContainerManagerComponent> Inventory;
UPROPERTY(EditDefaultsOnly, Category = "Inventory")
TObjectPtr<URicContainerDefinition> MainContainerDefinition;
UPROPERTY(EditDefaultsOnly, Category = "Inventory")
TObjectPtr<URicItemDefinition> StarterItemDefinition;
};InventoryOwner.cpp
cpp
#include "InventoryOwner.h"
#include "Components/RicContainerManagerComponent.h"
#include "Definitions/RicContainerDefinition.h"
#include "Definitions/RicItemDefinition.h"
#include "Instances/RicContainerBase.h"
AInventoryOwner::AInventoryOwner()
{
bReplicates = true;
Inventory = CreateDefaultSubobject<URicContainerManagerComponent>(
TEXT("Inventory"));
}
void AInventoryOwner::BeginPlay()
{
Super::BeginPlay();
if (!HasAuthority() || !MainContainerDefinition ||
!StarterItemDefinition)
{
return;
}
Inventory->SetID(FName(TEXT("ExampleInventory")));
URicContainerBase* Main =
Inventory->AddContainerFromDefinition(MainContainerDefinition);
if (!Main)
{
return;
}
int32 Added = 0;
Main->AddItemDefinition(StarterItemDefinition, 12, Added);
UE_LOG(LogTemp, Log, TEXT("Starter inventory: %d of 12 added"), Added);
}Create a Blueprint child, assign the two assets, and place one instance in a level. Give each persistent owner a stable, unique manager ID in a real game.
4. Read the result
Run the level. With open cells and no rejecting filters, the grid contains one stack of 12.
Read Inventory->GetContainers(), then Main->GetItems(). Items expose GetDefinition(), GetID(), and GetNum(). Always use the actual number added: capacity and filters can cause partial placement.
5. Connect change events
Use OnContainersChanged to rebuild the container list and OnSlotsChanged to refresh contents. A dynamic handler must have UFUNCTION() and the matching signature:
cpp
UFUNCTION()
void HandleSlotsChanged(
URicContainerBase* Container, const TArray<int32>& Indexes);Bind in your actual widget or controller class:
cpp
Inventory->OnSlotsChanged.AddUniqueDynamic(
this, &UYourInventoryWidget::HandleSlotsChanged);Replace UYourInventoryWidget with your class. Remove the binding when the UI stops observing the inventory. See UI integration.
6. Add player operations
For an owning player's inventory, add these components on the same actor:
cpp
#include "Components/RicItemOperationsRPCsComponent.h"
#include "Components/RicClientPredictedInventoryComponent.h"
// In the actor constructor:
CreateDefaultSubobject<URicItemOperationsRPCsComponent>(
TEXT("InventoryRPCs"));
CreateDefaultSubobject<URicClientPredictedInventoryComponent>(
TEXT("PredictedInventory"));The actor needs the player's owning connection for server RPCs. A placed actor does not acquire that connection simply by replicating.
Build the local UI from prediction's container copies and submit operations through the predicted component. Continue with Networking.