Skip to content

inventory

inventory

The inventory module includes the Inventory class which backs the inventory attribute of a PlayerCharacter.

Inventory

Inventory(player_character_owner: PlayerCharacter)

A container to hold and manage items owned by a PlayerCharacter.

You should not create an Inventory directly. When you initialize a PlayerCharacter, an inventory is created as a property of the PC. You can then add and remove items to and from the inventory using methods on the PlayerCharacter.

Attributes:

  • item_dict (defaultdict[ItemType, List[Item]]) –

    List of items in the inventory.

  • owner (PlayerCharacter) –

    Owner of the inventory.

Example:

>>> pc = PlayerCharacter()
>>> inv = Inventory(pc)

Parameters:

  • player_character_owner (PlayerCharacter) –

    The player character that owns the inventory.

all_items property

all_items: List[Item]

Gets all items stored in the items defaultdict inventory property.

Returns:

  • List[Item]

    List of all items in the inventory.

armor property

armor: List[Armor]

Gets all armor items stored in the items defaultdict inventory property.

Returns:

  • List[Armor]

    List of armor items. Returns an empty list if no armor items are present.

equipment property

equipment: List[Item]

Gets all equipment items stored in the items defaultdict inventory property.

Returns:

  • List[Item]

    List of equipment items. Returns an empty list if no equipment items are present.

equipped_items property

equipped_items: List[Item]

Get all equipped items in the inventory.

Returns:

  • List[Item]

    List of equipped items. Returns an empty list if no equipped items are present.

magic_items property

magic_items: List[Item]

Gets all magic items stored in the items defaultdict inventory property.

Returns:

  • List[Item]

    List of magic items. Returns an empty list if no magic items are present.

misc_items property

misc_items: List[Item]

Gets all miscellaneous items stored in the items defaultdict inventory property.

Miscellaneous items include items that are not armor, weapons, spells, equipment, or magic items.

Returns:

  • List[Item]

    List of miscellaneous items. Returns an empty list if no miscellaneous items are present.

spells property

spells: List[Spell]

Gets all spell items stored in the items defaultdict inventory property.

Returns:

  • List[Spell]

    List of spell items. Returns an empty list if no spell items are present.

weapons property

weapons: List[Weapon]

Gets all weapon items stored in the items defaultdict inventory property.

Returns:

  • List[Weapon]

    List of weapon items. Returns an empty list if no weapon items are present.

add_item

add_item(item: Item)

Add an item to the inventory and sets its owner.

Parameters:

  • item (Item) –

    Item to add.

drop_all_items

drop_all_items() -> List[Item]

Remove all items from the inventory and return a collection of the items that were removed.

Equipped items are unequipped prior to being removed.

Returns:

  • List[Item]

    List of all items that were removed.

equip_item

equip_item(item: Item) -> bool

Equips an item if it can be equipped.

Parameters:

  • item (Item) –

    Item to equip.

Returns:

  • bool ( bool ) –

    Whether the item was successfully equipped.

Raises:

from_dict classmethod

from_dict(inventory_dict: dict, player_character_owner: PlayerCharacter) -> Inventory

Deserializes a dictionary representation of an Inventory object. Typically done after getting the dictionary from persistent storage.

Parameters:

  • inventory_dict (dict) –

    Dictionary representation of the inventory.

  • player_character_owner (PlayerCharacter) –

    The player character that owns the inventory.

get_equipped_weapon

get_equipped_weapon() -> Weapon

Gets the first equipped weapon in the inventory.

Returns:

  • Weapon ( Weapon ) –

    The equipped weapon. Returns "Fists" (1 HP damage) if no other weapon is equipped.

get_item

get_item(item: Item) -> Item

Gets an item from the inventory.

Parameters:

  • item (Item) –

    Item to get from the inventory.

Returns:

  • Item ( Item ) –

    The item if it exists in the inventory, otherwise an Exception is thrown.

Raises:

  • Exception

    If the item does not exist in the inventory.

remove_item

remove_item(item: Item) -> bool

Removes an item from the inventory and resets its owner to None.

Parameters:

  • item (Item) –

    Item to remove.

Returns:

  • bool ( bool ) –

    Whether the item was successfully removed from the inventory.

Raises:

  • Exception

    If the item is currently equipped.

to_dict

to_dict() -> dict

Serializes the Inventory to a dictionary, typically in preparation for writing it to persistent storage in a downstream operation.

unequip_item

unequip_item(item: Item) -> bool

Unequips an item if it is currently equipped.

Parameters:

  • item (Item) –

    Item to unequip.

Returns:

  • bool ( bool ) –

    Whether the item was successfully unequipped.

Raises:

  • Exception

    If the item is not currently equipped.