Skip to content

MagpieInventoryContents

Dragonite edited this page Feb 16, 2021 · 2 revisions

In addition to the basic Get() method, Magpie contains a number of methods for fetching lists of contents with various sorting types.

Item Stacks

Many of the methods on this page return an Item Stack. Item stacks are a struct which contain two pieces of information:

Property Type Description
item * The item which this stack represents
quantity number The number of items represented by this stack

MagpieInventory::Get(item)

Yes, I'm documenting this on both pages.

Returns: ItemStack or undefined

Parameter Type Description
item * The item to look up in the inventory

Retrieve information about the item from the inventory. If the item exists in the inventory, it will return an ItemStack. (Stacking mode will be disregarded).

MagpieInventory::PocketContentsByQuantity(pocket*)

Returns: Array

Parameter Type Description
pocket * The pocket to fetch the contents of (optional, defaults to undefined)

Returns an array containing the contents of a specified pocket. If no pocket is specified, it will return the contents of the entire inventory instead. The returned array will contain ItemStacks representing the type and quantity of each item.

The returned array will be sorted by item quantity.

MagpieInventory::PocketContentsByAlphabetical(pocket*)

Returns: Array

Parameter Type Description
pocket * The pocket to fetch the contents of (optional, defaults to undefined)

Returns an array containing the contents of a specified pocket. If no pocket is specified, it will return the contents of the entire inventory instead. The returned array will contain ItemStacks representing the type and quantity of each item.

Usage of this method requires some setup. Items must be either a struct instance or a GameMaker object instance with a name variable containing a string, as this is what Magpie will look for to sort the list alphabetically. By default, calling this method will crash the game; reading the error message and acting accordingly is the barrier for entry for using this (otherwise free) library.

If used correctly, the returned array will be sorted by item name.

MagpieInventory::PocketContentsByGeneric(pocket*, sort_function*)

Returns: Array

Parameter Type Description
pocket * The pocket to fetch the contents of (optional, defaults to undefined)
sort_function * A function to sort the list by; see below (optional, ignored if not provided)

Returns an array containing the contents of a specified pocket. If no pocket is specified, it will return the contents of the entire inventory instead. The returned array will contain ItemStacks representing the type and quantity of each item.

You may specify your own sorting function. Sorting functions should take two ItemStacks as parameters, and return a -1, 0, or +1 depending on which value is to be listed first. For example, this is the sorting function used by the ByQuantity method:

function(a, b) {
    return a.quantity > b.quantity;
}

If no sorting function is provided to the method, the array will be stored in a random order.

MagpieInventory::GetPockets(sort_function*)

Returns: Array<*>

Parameter Type Description
sort_function * A function to sort the list by; see below (optional, ignored if not provided)

Returns an array containing every item pocket in the inventory.

You may specify your own sorting function. Sorting functions should take two item pockets as parameters (those being whatever you used when you added an item to one), and return a -1, 0, or +1 depending on which value is to be listed first. For example, a common general sorting function might be:

function(a, b) {
    return a > b;
}

If no sorting function is provided to the method, the array will be stored in a random order.

Clone this wiki locally