NsBackpack gives an actor an inventory of slots. Each slot holds an item, a quantity, and optional data for that one item. An item is a data asset with a name, an icon, a mesh, a stack size, and fragments that your game makes. The NsBackpackGrid module puts the slots in a grid, where an item covers the cells of its shape.
NsBackpack has no UI, no selected slot, no item use, no crafting, and no save system. It has no server RPCs. Your game builds these parts on top of the backpack.
A slot index is a cell index, row by row from the top left. A move never swaps two items. The demo has no item data, so two stacks of the same item can always join.
NsBackpack folder into the Plugins folder of your project."NsBackpack" to the dependencies of your game module in Build.cs."NsBackpackGrid".The plugin has no project settings.
NsBackpackItemDefinition and name the asset DA_Bolt.DA_Bolt. Set Display Name to Bolt and Max Stack to 10.DA_Bolt and Quantity to 15.On Changed and print Get Slots.In C++:
#include "NsBackpackComponent.h"
// In the constructor of the character
Backpack = CreateDefaultSubobject<UNsBackpackComponent>(TEXT("Backpack"));
// On the server. The return value is the quantity that did not fit.
const int32 Left = Backpack->AddItem(BoltItem, 15);
Add Item with the item and the quantity.The items fill the stacks of the same item first. Then they go into empty slots, one full stack at a time.
Remove Item with the item and the quantity to remove the items from the first slots first.Remove From Slot with a slot index and a quantity to remove items from one slot.Both calls return false and remove nothing when the backpack, or the slot, holds fewer items.
Move Slot with the source slot and the target slot.The items join the target stack when it has room. The rest stays in the source slot. When the items cannot join, the two slots swap.
Get Slots for all slots, or Get Slot for one slot.Count Item for the total of one item.Find Item for the first slot with the item, or Find Empty Slot for the first empty slot. Both return -1 when nothing matches.On Changed on the backpack.On Changed fires on the server and on each client after the slots change.
NsBackpackItemFragment.Find Fragment with the class. In C++, call Item->FindFragment<UMyFragment>().Set Slot.A slot with data does not stack.
Can Accept to allow or block an item in a slot, for example a slot for tools only.Can Stack to allow or block a stack.The default Can Accept returns true. The default Can Stack needs the same item and no slot data.
Get Slots and store each slot in your save game.Reset Slots with the saved slot count.Set Slot for each saved slot.For a grid backpack, call Reset Grid, then Set Grid Slot with the saved rotation from Get Rotation.
A slot index is a cell index, row by row from the top left. An item sits in the slot of the top left cell of its box. An item without a Grid Shape fragment covers one cell. Add Item tries every cell with no rotation first, then with quarter turns.
Can Place with the item, the target cell, and the rotation to show a preview.Move Item with the source slot, the target cell, and the rotation.Rotate Item to turn an item one quarter turn clockwise on its cell.A rotation is a number of clockwise quarter turns, from 0 to 3. A grid move never swaps two items. A move onto a stack of the same item joins the stack.
Get Cell Owner with a cell index. It returns the slot of the item that covers the cell, or -1.Get Item Cells with a slot index for all cells of that item.Get Cell Index and Get Cell Position to convert between a cell index and a column and row.In a grid, an empty slot can be under another item. Use Get Cell Owner or Find Empty Slot to find a free cell, not Get Slot.
| Setting | Default | Effect |
|---|---|---|
| Slot Count | 8 | Number of slots when play starts. |
| Start Items | Empty | Items that the backpack gets when play starts. Each entry has an Item and a Quantity (default 1). |
| Setting | Default | Effect |
|---|---|---|
| Slot Count | 16 | Number of cells when play starts. |
| Grid Width | 4 | Number of columns. |
| Start Items | Empty | Items that the backpack gets when play starts. |
| Setting | Default | Effect |
|---|---|---|
| Display Name | Empty | Name for the UI. |
| Description | Empty | Text about the item for the UI. |
| Icon | None | Texture for the UI. |
| World Mesh | None | Static mesh for the item in the world. |
| Max Stack | 1 | Most items in one slot. The minimum is 1. |
| Fragments | Empty | Game data of the item. |
The plugin does not read Display Name, Description, Icon, or World Mesh. Your UI and game code read them.
| Setting | Default | Effect |
|---|---|---|
| Size | 1 x 1 | Width and height of the box of the shape, in cells. |
| Cells | Empty | Cells of the shape inside the box, from the top left. Empty fills the whole box. |
The plugin has no console commands, console variables, or log category.
Data validation on an item definition reports these messages:
Run data validation on the item asset from the Content Browser to see them.
| Problem | Cause | Fix |
|---|---|---|
Add Item returns the full quantity, and nothing changes. |
The call ran on a client. Only the server can change the slots. | Call it on the server, for example from a server RPC of your game. |
Add Item returns more than 0. |
No stack has room and no empty slot accepts the item. | Add slots with Reset Slots, or read the return value and drop the rest. |
Remove Item returns false. |
The backpack holds fewer items than the quantity. | Call Count Item first. |
| Two slots of the same item do not join. | A slot has Data, or Can Stack returns false. |
Clear the slot data, or check your Can Stack override. |
Move Slot returns false. |
The source slot is empty, the indices are the same, or Can Accept blocks the swap. |
Check the slots and your Can Accept override. |
| A grid item does not fit. | A cell of the shape is outside the grid or under another item. | Call Find Placement to get a free cell and rotation. |
| The last cells of a grid stay empty. | Slot Count is not a multiple of Grid Width. Only full rows count. | Set Slot Count to a multiple of Grid Width. |
| A change to Slot Count in play does nothing. | The backpack reads Slot Count only in BeginPlay. |
Call Reset Slots or Reset Grid. |
| A second fragment of one class has no effect. | Find Fragment returns the first match. |
Keep one fragment of each class. |
Reset Slots or Reset Grid call.Set Slot does not limit the quantity to Max Stack.