Scripting

Scripting2019-06-10T11:48:17+00:00
Before you can use Inventory System through scripting you need to add the namespace to the top of your script.
Finding items in database2019-05-27T14:29:01+00:00

Reference item in Unity Inspector. With this code snippet you can select a database item in the Unity Inspector.

Find an item in database by index.

Find an item in database by name property using Linq.

Changes made to database items are persistent and will not be resetted on restart of the game. Instantiate the item if you need an instance of it.

Changes made to database items are persistent and will not be resetted on restart of the game. Instantiate the item if you need an instance of it.
Finding items in ItemContainer2019-05-27T14:26:41+00:00

Find items by id.

Find item by type and name.

Adding an item to ItemContainer2019-05-27T14:24:09+00:00

The following code adds an item to an ItemContainer with name “Inventory”. This method doesn’t check multiple ItemContainers. It uses the ItemContainer with highest priority.

Using a static method to add an item to an ItemContainer will check if the item can be added. If it fails to add the item, it will move to the next ItemContainer with the same name but lower priority. So if your main “Inventory” is full it will try to add the item to the next “Inventory”.

Removing an item from ItemContainer2019-05-27T14:21:36+00:00

The following code removes an item from ItemContainer with name “Inventory”.

A static method includes all containers with name “Inventory”. If your main “Inventory” container doesn’t have the amount it will continue to search in lower priority container for the remaining amount.

ItemContainer callbacks2019-05-27T14:18:52+00:00

Register to a container callback.

Availible ItemContainer callbacks:

OnAddItem(Item item, Slot slot); Called when an item is added to the container.
OnFailedToAddItem(Item item); Called when an item could not be added to the container.
OnRemoveItem(Item item, int amount, Slot slot); Called when an item was removed from the container.
OnFailedToRemoveItem(Item item, int amount); Called when an item could not be removed.
OnUseItem(Item item, Slot slot); Called when an item was used from this container.
OnDropItem(Item item, GameObject droppedInstance); Called when an item was dropped from this container to world.
Accessing item properties2019-05-27T14:15:26+00:00

This code snippet finds an item property by name.

Randomize item property2019-05-27T14:13:35+00:00

Randomize item property in a percentage range.

Use custom randomization.

Creating custom ItemAction2019-05-27T13:23:23+00:00

To create a custom ItemAction extend from ItemAction and override the OnUpdate method. If your action requires multiple frames or time to complete you can return ActionStatus.Running until it is finished. Then return ActionStatus.Success to continue executing next action in list. Returning ActionStatus.Failure will stop the behaviour including all following acrions.

Custom ItemActions will be added automatically to the editor.

Trigger Callbacks2019-05-27T14:08:54+00:00

To receive callbacks from trigger you need to implement one of the following callbacks.

Interface Method
ITriggerUsedHandler // Use this callback to detect trigger used events
void OnTriggerUsed(GameObject player);
ITriggerUnUsedHandler // Use this callback to detect trigger un-used events
void OnTriggerUnUsed(GameObject player);
ITriggerCameInRange // Use this callback to detect when player comes in range
void OnCameInRange(GameObject player);
ITriggerWentOutOfRange // Use this callback to detect when player went out of range
void OnWentOutOfRange(GameObject player);
ITriggerPointerEnter // Use this callback to detect when the pointer enters the trigger(Mouse over trigger)
void OnPointerEnter(PointerEventData eventData);
ITriggerPointerExit // Use this callback to detect when the pointer exits the trigger(Mouse not longer over trigger)
void OnPointerExit(PointerEventData eventData);
ITriggerSelectSellItem // Use this callback to detect when an item is selected for sell
void OnSelectSellItem(Item item, GameObject player);
ITriggerSoldItem // Use this callback to detect when an item was sold
void OnSoldItem(Item item, GameObject player);
ITriggerFailedToSellItem // Use this callback to detect when an item couldn’t be sold
void OnFailedToSellItem(Item item, GameObject player, Trigger.FailureCause failureCause);
ITriggerSelectBuyItem // Use this callback to detect when an item is selected for purchase
void OnSelectBuyItem(Item item, GameObject player);
ITriggerBoughtItem // Use this callback to detect when an item was bought
void OnBoughtItem(Item item, GameObject player);
ITriggerFailedToBuyItem // Use this callback to detect when a purchase failed(Often because no currency or if the inventory is full)
void OnFailedToBuyItem(Item item, GameObject player, Trigger.FailureCause failureCause);
ITriggerCraftStart // Use this callback to detect when the user starts crafting
void OnCraftStart(Item item, GameObject player);
ITriggerFailedCraftStart // Use this callback to detect when craft start failed, for example the player has no ingredients or if he is already crafting
void OnFailedCraftStart(Item item, GameObject player, Trigger.FailureCause failureCause);
ITriggerCraftItem // Use this callback to detect when the user crafted an item
void OnCraftItem(Item item, GameObject player);
ITriggerFailedToCraftItem // Use this callback to detect when the user failed to craft item
void OnFailedToCraftItem(Item item, GameObject player, Trigger.FailureCause failureCause);
ITriggerCraftStop // Use this callback to detect when the user stops crafting
void OnCraftStop(Item item, GameObject player);
Extending Trigger2019-05-27T14:07:04+00:00

Example how to extend Trigger.

Creating custom TriggerAction2019-05-27T13:17:12+00:00

This TriggerAction sets players rotation to look at the trigger.

Custom trigger actions will be added automatically to triggers behavior menu.