Adventure Creator Wikia

When multiple instances of an item can be carried by the player, the item's properties let you choose between selecting all items - or one-at-a-time - to transfer to another slot.

This script allows the player to specify exactly how many to transfer, by way of a "pop up" menu that uses a slider to represent the amount.

To use it:

  1. In the Settings Manager' Inventory settings panel, check "Items can be re-ordered in Menus?".
  2. In the Inventory menu's properties, set the "Inventory box type" field to "Custom Script". Note that this requires all of the element's click behaviour to be handled via a custom script, so any interaction behaviour must be incorporated into the script below.
  3. In the Variables Manager, create a new Global Float variable named "TransferAmount".
  4. Create a new Menu named ItemAmount. This will be the "pop up" box that is used to set the amount being transferred. Set its "Appear type" to "Manual".
  5. Inside this Menu, create a Slider element (named "Slider") and link its value to the TransferAmount variable. Optionally, create a Label that is also linked to this variable.
  6. Add a Button element named "OKButton", and set its "Click type" to "Custom Script".
  7. Paste the code below into a C# script named ItemTransfer.cs, and attach to a GameObject in the scene:

ItemTransfer.cs:

using UnityEngine;
using AC;

public class ItemTransfer : MonoBehaviour
{

	private const string transferAmountFloatVariableName = "TransferAmount";

	private const string inventoryMenuName = "Inventory";
	private const string inventoryBoxElementName = "InventoryBox";

	private const string itemAmountMenuName = "ItemAmount";
	private const string sliderElementName = "Slider";
	private const string buttonElementName = "OKButton";

	private InvInstance invInstanceToTransfer;
	private int slotToTransferTo;


	private void OnEnable ()
	{
		EventManager.OnMenuElementClick += OnMenuElementClick;
		EventManager.OnMenuTurnOn += OnMenuTurnOn;
	}


	private void OnDisable ()
	{
		EventManager.OnMenuTurnOn -= OnMenuTurnOn;
		EventManager.OnMenuElementClick -= OnMenuElementClick;
	}


	private void OnMenuElementClick (Menu menu, MenuElement menuElement, int slot, int buttonPressed)
	{
		if (menu.title == inventoryMenuName)
		{
			if (PlayerMenus.GetMenuWithName (itemAmountMenuName).IsOn ())
			{
				return;
			}

			if (menuElement.title == inventoryBoxElementName)
			{
				MenuInventoryBox inventoryBox = menuElement as MenuInventoryBox;

				InvInstance selectedInstance = KickStarter.runtimeInventory.SelectedInstance;
				InvInstance clickedInstance = inventoryBox.GetInstance (slot);

				if (InvInstance.IsValid (selectedInstance))
				{
					if (InvInstance.IsValid (clickedInstance))
					{
						if (selectedInstance == clickedInstance)
						{
							// Clicked currently-selected item, deselect
							KickStarter.runtimeInventory.SetNull ();
						}
						else if (selectedInstance.InvItem == clickedInstance.InvItem)
						{
							// Clicked filled slot with same item type as selected
							RequestTransfer (selectedInstance, slot);
						}
						else
						{
							// Clicked filled slot with another item selected
						}
					}
					else
					{
						// Clicked empty slot, item selected
						RequestTransfer (selectedInstance, slot);
					}
				}
				else
				{
					if (InvInstance.IsValid (clickedInstance))
					{
						// Select item as normal
						clickedInstance.Select ();
					}
					else
					{
						// Clicked empty slot, no item selected
					}
				}
			}
		}
		else if (menu.title == itemAmountMenuName)
		{
			if (menuElement.title == buttonElementName)
			{
				PerformTransfer ();
			}
		}
	}


	private void OnMenuTurnOn (Menu menu, bool isInstant)
	{
		if (menu.title == itemAmountMenuName)
		{
			MenuSlider menuSlider = (MenuSlider) menu.GetElementWithName (sliderElementName);
			if (menuSlider.uiSlider)
			{
				// Sync UI slider min/max values
				menuSlider.uiSlider.minValue = menuSlider.minValue;
				menuSlider.uiSlider.maxValue = menuSlider.maxValue;
			}
		}
	}


	private void RequestTransfer (InvInstance invInstance, int slot)
	{
		invInstanceToTransfer = invInstance;
		slotToTransferTo = slot;

		float maxValue = (float) invInstance.Count;

		MenuSlider menuSlider = (MenuSlider) PlayerMenus.GetElementWithName (itemAmountMenuName, sliderElementName);
		menuSlider.minValue = 1;
		menuSlider.maxValue = maxValue;
		menuSlider.numberOfSteps = invInstance.Count - 1;
		GlobalVariables.GetVariable (transferAmountFloatVariableName).FloatValue = maxValue;

		KickStarter.runtimeInventory.SetNull ();
		PlayerMenus.GetMenuWithName (itemAmountMenuName).TurnOn ();
	}


	private void PerformTransfer ()
	{
		invInstanceToTransfer.TransferCount = (int) GlobalVariables.GetVariable (transferAmountFloatVariableName).FloatValue;
		KickStarter.runtimeInventory.PlayerInvCollection.Insert (invInstanceToTransfer, slotToTransferTo);
		PlayerMenus.GetMenuWithName (itemAmountMenuName).TurnOff ();
	}

}