# Equipment Slot

## 🎒 EquipmentSlot Class

Data container for a single equipment slot.

> Namespace: `Esper.Inventool`
>
> Access: `public`
>
> Type: `class`

***

### 📦 Fields

| Access          | Field                     | Type                                          | Description                                          |
| --------------- | ------------------------- | --------------------------------------------- | ---------------------------------------------------- |
| `public`        | `positionIndex`           | `int`                                         | The index of the position to use.                    |
| `public`        | `name`                    | `string`                                      | The name of the slot. Used for searching purposes.   |
| `public`        | `itemStack`               | `ItemStack`                                   | The equipped item.                                   |
| `public`        | `backgroundIcon`          | `Sprite`                                      | The background icon of the slot.                     |
| `public`        | `backgroundColorTint`     | `Color`                                       | The background icon color tint.                      |
| `public`        | `itemType`                | `ItemType`                                    | The item type that is accepted by the slot.          |
| `public static` | `actionMenuOptionsGetter` | `Func<EquipmentSlot, List<ActionMenuOption>>` | A getter that returns a list of action menu options. |

***

### 🔍 Properties

| Access   | Property  | Type   | Description               |
| -------- | --------- | ------ | ------------------------- |
| `public` | `HasItem` | `bool` | If this slot has an item. |

***

### 🧰 Methods

| Access           | Method                                            | Returns                  | Description                                                                                                                                                                                         |
| ---------------- | ------------------------------------------------- | ------------------------ | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `public virtual` | `TryEquip(ItemStack itemStack)`                   | `bool`                   | Tries to equip an item. The item will not be equipped if this slot doesn't support the type of the item. Returns true if the item was successfully equipped. Otherwise, false.                      |
| `public virtual` | `Unequip()`                                       | `void`                   | Unequips the currently equipped item.                                                                                                                                                               |
| `public`         | `CanEquip(ItemStack itemStack)`                   | `bool`                   | Checks if the slot can equip a specific item stack. Returns true if the slot can equip the item stack. Otherwise, false.                                                                            |
| `public`         | `CanEquip(Item item)`                             | `bool`                   | Checks if the slot can equip a specific item. Returns true if the slot can equip the item. Otherwise, false.                                                                                        |
| `public virtual` | `TrySwitch(EquipmentSlot other)`                  | `bool`                   | Tries to switch the item in this slot with an item from another slot. The switch will fail if the item types don't match. Returns true if the items successfully switched places. Otherwise, false. |
| `public static`  | `GetDefaultActionMenuOptions(EquipmentSlot slot)` | `List<ActionMenuOption>` | Creates and returns the default action menu options for the equipment slot target.                                                                                                                  |
| `public virtual` | `GetActionMenuOptions()`                          | `List<ActionMenuOption>` | Returns a list of action menu options for this slot. Menu options are based on the settings you set in the Inventool window and item types window.                                                  |

## Creating a Slot

While it's recommended to use the [equipment](https://stylishesper.gitbook.io/inventool/editors/inventool/equipment "mention") to create slots, it's possible to create them through code at runtime.&#x20;

```csharp
var mySlot = new EquipmentSlot()
{
    // Set the position index
    positionIndex = 0,

    // Create searchable name
    name = "My Slot Name",

    // Set the background icon (Sprite)
    backgroundIcon = mySlotSprite,

    // Set the color tint of the background icon
    backgroundColorTint = Color.white,

    // Set the item type restriction (ItemType)
    itemType = myItemType
};
```

## Setting Items

Equipment slots can equip an [item-stack](https://stylishesper.gitbook.io/inventool/scripting/api/items/item-stack "mention"). In the examples below, `mySlot` is an instance of an `EquipmentSlot`.

Checking If Equipping Is Possible

You can check if an item can be equipped by an equipment slot. Note that TryEquip already checks for this.

### Equip An Item

The `TryEquip` method attempts to equip an `ItemStack`.  This method returns true if the item stack was successfully equipped. This may fail if the slot restricts the type of the item.

```csharp
bool result = mySlot.TryEquip(myItemStack);
```

### Unequip An Item

```csharp
mySlot.Unequip();
```

### Switching Items

The `TrySwitch` method attempts to switch item stacks with another slot. This method returns true if the switch was successful. This may fail if the slot cannot equip the item of the other slot (restricted type).

```csharp
bool result = mySlot.TrySwitch(myOtherSlot);
```

## Action Menu Options

By default, equipment slots' UI counterpart logically display item options for the [action-menu](https://stylishesper.gitbook.io/inventool/editors/components/ui/ui-toolkit/action-menu "mention"). You can override these options by editing the `EquipmentSlot.actionMenuOptionsGetter` field.

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

```csharp
EquipmentSlot.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.

```csharp
var options = mySlot.GetActionMenuOptions();
```
