Factorio.com|Forums|Wiki|Mod Portal|API Docs

# Factorio Runtime Docs Version 2.0.77

# LuaInventory - Runtime Docs

## Class LuaInventory

A storage of item stacks.

## Members

clear() |  | Clear this inventory of all items so that it becomes empty.
Clear this inventory of all items so that it becomes empty.
can_insert(items) | → boolean | Can at least some items be inserted?
Can at least some items be inserted?
insert(items) | → uint32 | Insert items into this inventory.
Insert items into this inventory.
remove(items) | → uint32 | Remove items from this inventory.
Remove items from this inventory.
get_item_count(item?) | → uint32 | Get the number of all or some items in this inventory.
Get the number of all or some items in this inventory.
get_item_count_filtered(filter) | → uint32 | Get the number of items in this inventory that match provided filter.
Get the number of items in this inventory that match provided filter.
get_item_quality_counts(item?) | → dictionary[string → uint32] | Get the number of all or some items in this inventory, aggregated by quality.
Get the number of all or some items in this inventory, aggregated by quality.
is_empty() | → boolean | Does this inventory contain nothing?
Does this inventory contain nothing?
is_full() | → boolean | Is every stack in this inventory full? [...]
Is every stack in this inventory full? [...]
get_contents() | → array[ItemWithQualityCount] | Get counts of all items in this inventory.
Get counts of all items in this inventory.
supports_bar() | → boolean | Does this inventory support a bar? [...]
Does this inventory support a bar? [...]
get_bar() | → uint32 | Get the current bar. [...]
Get the current bar. [...]
set_bar(bar?) |  | Set the current bar. [...]
Set the current bar. [...]
supports_filters() | → boolean | If this inventory supports filters.
If this inventory supports filters.
is_filtered() | → boolean | If this inventory supports filters and has at least 1 filter set.
If this inventory supports filters and has at least 1 filter set.
can_set_filter(index, filter) | → boolean | If the given inventory slot filter can be set to the given filter.
If the given inventory slot filter can be set to the given filter.
get_filter(index) | → ItemFilter? | Gets the filter for the given item stack index.
Gets the filter for the given item stack index.
set_filter(index, filter) | → boolean | Sets the filter for the given item stack index. [...]
Sets the filter for the given item stack index. [...]
find_item_stack(item) | → LuaItemStack?, uint32? | Finds the first LuaItemStack in the inventory that matches the given item name.
Finds the first LuaItemStack in the inventory that matches the given item name.
find_empty_stack(item?) | → LuaItemStack?, uint32? | Finds the first empty stack. [...]
Finds the first empty stack. [...]
count_empty_stacks(include_filtered?, include_bar?) | → uint32 | Counts the number of empty stacks.
Counts the number of empty stacks.
get_insertable_count(item) | → uint32 | Gets the number of the given item that can be inserted into this inventory. [...]
Gets the number of the given item that can be inserted into this inventory. [...]
sort_and_merge() |  | Sorts and merges the items in this inventory.
Sorts and merges the items in this inventory.
resize(size) |  | Resizes the inventory. [...]
Resizes the inventory. [...]
destroy() |  | Destroys this inventory. [...]
Destroys this inventory. [...]
index | :: R defines.inventory? | The inventory index this inventory uses, if any.
The inventory index this inventory uses, if any.
name | :: R string? | Name of this inventory, if any. [...]
Name of this inventory, if any. [...]
entity_owner | :: R LuaEntity? | The entity that owns this inventory, if any.
The entity that owns this inventory, if any.
player_owner | :: R LuaPlayer? | The player that owns this inventory, if any.
The player that owns this inventory, if any.
equipment_owner | :: R LuaEquipment? | The equipment that owns this inventory, if any.
The equipment that owns this inventory, if any.
mod_owner | :: R string? | The mod that owns this inventory, if any.
The mod that owns this inventory, if any.
weight | :: R Weight | Gives a total weight of all items currently in this inventory.
Gives a total weight of all items currently in this inventory.
max_weight | :: R Weight? | Gives a maximum weight of items that can be inserted into this inventory.
Gives a maximum weight of items that can be inserted into this inventory.
valid | :: R boolean | Is this object valid? [...]
Is this object valid? [...]
object_name | :: R string | The class name of this object. [...]
The class name of this object. [...]
[] (index) | :: R LuaItemStack | The indexing operator.
The indexing operator.
# (length) | :: R uint32 | Get the number of slots in this inventory.
Get the number of slots in this inventory.

## Methods

### clear()

Clear this inventory of all items so that it becomes empty.

### can_insert(items) → boolean

Can at least some items be inserted?

#### Parameters

items | :: ItemStackIdentification | Items that would be inserted.
Items that would be inserted.

#### Return values

→ boolean | `true` if at least a part of the given items could be inserted into this inventory.

### insert(items) → uint32

Insert items into this inventory.

#### Parameters

items | :: ItemStackIdentification | Items to insert.
Items to insert.

#### Return values

→ uint32 | Number of items actually inserted.

### remove(items) → uint32

Remove items from this inventory.

#### Parameters

items | :: ItemStackIdentification | Items to remove.
Items to remove.

#### Return values

→ uint32 | Number of items actually removed.

### get_item_count(item?) → uint32

Get the number of all or some items in this inventory.

#### Parameters

item | :: ItemWithQualityID? | The item to count. If not specified, count all items.
The item to count. If not specified, count all items.

### get_item_count_filtered(filter) → uint32

Get the number of items in this inventory that match provided filter.

#### Parameters

filter | :: ItemFilter |

### get_item_quality_counts(item?) → dictionary[string → uint32]

Get the number of all or some items in this inventory, aggregated by quality.

#### Parameters

item | :: ItemID? |

### is_empty() → boolean

Does this inventory contain nothing?

### is_full() → boolean

Is every stack in this inventory full? Ignores stacks blocked by the current bar.

For the input slots of crafting machines that allow counts larger than the item stack size, this may return true even when more items can still be inserted.

### get_contents() → array[ItemWithQualityCount]

Get counts of all items in this inventory.

#### Return values

→ array[ItemWithQualityCount] | List of all items in the inventory.

### supports_bar() → boolean

Does this inventory support a bar? Bar is the draggable red thing, found for example on chests, that limits the portion of the inventory that may be manipulated by machines.

"Supporting a bar" doesn't mean that the bar is set to some nontrivial value. Supporting a bar means the inventory supports having this limit at all. The character's inventory is an example of an inventory without a bar; the wooden chest's inventory is an example of one with a bar.

### get_bar() → uint32

Get the current bar. This is the index at which the red area starts.

Only useable if this inventory supports having a bar.

### set_bar(bar?)

Set the current bar.

Only useable if this inventory supports having a bar.

#### Parameters

bar | :: uint32? | The new limit. Omitting this parameter or passing `nil` will clear the limit.
The new limit. Omitting this parameter or passing `nil` will clear the limit.

### supports_filters() → boolean

If this inventory supports filters.

### is_filtered() → boolean

If this inventory supports filters and has at least 1 filter set.

### can_set_filter(index, filter) → boolean

If the given inventory slot filter can be set to the given filter.

#### Parameters

index | :: uint32 | The item stack index
The item stack index
filter | :: ItemFilter | The item filter
The item filter

### get_filter(index) → ItemFilter?

Gets the filter for the given item stack index.

#### Parameters

index | :: uint32 | The item stack index
The item stack index

#### Return values

→ ItemFilter? | The current filter or `nil` if none.

### set_filter(index, filter) → boolean

Sets the filter for the given item stack index.

Some inventory slots don't allow some filters (gun ammo can't be filtered for non-ammo).

#### Parameters

index | :: uint32 | The item stack index.
The item stack index.
filter | :: ItemFilter or nil | The new filter. `nil` erases any existing filter.
The new filter. `nil` erases any existing filter.

#### Return values

→ boolean | If the filter was allowed to be set.

### find_item_stack(item) → LuaItemStack?, uint32?

Finds the first LuaItemStack in the inventory that matches the given item name.

#### Parameters

item | :: ItemWithQualityID | The item to find
The item to find

#### Return values

→ LuaItemStack? | The first matching stack, or `nil` if none match.
→ uint32? | The stack index of the matching stack, if any is found.

### find_empty_stack(item?) → LuaItemStack?, uint32?

Finds the first empty stack. Filtered slots are excluded unless a filter item is given.

#### Parameters

item | :: ItemWithQualityID? | If given, empty stacks that are filtered for this item will be included.
If given, empty stacks that are filtered for this item will be included.

#### Return values

→ LuaItemStack? | The first empty stack, or `nil` if there aren't any empty stacks.
→ uint32? | The stack index of the matching stack, if any is found.

### count_empty_stacks(include_filtered?, include_bar?) → uint32

Counts the number of empty stacks.

#### Parameters

include_filtered | :: boolean? | If true, filtered slots will be included. Defaults to false.
If true, filtered slots will be included. Defaults to false.
include_bar | :: boolean? | If true, slots blocked by the current bar will be included. Defaults to true.
If true, slots blocked by the current bar will be included. Defaults to true.

### get_insertable_count(item) → uint32

Gets the number of the given item that can be inserted into this inventory.

This is a "best guess" number; things like assembling machine filtered slots, module slots, items with durability, and items with mixed health will cause the result to be inaccurate. The main use for this is in checking how many of a basic item can fit into a basic inventory.

This accounts for the 'bar' on the inventory.

#### Parameters

item | :: ItemWithQualityID | The item to check.
The item to check.

### sort_and_merge()

Sorts and merges the items in this inventory.

### resize(size)

Resizes the inventory.

Items in slots beyond the new capacity are deleted.

Only inventories created by LuaGameScript::create_inventory can be resized.

#### Parameters

size | :: uint16 | New size of a inventory
New size of a inventory

#### Raised events

on_pre_script_inventory_resized instantly |
on_script_inventory_resized instantly |

### destroy()

Destroys this inventory.

Only inventories created by LuaGameScript::create_inventory can be destroyed this way.

## Attributes

### index :: Read defines.inventory?

The inventory index this inventory uses, if any.

### name :: Read string?

Name of this inventory, if any. Names match keys of defines.inventory.

### entity_owner :: Read LuaEntity?

The entity that owns this inventory, if any.

### player_owner :: Read LuaPlayer?

The player that owns this inventory, if any.

### equipment_owner :: Read LuaEquipment?

The equipment that owns this inventory, if any.

### mod_owner :: Read string?

The mod that owns this inventory, if any.

### weight :: Read Weight

Gives a total weight of all items currently in this inventory.

### max_weight :: Read Weight?

Gives a maximum weight of items that can be inserted into this inventory.

Can only be used if this is InventoryWithWeightLimit

### valid :: Read boolean

Is this object valid? This Lua object holds a reference to an object within the game engine. It is possible that the game-engine object is removed whilst a mod still holds the corresponding Lua object. If that happens, the object becomes invalid, i.e. this attribute will be `false`. Mods are advised to check for object validity if any change to the game state might have occurred between the creation of the Lua object and its access.

### object_name :: Read string

The class name of this object. Available even when `valid` is false. For LuaStruct objects it may also be suffixed with a dotted path to a member of the struct.

## Operators

### [] (index)

The indexing operator.

#### Example

    -- Will get the first item in the player's inventory.
    game.player.get_main_inventory()[1]

### # (length)

Get the number of slots in this inventory.

#### Example

    -- Will print the number of slots in the player's main inventory.
    game.player.print(#game.player.get_main_inventory())

Class used in

- LuaBurner::inventory
- LuaBurner::burnt_result_inventory
- LuaControl::get_inventory()
- LuaControl::get_main_inventory()
- LuaControl::opened
- LuaEntity::get_output_inventory()
- LuaEntity::get_module_inventory()
- LuaEntity::get_fuel_inventory()
- LuaEntity::get_burnt_result_inventory()
- LuaEntity::revive()
- LuaEntity::silent_revive()
- LuaEntity::mine()
- LuaEntity::set_inventory_size_override()
- LuaForce::get_linked_inventory()
- LuaGameScript::create_inventory()
- LuaGameScript::get_script_inventories()
- LuaItemCommon::get_inventory()
- LuaSurface::spill_inventory()
- on_built_entity
- on_entity_died
- on_gui_closed
- on_gui_opened
- on_player_built_tile
- on_player_cancelled_crafting
- on_player_mined_entity
- on_pre_player_crafted_item
- on_pre_script_inventory_resized
- on_robot_built_tile
- on_robot_mined_entity
- on_script_inventory_resized
- on_space_platform_built_tile
- on_space_platform_mined_entity
- on_tower_mined_plant