Inventory

class pygamelib.engine.Inventory(max_size=10, parent=None)

Bases: object

A class that represent the Player (or NPC) inventory.

This class is pretty straightforward: it is an object container, you can add, get and remove items and you can get a value from the objects in the inventory.

The constructor takes only one parameter: the maximum size of the inventory. Each BoardItem that is going to be put in the inventory has a size (default is 1), the total addition of all these size cannot exceed max_size.

Parameters:
  • max_size (int) – The maximum size of the inventory. Deafult value: 10.
  • parent – The parent object (usually a BoardItem).

Note

You can print() the inventory. This is mostly useful for debug as you want to have a better display in your game.

Warning

The Game engine and Player takes care to initiate an inventory for the player, you don’t need to do it.

__init__(max_size=10, parent=None)

Initialize self. See help(type(self)) for accurate signature.

Methods

__init__([max_size, parent]) Initialize self.
add_item(item) Add an item to the inventory.
delete_item(name) Delete the item corresponding to the name given in argument.
empty() Empty the inventory.
get_item(name) Return the item corresponding to the name given in argument.
items_name() Return the list of all items names in the inventory.
search(query) Search for objects in the inventory.
size() Return the cumulated size of the inventory.
value() Return the cumulated value of the inventory.
add_item(item)

Add an item to the inventory.

This method will add an item to the inventory unless:

  • it is not an instance of BoardItem,
  • you try to add an item that is not pickable,
  • there is no more space left in the inventory (i.e: the cumulated size of the inventory + your item.size is greater than the inventory max_size)
Parameters:item (BoardItem) – the item you want to add
Raises:PglInventoryException, PglInvalidTypeException

Example:

item = Treasure(model=Sprites.MONEY_BAG,size=2,name='Money bag')
try:
    mygame.player.inventory.add_item(item)
expect PglInventoryException as e:
    if e.error == 'not_enough_space':
        print(f"Impossible to add {item.name} to the inventory, there is no"
        "space left in it!")
        print(e.message)
    elif e.error == 'not_pickable':
        print(e.message)

Warning

if you try to add more than one item with the same name (or if the name is empty), this function will automatically change the name of the item by adding a UUID to it.

delete_item(name)

Delete the item corresponding to the name given in argument.

Parameters:name (str) – the name of the item you want to delete.

Note

in case an execpetion is raised, the error will be ‘no_item_by_that_name’ and the message is giving the specifics.

See also

pygamelib.base.PglInventoryException.

Example:

life_container = mygame.player.inventory.get_item('heart_1')
if isinstance(life_container,GenericActionableStructure):
    life_container.action(life_container.action_parameters)
    mygame.player.inventory.delete_item('heart_1')
empty()

Empty the inventory.

Example:

if inventory.size() > 0:
    inventory.empty()
get_item(name)

Return the item corresponding to the name given in argument.

Parameters:name (str) – the name of the item you want to get.
Returns:An item.
Return type:BoardItem
Raises:PglInventoryException

Note

in case an execpetion is raised, the error will be ‘no_item_by_that_name’ and the message is giving the specifics.

See also

pygamelib.base.PglInventoryException.

Example:

life_container = mygame.player.inventory.get_item('heart_1')
if isinstance(life_container,GenericActionableStructure):
    life_container.action(life_container.action_parameters)

Note

Please note that the item object reference is returned but nothing is changed in the inventory. The item hasn’t been removed.

items_name()

Return the list of all items names in the inventory.

Returns:a list of string representing the items names.
Return type:list
classmethod load(data)
search(query)

Search for objects in the inventory.

All objects that matches the query are going to be returned. :param query: the query that items in the inventory have to match to be returned :type name: str :returns: a table of BoardItems. :rtype: list

Example:

for item in game.player.inventory.search('mighty'):
    print(f"This is a mighty item: {item.name}")
serialize()

Serialize the inventory in a dictionary.

Returns:The serialized data.
Return type:dict

Example:

json.dump(my_inventory.serialize(), out_file)
size()

Return the cumulated size of the inventory. It can be used in the UI to display the size compared to max_size for example.

Returns:size of inventory
Return type:int

Example:

print(f"Inventory: {mygame.player.inventory.size()}/"
"{mygame.player.inventory.max_size}")
value()

Return the cumulated value of the inventory. It can be used for scoring for example.

Returns:value of inventory
Return type:int

Example:

if inventory.value() >= 10:
    print('Victory!')
    break