logo SERENITY
Game Systems Foundation
Search Results for

    Show / Hide Table of Contents

    Class InventorySection

    ISaveSection holding the player's inventory: a list of item entries, each with an id, a quantity, and an optional project-defined JSON escape hatch.

    Inheritance
    object
    InventorySection
    Implements
    ISaveSection
    Inherited Members
    object.ToString()
    object.Equals(object)
    object.Equals(object, object)
    object.ReferenceEquals(object, object)
    object.GetHashCode()
    object.GetType()
    Namespace: Serenity.GameSave.Domain.Sections
    Assembly: Serenity.GameSave.Domain.dll
    Syntax
    public sealed class InventorySection : ISaveSection

    Constructors

    InventorySection()

    Declaration
    public InventorySection()

    Fields

    KEY

    The section key this section is stored under.

    Declaration
    public const string KEY = "inventory"
    Field Value
    Type Description
    string

    Properties

    Entries

    Every inventory entry, in insertion order.

    Declaration
    public IReadOnlyList<InventorySection.InventoryEntry> Entries { get; }
    Property Value
    Type Description
    IReadOnlyList<InventorySection.InventoryEntry>

    SectionKey

    The stable key this section is stored under within a SaveDocument.

    Declaration
    public string SectionKey { get; }
    Property Value
    Type Description
    string

    Methods

    AddItem(string, int, string)

    Adds quantity of itemId. If the item is already present, its quantity accumulates and its customJson replaces the stored one when non-null; otherwise a new entry is appended.

    Declaration
    public void AddItem(string itemId, int quantity, string customJson = null)
    Parameters
    Type Name Description
    string itemId
    int quantity
    string customJson
    Exceptions
    Type Condition
    ArgumentException

    Thrown when itemId is null or empty.

    Deserialize(string)

    Restores every entry from payload. itemId and quantity are required per entry — a payload missing either is not a valid inventory entry and fails the whole load. customJson is treated as OPTIONAL (looked up via the item's property map rather than GetProperty(string)) so an older or hand-edited save written before this field existed still loads, defaulting the field to null instead of failing.

    Declaration
    public void Deserialize(string payload)
    Parameters
    Type Name Description
    string payload

    GetQuantity(string)

    Gets the quantity held of itemId, or zero when absent.

    Declaration
    public int GetQuantity(string itemId)
    Parameters
    Type Name Description
    string itemId
    Returns
    Type Description
    int

    HasItem(string)

    Checks whether at least one unit of itemId is held.

    Declaration
    public bool HasItem(string itemId)
    Parameters
    Type Name Description
    string itemId
    Returns
    Type Description
    bool

    RemoveItem(string, int)

    Removes quantity of itemId, dropping the entry entirely once its remaining quantity reaches zero or below. A missing item is a no-op.

    Declaration
    public void RemoveItem(string itemId, int quantity)
    Parameters
    Type Name Description
    string itemId
    int quantity

    Serialize()

    Serializes this section's current state to JSON.

    Declaration
    public string Serialize()
    Returns
    Type Description
    string

    Implements

    ISaveSection
    In this article
    © 2026 Serenity. All Rights Reserved