InventoryComponent

The component which manages owned items.

# Properties

[{"index":"boolean IsInitialized (ReadOnly) (HideFromInspector)","0":"Checks whether inventory initialization is complete. InventoryComponent-related functions will work normally after initialization."}]

##### inherited from Component:

[{"index":"boolean Enable (Sync)","0":"Checks whether Component is activated or not."}]

[{"index":"boolean EnableInHierarchy (ReadOnly) (HideFromInspector)","0":"Displays whether this Component is Enable in the hierarchy. If Entity Enable is false regardless of Component Enable, returns false."}]

[{"index":"Entity Entity (ReadOnly)","0":"The Entity that owns this component."}]

# Methods

[{"index":"table GetItemList()","0":"Gets the owned items."}]

[{"index":"table GetItemsWithType(Type itemType)","0":"Brings the input type item among the owned ones."}]

##### inherited from Component:

[{"index":"boolean IsClient()","0":"Returns whether the current execution environment is a client or not."}]

[{"index":"boolean IsServer()","0":"Returns whether the current execution environment is a server or not."}]

# Events

[{"index":"InventoryItemAddedEvent","0":"This event occurs when adding an item to the inventory."}]

[{"index":"InventoryItemInitEvent","0":"This event occurs when initializing the inventory. InventoryComponent related functions will work normally after the event."}]

[{"index":"InventoryItemModifiedEvent","0":"This event occurs when editing an item of the inventory."}]

[{"index":"InventoryItemRemovedEvent","0":"This event occurs when removing an item of the inventory."}]

# Examples

The InventoryComponent fires events when an item is added, removed, or changed in the inventory. Here is an example of when each event was called using the example code for ItemService.
    
    
    @Component
    script ItemTriggerProcessor extends Component
        @EventSender("Self")
        handler HandleTriggerEnterEvent(TriggerEnterEvent event)
        -- Parameters
        local TriggerBodyEntity = event.TriggerBodyEntity
        --------------------------------------------------------
        if self:IsClient() then
            return
        end
    
        local inventory = self.Entity.InventoryComponent
        local items = inventory:GetItemList()
    
        if TriggerBodyEntity.Name == "Get Item" then
            local newItem = _ItemService:CreateItem(TestItem, "Test Item", inventory)
            newItem.ItemCount = 3
        elseif TriggerBodyEntity.Name == "Give Item" then
            if #items > 0 then
                items[1].ItemCount = items[1].ItemCount - 1
                if items[1].ItemCount == 0 then
                    _ItemService:RemoveItem(items[1])
                end
            end
        elseif TriggerBodyEntity.Name == "Trash Can" then
            if #items > 0 then
                _ItemService:RemoveItem(items[1])
            end
        end
        end
    end
    

When adding and removing items to the inventory using the `_ItemService:CreateItem` and `_ItemService:RemoveItem` functions,  
the `InventoryItemAddedEvent` and `InventoryItemRemovedEvent` events are fired respectively.
    
    
    @Component
    script InventoryLogger extends Component
        @EventSender("Self")
        handler HandleInventoryItemAddedEvent(InventoryItemAddedEvent event)
        -- Parameters
        local Entity = event.Entity
        local Items = event.Items
        --------------------------------------------------------
        if self:IsServer() then
            return
        end
    
        log("[Get Item]")
        end
    
        @EventSender("Self")
        handler HandleInventoryItemRemovedEvent(InventoryItemRemovedEvent event)
        -- Parameters
        local Entity = event.Entity
        local Items = event.Items
        --------------------------------------------------------
        if self:IsServer() then
            return
        end
    
        log("[Item Removed]")
        end
    end
    

When the value of the ItemCount property changes, the `InventoryItemModifiedEvent` event occurs.

An event occurs if as follows.

  * When the owner of the item is changed
  * When the property value of the item type (in this example, TestItem) is changed


    
    
    @Component
    script InventoryAcquisitionLogger extends Component
        @EventSender("Self")
        handler HandleInventoryItemModifiedEvent(InventoryItemModifiedEvent event)
        -- Parameters
        local Entity = event.Entity
        local Items = event.Items
        --------------------------------------------------------
        if self:IsServer() then
            return
        end
    
        log ("[Give Item]")
        end
    end
    

Your inventory remains after you leave the game. The list of items are checked in `InventoryItemInitEvent`, which occurs when entering the game.
    
    
    @Component
    script InventoryItemLogger extends Component
        @EventSender("Self")
        handler HandleInventoryItemInitEvent(InventoryItemInitEvent event)
        -- Parameters
        local Entity = event.Entity
        local Items = event.Items
        --------------------------------------------------------
        if self:IsServer() then
            return
        end
    
        log("[Current Items]")
        for i = 1, #Items do
            log("Item" .. i .. ": ", Items[i].ItemDataTableName)
        end
        end
    end
    

# SeeAlso

  * InventoryItemAddedEvent
  * InventoryItemInitEvent
  * InventoryItemModifiedEvent
  * InventoryItemRemovedEvent
  * TriggerEnterEvent
  * ItemService