Inventory Engine  v3.8
Public Types | Public Member Functions | Static Public Member Functions | Public Attributes | Static Public Attributes | Protected Member Functions | Static Protected Member Functions | Protected Attributes | Properties | List of all members
MoreMountains.InventoryEngine.Inventory Class Reference

Base inventory class. More...

Inherits MonoBehaviour, MoreMountains.Tools.MMEventListener< MMInventoryEvent >, and MoreMountains.Tools.MMEventListener< MMGameEvent >.

Public Types

enum  InventoryTypes { InventoryTypes.Main, InventoryTypes.Equipment }
 The different possible inventory types, main are regular, equipment will have special behaviours (use them for slots where you put the equipped weapon/armor/etc). More...
 

Public Member Functions

int NumberOfStackableSlots (string searchedItemID, int maxStackSize)
 
virtual void SetOwner (GameObject newOwner)
 Sets the owner of this inventory, useful to apply the effect of an item for example. More...
 
virtual bool AddItem (InventoryItem itemToAdd, int quantity)
 Tries to add an item of the specified type. More...
 
virtual bool AddItemAt (InventoryItem itemToAdd, int quantity, int destinationIndex)
 Adds the specified quantity of the specified item to the inventory, at the destination index of choice More...
 
virtual bool MoveItem (int startIndex, int endIndex)
 Tries to move the item at the first parameter slot to the second slot More...
 
virtual bool MoveItemToInventory (int startIndex, Inventory targetInventory, int endIndex=-1)
 This method lets you move the item at startIndex to the chosen targetInventory, at an optional endIndex there More...
 
virtual bool RemoveItem (int i, int quantity)
 Removes the specified item from the inventory. More...
 
virtual bool RemoveItemByID (string itemID, int quantity)
 Removes the specified quantity of the item matching the specified itemID More...
 
virtual bool DestroyItem (int i)
 Destroys the item stored at index i More...
 
virtual void EmptyInventory ()
 Empties the current state of the inventory. More...
 
virtual int CapMaxQuantity (InventoryItem itemToAdd, int newQuantity)
 Returns the max value of a specific item that can be added to this inventory without exceeding the max quantity defined on the item More...
 
virtual void ResizeArray (int newSize)
 Resizes the array to the specified new size More...
 
virtual int GetQuantity (string searchedItemID)
 Returns the total quantity of items matching the specified name More...
 
virtual List< int > InventoryContains (string searchedItemID)
 Returns a list of all the items in the inventory that match the specified name More...
 
virtual List< int > InventoryContains (MoreMountains.InventoryEngine.ItemClasses searchedClass)
 Returns a list of all the items in the inventory that match the specified class More...
 
virtual void SaveInventory ()
 Saves the inventory to a file More...
 
virtual void LoadSavedInventory ()
 Tries to load the inventory if a file is present More...
 
virtual void ResetSavedInventory ()
 Destroys any save file More...
 
virtual bool UseItem (InventoryItem item, int index, InventorySlot slot=null)
 Triggers the use and potential consumption of the item passed in parameter. More...
 
virtual bool UseItem (string itemName)
 Triggers the use of an item, as specified by its name. More...
 
virtual void EquipItem (InventoryItem item, int index, InventorySlot slot=null)
 Equips the item at the specified slot More...
 
virtual void DropItem (InventoryItem item, int index, InventorySlot slot=null)
 Drops the item, removing it from the inventory and potentially spawning an item on the ground near the character More...
 
virtual void DestroyItem (InventoryItem item, int index, InventorySlot slot=null)
 
virtual void UnEquipItem (InventoryItem item, int index, InventorySlot slot=null)
 
virtual void OnMMEvent (MMInventoryEvent inventoryEvent)
 Catches inventory events and acts on them More...
 
virtual void OnMMEvent (MMGameEvent gameEvent)
 When we catch an MMGameEvent, we do stuff based on its name More...
 
- Public Member Functions inherited from MoreMountains.Tools.MMEventListener< MMInventoryEvent >
void OnMMEvent (T eventType)
 
- Public Member Functions inherited from MoreMountains.Tools.MMEventListener< MMGameEvent >
void OnMMEvent (T eventType)
 

Static Public Member Functions

static Inventory FindInventory (string inventoryName, string playerID)
 Returns (if found) an inventory matching the searched name and playerID More...
 

Public Attributes

string PlayerID = "Player1"
 a unique ID used to identify the owner of this inventory More...
 
InventoryItem[] Content
 the complete list of inventory items in this inventory More...
 
InventoryTypes InventoryType = InventoryTypes.Main
 whether this inventory is a main inventory or equipment one More...
 
Transform TargetTransform
 the transform at which objects will be spawned when dropped More...
 
bool Persistent = true
 whether this inventory will be saved and loaded More...
 
bool ResetThisInventorySaveOnStart = false
 whether or not this inventory should be reset on start More...
 
bool DrawContentInInspector = false
 If true, will draw the contents of the inventory in its inspector. More...
 
virtual int NumberOfFreeSlots => Content.Length - NumberOfFilledSlots
 The number of free slots in this inventory. More...
 
virtual bool IsFull => NumberOfFreeSlots <= 0
 whether or not the inventory is full (doesn't have any remaining free slots) More...
 

Static Public Attributes

static List< InventoryRegisteredInventories
 
static string _resourceItemPath = "Items/"
 
static string _saveFolderName = "InventoryEngine/"
 
static string _saveFileExtension = ".inventory"
 

Protected Member Functions

virtual void Awake ()
 On Awake we register this inventory More...
 
virtual void RegisterInventory ()
 Registers this inventory so other scripts can access it later on More...
 
virtual bool AddItemToArray (InventoryItem itemToAdd, int quantity)
 Adds the item to content array. More...
 
virtual bool RemoveItemFromArray (int i)
 Removes the item at index i from the array. More...
 
virtual void FillSerializedInventory (SerializedInventory serializedInventory)
 Fills the serialized inventory for storage More...
 
virtual void ExtractSerializedInventory (SerializedInventory serializedInventory)
 Extracts the serialized inventory from a file content More...
 
virtual string DetermineSaveName ()
 
virtual void OnEnable ()
 On enable, we start listening for MMGameEvents. More...
 
virtual void OnDisable ()
 On disable, we stop listening for MMGameEvents. More...
 

Static Protected Member Functions

static void InitializeStatics ()
 

Protected Attributes

InventoryItem _loadedInventoryItem
 

Properties

virtual GameObject Owner [get, set]
 the owner of the inventory (for games where you have multiple characters) More...
 
int NumberOfFilledSlots [get]
 The number of filled slots. More...
 

Detailed Description

Base inventory class.

Will handle storing items, saving and loading its content, adding items to it, removing items, equipping them, etc.

Member Enumeration Documentation

◆ InventoryTypes

The different possible inventory types, main are regular, equipment will have special behaviours (use them for slots where you put the equipped weapon/armor/etc).

Enumerator
Main 
Equipment 

Member Function Documentation

◆ AddItem()

virtual bool MoreMountains.InventoryEngine.Inventory.AddItem ( InventoryItem  itemToAdd,
int  quantity 
)
virtual

Tries to add an item of the specified type.

Note that this is name based.

Returns
true, if item was added, false if it couldn't be added (item null, inventory full).
Parameters
itemToAddItem to add.

◆ AddItemAt()

virtual bool MoreMountains.InventoryEngine.Inventory.AddItemAt ( InventoryItem  itemToAdd,
int  quantity,
int  destinationIndex 
)
virtual

Adds the specified quantity of the specified item to the inventory, at the destination index of choice

Parameters
itemToAdd
quantity
destinationIndex
Returns

◆ AddItemToArray()

virtual bool MoreMountains.InventoryEngine.Inventory.AddItemToArray ( InventoryItem  itemToAdd,
int  quantity 
)
protectedvirtual

Adds the item to content array.

Returns
true, if item to array was added, false otherwise.
Parameters
itemToAddItem to add.
quantityQuantity.

◆ Awake()

virtual void MoreMountains.InventoryEngine.Inventory.Awake ( )
protectedvirtual

On Awake we register this inventory

◆ CapMaxQuantity()

virtual int MoreMountains.InventoryEngine.Inventory.CapMaxQuantity ( InventoryItem  itemToAdd,
int  newQuantity 
)
virtual

Returns the max value of a specific item that can be added to this inventory without exceeding the max quantity defined on the item

Parameters
itemToAdd
newQuantity
Returns

◆ DestroyItem() [1/2]

virtual bool MoreMountains.InventoryEngine.Inventory.DestroyItem ( int  i)
virtual

Destroys the item stored at index i

Returns
true, if item was destroyed, false otherwise.
Parameters
iThe index.

◆ DestroyItem() [2/2]

virtual void MoreMountains.InventoryEngine.Inventory.DestroyItem ( InventoryItem  item,
int  index,
InventorySlot  slot = null 
)
virtual

◆ DetermineSaveName()

virtual string MoreMountains.InventoryEngine.Inventory.DetermineSaveName ( )
protectedvirtual

◆ DropItem()

virtual void MoreMountains.InventoryEngine.Inventory.DropItem ( InventoryItem  item,
int  index,
InventorySlot  slot = null 
)
virtual

Drops the item, removing it from the inventory and potentially spawning an item on the ground near the character

Parameters
itemItem.
indexIndex.
slotSlot.

◆ EmptyInventory()

virtual void MoreMountains.InventoryEngine.Inventory.EmptyInventory ( )
virtual

Empties the current state of the inventory.

◆ EquipItem()

virtual void MoreMountains.InventoryEngine.Inventory.EquipItem ( InventoryItem  item,
int  index,
InventorySlot  slot = null 
)
virtual

Equips the item at the specified slot

Parameters
itemItem.
indexIndex.
slotSlot.

◆ ExtractSerializedInventory()

virtual void MoreMountains.InventoryEngine.Inventory.ExtractSerializedInventory ( SerializedInventory  serializedInventory)
protectedvirtual

Extracts the serialized inventory from a file content

Parameters
serializedInventorySerialized inventory.

◆ FillSerializedInventory()

virtual void MoreMountains.InventoryEngine.Inventory.FillSerializedInventory ( SerializedInventory  serializedInventory)
protectedvirtual

Fills the serialized inventory for storage

Parameters
serializedInventorySerialized inventory.

◆ FindInventory()

static Inventory MoreMountains.InventoryEngine.Inventory.FindInventory ( string  inventoryName,
string  playerID 
)
static

Returns (if found) an inventory matching the searched name and playerID

Parameters
inventoryName
playerID
Returns

◆ GetQuantity()

virtual int MoreMountains.InventoryEngine.Inventory.GetQuantity ( string  searchedItemID)
virtual

Returns the total quantity of items matching the specified name

Returns
The quantity.
Parameters
searchedItemSearched item.

◆ InitializeStatics()

static void MoreMountains.InventoryEngine.Inventory.InitializeStatics ( )
staticprotected

◆ InventoryContains() [1/2]

virtual List<int> MoreMountains.InventoryEngine.Inventory.InventoryContains ( MoreMountains.InventoryEngine.ItemClasses  searchedClass)
virtual

Returns a list of all the items in the inventory that match the specified class

Returns
A list of item matching the search criteria.
Parameters
searchedTypeThe searched type.

◆ InventoryContains() [2/2]

virtual List<int> MoreMountains.InventoryEngine.Inventory.InventoryContains ( string  searchedItemID)
virtual

Returns a list of all the items in the inventory that match the specified name

Returns
A list of item matching the search criteria.
Parameters
searchedTypeThe searched type.

◆ LoadSavedInventory()

virtual void MoreMountains.InventoryEngine.Inventory.LoadSavedInventory ( )
virtual

Tries to load the inventory if a file is present

◆ MoveItem()

virtual bool MoreMountains.InventoryEngine.Inventory.MoveItem ( int  startIndex,
int  endIndex 
)
virtual

Tries to move the item at the first parameter slot to the second slot

Returns
true, if item was moved, false otherwise.
Parameters
startIndexStart index.
endIndexEnd index.

◆ MoveItemToInventory()

virtual bool MoreMountains.InventoryEngine.Inventory.MoveItemToInventory ( int  startIndex,
Inventory  targetInventory,
int  endIndex = -1 
)
virtual

This method lets you move the item at startIndex to the chosen targetInventory, at an optional endIndex there

Parameters
startIndex
targetInventory
endIndex
Returns

◆ NumberOfStackableSlots()

int MoreMountains.InventoryEngine.Inventory.NumberOfStackableSlots ( string  searchedItemID,
int  maxStackSize 
)

◆ OnDisable()

virtual void MoreMountains.InventoryEngine.Inventory.OnDisable ( )
protectedvirtual

On disable, we stop listening for MMGameEvents.

You may want to extend that to stop listening to other types of events.

◆ OnEnable()

virtual void MoreMountains.InventoryEngine.Inventory.OnEnable ( )
protectedvirtual

On enable, we start listening for MMGameEvents.

You may want to extend that to listen to other types of events.

◆ OnMMEvent() [1/2]

virtual void MoreMountains.InventoryEngine.Inventory.OnMMEvent ( MMGameEvent  gameEvent)
virtual

When we catch an MMGameEvent, we do stuff based on its name

Parameters
gameEventGame event.

◆ OnMMEvent() [2/2]

virtual void MoreMountains.InventoryEngine.Inventory.OnMMEvent ( MMInventoryEvent  inventoryEvent)
virtual

Catches inventory events and acts on them

Parameters
inventoryEventInventory event.

◆ RegisterInventory()

virtual void MoreMountains.InventoryEngine.Inventory.RegisterInventory ( )
protectedvirtual

Registers this inventory so other scripts can access it later on

◆ RemoveItem()

virtual bool MoreMountains.InventoryEngine.Inventory.RemoveItem ( int  i,
int  quantity 
)
virtual

Removes the specified item from the inventory.

Returns
true, if item was removed, false otherwise.
Parameters
itemToRemoveItem to remove.

◆ RemoveItemByID()

virtual bool MoreMountains.InventoryEngine.Inventory.RemoveItemByID ( string  itemID,
int  quantity 
)
virtual

Removes the specified quantity of the item matching the specified itemID

Parameters
itemID
quantity
Returns

◆ RemoveItemFromArray()

virtual bool MoreMountains.InventoryEngine.Inventory.RemoveItemFromArray ( int  i)
protectedvirtual

Removes the item at index i from the array.

Returns
true, if item from array was removed, false otherwise.
Parameters
iThe index.

◆ ResetSavedInventory()

virtual void MoreMountains.InventoryEngine.Inventory.ResetSavedInventory ( )
virtual

Destroys any save file

◆ ResizeArray()

virtual void MoreMountains.InventoryEngine.Inventory.ResizeArray ( int  newSize)
virtual

Resizes the array to the specified new size

Parameters
newSizeNew size.

◆ SaveInventory()

virtual void MoreMountains.InventoryEngine.Inventory.SaveInventory ( )
virtual

Saves the inventory to a file

◆ SetOwner()

virtual void MoreMountains.InventoryEngine.Inventory.SetOwner ( GameObject  newOwner)
virtual

Sets the owner of this inventory, useful to apply the effect of an item for example.

Parameters
newOwnerNew owner.

◆ UnEquipItem()

virtual void MoreMountains.InventoryEngine.Inventory.UnEquipItem ( InventoryItem  item,
int  index,
InventorySlot  slot = null 
)
virtual

◆ UseItem() [1/2]

virtual bool MoreMountains.InventoryEngine.Inventory.UseItem ( InventoryItem  item,
int  index,
InventorySlot  slot = null 
)
virtual

Triggers the use and potential consumption of the item passed in parameter.

You can also specify the item's slot (optional) and index.

Parameters
itemItem.
slotSlot.
indexIndex.

◆ UseItem() [2/2]

virtual bool MoreMountains.InventoryEngine.Inventory.UseItem ( string  itemName)
virtual

Triggers the use of an item, as specified by its name.

Prefer this signature over the previous one if you don't particularly care what slot the item will be taken from in case of duplicates.

Parameters
itemName
Returns

Member Data Documentation

◆ _loadedInventoryItem

InventoryItem MoreMountains.InventoryEngine.Inventory._loadedInventoryItem
protected

◆ _resourceItemPath

string MoreMountains.InventoryEngine.Inventory._resourceItemPath = "Items/"
static

◆ _saveFileExtension

string MoreMountains.InventoryEngine.Inventory._saveFileExtension = ".inventory"
static

◆ _saveFolderName

string MoreMountains.InventoryEngine.Inventory._saveFolderName = "InventoryEngine/"
static

◆ Content

InventoryItem [] MoreMountains.InventoryEngine.Inventory.Content

the complete list of inventory items in this inventory

◆ DrawContentInInspector

bool MoreMountains.InventoryEngine.Inventory.DrawContentInInspector = false

If true, will draw the contents of the inventory in its inspector.

◆ InventoryType

InventoryTypes MoreMountains.InventoryEngine.Inventory.InventoryType = InventoryTypes.Main

whether this inventory is a main inventory or equipment one

◆ IsFull

virtual bool MoreMountains.InventoryEngine.Inventory.IsFull => NumberOfFreeSlots <= 0

whether or not the inventory is full (doesn't have any remaining free slots)

◆ NumberOfFreeSlots

virtual int MoreMountains.InventoryEngine.Inventory.NumberOfFreeSlots => Content.Length - NumberOfFilledSlots

The number of free slots in this inventory.

◆ Persistent

bool MoreMountains.InventoryEngine.Inventory.Persistent = true

whether this inventory will be saved and loaded

◆ PlayerID

string MoreMountains.InventoryEngine.Inventory.PlayerID = "Player1"

a unique ID used to identify the owner of this inventory

◆ RegisteredInventories

List<Inventory> MoreMountains.InventoryEngine.Inventory.RegisteredInventories
static

◆ ResetThisInventorySaveOnStart

bool MoreMountains.InventoryEngine.Inventory.ResetThisInventorySaveOnStart = false

whether or not this inventory should be reset on start

◆ TargetTransform

Transform MoreMountains.InventoryEngine.Inventory.TargetTransform

the transform at which objects will be spawned when dropped

Property Documentation

◆ NumberOfFilledSlots

int MoreMountains.InventoryEngine.Inventory.NumberOfFilledSlots
get

The number of filled slots.

◆ Owner

virtual GameObject MoreMountains.InventoryEngine.Inventory.Owner
getset

the owner of the inventory (for games where you have multiple characters)


The documentation for this class was generated from the following file: