Inventory

The inventory class represents an inventory of items, it is not linked to a specific game object however a game object can be linked to an inventory. This allows for any character or item to hold an inventory. An inventory has a size and as such the maximum number of items that can be stored will be limited by the size of each item.
Return to Index
new ( integer width , integer height )
__gc ( )
__eq ( am.inventory rhs ) -> ( boolean )
size ( ) -> ( integer , integer )
has_space_for ( am.item item , integer x , integer y ) -> ( boolean )
add_item ( am.item item ) -> ( boolean )
add_item ( am.item item , integer x , integer y ) -> ( boolean )
remove_item ( am.item item ) -> ( boolean )
remove_all ( ) -> ( boolean )
has_item ( am.item item ) -> ( boolean )
item_at ( integer locationX , integer locationY ) -> ( am.item )
spots ( ) -> ( Array )
new ( integer width , integer height )
Creates a new inventory instance.
Parameters:
integer width : The width of the inventory in item size units.
integer height : The height of the inventory in item size units.
__gc ( )
Releases the reference counter on this inventory.
__eq ( am.inventory rhs ) -> ( boolean )
Compares this inventory with another inventory object. Will not return true for equivalent inventories.
Parameters:
am.inventory rhs : The other inventory to compare with.
Returns:
boolean : True if the inventories are the same object.
size ( ) -> ( integer , integer )
Returns the width and height of this inventory.
Returns:
integer : The width of the inventory.
integer : The height of the inventory.
has_space_for ( am.item item , integer x , integer y ) -> ( boolean )
Returns true if there is enough space to place the given item at the given location. False indicates that either the location will put the item outside of the bounds of the inventory or that there is another item blocking this items placement.
Parameters:
am.item item : The item to place.
integer x : The x location to check at
integer y : The y location to check at
Returns:
boolean : True if there is space at the given location for the given item. False otherwise if there isn't any space or the location is invalid.
add_item ( am.item item ) -> ( boolean )
Attempts to add an item to the inventory, returns true if a spot was found the item, false if the item was nil or if no spot could be found.
Parameters:
am.item item : The item to add to the inventory.
Returns:
boolean : True if the item was added to the inventory successfully.
add_item ( am.item item , integer x , integer y ) -> ( boolean )
Attempts to add an item to the inventory at the given location, returns true if the space required for the item was available.
Parameters:
am.item item : The item to add to the inventory.
integer x : The x location to add the item at.
integer y : The y location to add the item at.
Returns:
boolean : True if the item was added to the inventory successfully.
remove_item ( am.item item ) -> ( boolean )
Removes the given item from the inventory, returns true if the item was found in the inventory and removed.
Parameters:
am.item item : The item to remove from the inventory.
Returns:
boolean : True if the item was removed.
remove_all ( ) -> ( boolean )
Removes all items from the inventory.
Returns:
boolean : True if all items were successfully removed, false is there was an error.
has_item ( am.item item ) -> ( boolean )
Looks for the given item in the inventory. Returns true if the given item was found.
Parameters:
am.item item : The item to look for in the inventory.
Returns:
boolean : True if the item was found.
item_at ( integer locationX , integer locationY ) -> ( am.item )
Returns the item at the given location, nil if there is no item at that location. Items do not overlap so there cannot be multiple items returned.
Parameters:
integer locationX : The x location to look at.
integer locationY : The y location to look at.
Returns:
am.item : The found item, or nil if it was nothing was found.
spots ( ) -> ( Array )
Returns an array of tables which represent all the items in the inventory. Each array element contains the item, and x and y locations.
 inv = am.inventory.new(3, 3)
 scroll1 = am.item.new()
 scroll1:inventory_size(2, 1)
 scroll2 = scroll1:clone()
 scroll1:name("Scroll 1")
 scroll2:name("Scroll 2")
 inv.add_item(scroll1)
 inv.add_item(scroll2)
 spots = inv:spots()
 for key, value in pairs(spots) do
     am_log(key .. ": " .. value.item:name() .. " at " .. value.x .. ", " .. value.y)
 end
 
 Outputs:
 2: Scroll 2 at 0, 1
 1: Scroll 1 at 0, 0
Returns:
Array : Array of all the items in the inventory and their locations.