ItemService

Provides item related functions.

# Methods

[{"index":"void ChangeOwner(Item item, InventoryComponent owner) (ServerOnly)","0":"Transfers the item's ownership right to the corresponding InventoryComponent."}]

[{"index":"void ChangeOwner(string itemGUID, InventoryComponent owner) (ServerOnly)","0":"Transfers the item's ownership right to the corresponding InventoryComponent."}]

[{"index":"Item CreateItem(Type itemType, string itemDataTableName, InventoryComponent ownerInventoryComponent = nil) (ServerOnly)","0":"Creates an item of the corresponding itemType. By entering tableDataName, you can retrieve data matching the Name among the data in DataSets\/DataSets\/UserItemDataSets prepared in advance. In UserItemDataSets, Name, IconRUID, and Description columns should be created as default, and additional columns can be added as needed."}]

[{"index":"Item GetItemByGUID(string guid) (ServerOnly)","0":"Uses the item GUID to get the cached item on the server."}]

[{"index":"table GetMODItemsByOwner(InventoryComponent owner)","0":"Gets the items owned by InventoryComponent."}]

[{"index":"void RemoveItem(Item item) (ServerOnly)","0":"Deletes an item."}]

##### inherited from Service:

[{"index":"EventHandlerBase ConnectEvent(string key, IScriptFunction eventHandler)","0":"Links events. When a key-type event occurs in this service, a handler is notified. The handler receives event parameters."}]

[{"index":"EventHandlerBase ConnectEvent(Type eventType, IScriptFunction eventHandler)","0":"Links events. This service notifies a handler when an eventType event occurs. The handler receives event parameters."}]

[{"index":"boolean DisconnectEvent(string key, EventHandlerBase eventHandler)","0":"Disconnects the event with the key-type event and the handler."}]

[{"index":"boolean DisconnectEvent(Type eventType, EventHandlerBase eventHandler)","0":"Disconnects the eventType event from its handler."}]

[{"index":"void SendEvent(EventType sendEvent)","0":"Generates an event."}]

# Examples

This is an example of adding items to a player's inventory, changing the quantity, and removing items from the inventory.

If you collide with an object named Get Item, you will get 3 items.  
If you collide with an object named Give Item, the number of items is reduced by 1. When the number of items reaches 0, it is removed.  
If you collide with an object named Trash Can, it will remove the item.
    
    
    @Component
    script InventoryTriggerHandler 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
    

# SeeAlso

  * InventoryComponent
  * TriggerEnterEvent