Item Stack

Scripting with the ItemStack class.

Representation of a stack of items. This is the main object used for storing items in the inventory.

Fields & Properties

Name
Description
Type
Access

item

The item.

Item

public

amount

The number of duplicate items in this stack.

int

public

placement

The placement of this item stack in the inventory.

ItemStack.Placement

public

enchants

A list of enchantments applied to this item stack. Stackable item types cannot be enchanted.

List<Enchantment>

public

BuyingPrice

The buying price of this item stack.

Currency

public

SellingPrice

The selling price of this item stack.

Currency

public

IsEmpty

If this item stack has no items.

bool

public

IsFull

If this item stack has reached the max capacity.

bool

public

IsEquipped

If this item stack is currently equipped.

bool

public

IsKeyItem

If this is a key item.

bool

public

Creating Item Stacks

Constructor

To create an ItemStack, you need a reference to an Item.

// Create an item stack with a stack of 10
ItemStack myItemStack = new ItemStack(myItem, 10);

Copy

You can create a copy of an ItemStack with the CreateCopy method.

ItemStack myItemStackCopy = myItemStack.CreateCopy();

Managing Amounts

When managing item amounts, it's recommended to use the methods below instead of just editing the amount field.

Add

The Add method increases the item stack's amount. It returns an int that represents the excess amount (the amount not added due to the item stack becoming full).

// Add 10 to the stack
int excess = myItemStack.Add(10);

Remove

The Remove method decreases the item stack's amount. It returns an int that represents the excess amount (the amount not removed due to the item stack becoming empty).

// Remove 10 to the stack
int excess = myItemStack.Remove(10);

Split

The Split method does 3 things: creates a copy of an ItemStack, gives half of the item to the copy, and returns the copy. So, if you have an ItemStack with it's amount equally to 20, using Split on it will cut the amount down to 10.

ItemStack mySplitStack = myItemStack.Split();

Combine

The Combine method takes the amount from another ItemStack as much as possible. This may result in the other item stack becoming empty. The method returns true if any amount of taken from the other item stack, otherwise, false will be returned.

bool result = myItemStack.Combine(myOtherItemStack);

Operators

ItemStack's support some operators that you can use to change the amount field. The operators will clamp the amount within 0 and the max item stack amount.

// Add 1 to the stack
myItemStack += 1;

// Remove 1 from the stack
myItemStack -= 1;

// Double the stack
myItemStack *= 2;

// Halve the stack
myItemStack /= 2;

Enchanting

Enchanting item stacks can be handled with the ItemStack class itself, as long as you have a reference to an enchanter ItemStack. Use the TryEnchant method for this purpose.

TryEnchant returns true if the enchant was successful. The enchant may fail if the item types were incorrect. Only enchanter items can enchant enchantable items. Learn more about enchanting here.

You can choose to apply the enchanting (currency) cost by passing true as the second parameter.

myEnchantableItemStack.TryEnchant(myEnchanterItemStack, true);

Action Menu Options

By default, item stacks' UI counterpart logically display item options for the Action Menu. You can override these options by editing the ItemStack.actionMenuOptionsGetter field.

Here's an example of how you can override these options:

ItemStack.actionMenuOptionsGetter = new(x =>
{
    List<ActionMenuOption> options = new();

    options.Add(new ActionMenuOption()
    {
        // Smallcaps is generally used by Inventool for styling purposes but it is not required
        text = $"<smallcaps>Some Action</smallcaps>",
        backgroundColor = new Color(0.3f, 0.3f, 0.3f),
        sortingOrder = 0,
        action = () => { Debug.Log("Do something"); }
    });

    return options;
});

You can call the GetActionMenuOptions method to get the options.

var options = myItemStack.GetActionMenuOptions();

Last updated