So, I have a very nice inventory GUi set up, and I can take GameObjects with an item script attached to them, and pick them up and put them in the inventory.
When I right click an item, what should happen is the item is ‘used’. I’ve got it recording these clicks… but I have no idea how to make the item ‘go’.
I believe I need two types of items. A instant use item (potions say) and delayed use items (like a grenade, that brings up a targeting cursor and isn’t actually used until thrown)
Unfortunately, I’m banging my head against the keyboard trying to figure out a clean way to do this without a bunch of delegate calls just to use one item, and might end up mis-triggering if the player is mashing the mouse.
How can I trigger the effects of an item. And if the item is used successfully, consume/destroy the item? Is there some way to just call a ‘UseItem’ function and pass it some details on what to do?
This is InventoryController which manages incoming items and outgoing items. It subscribes to a event ‘slotClickedEvent’ which is triggered by IPointerDown on a Slot (which is just a button)
using UnityEngine;
using System.Collections;
using System;
using UnityEngine.UI;
//Player checks inventory to see what they are carrying
//regarding passing of "Items" around.
//We HOLD and PASS GameObjects
//We ACCESS the Item component to do Item-y things.
public class InventoryController : MonoBehaviour
{
//...bunch of graphical related vars here...
private GameObject objectInHand //used with some accessors to track if mouse is holding an item. Mostly related to click/drag moving items between slots
private ArrayList allSlots;
private int emptySlots;
void Start()
{
//...
EventManager.slotClickedEvent += SlotClicked;
}
//code for picking up and adding is item here
private void SlotClicked(Slot slot)
{
if (Input.GetMouseButtonDown(0))
{
//logic for picking up and placing items here
}
else if (Input.GetMouseButtonDown(1) && slot.hasUseableItem)
{
////////////////////////////////
//use item code would go here//
}
}
//...code for tracking empty slots, adding items from the world etc here...
}
This is Slot, which is attached to a GUI prefab (has an image attached to another image)
//public delegate void SlotClickEventDelegate(Slot slot);
//public static event SlotClickEventDelegate SlotClickEvent;
private GameObject objectInSlot = null;
private Item itemInSlot = null;
public Item item
{ get { return itemInSlot; } }
// Use this for initialization
void Start ()
{ //...
}
//...methods for adding/merging/swapping items here...
public GameObject RemoveItem()
{
GameObject obj = objectInSlot;
objectInSlot = null;
SetSprite(null, 0.0f);
itemInSlot = null;
UpdateStackText(0);
return obj;
}
public void OnPointerDown (PointerEventData data)
{
EventManager.SendSlotClickEvent(this);
}
}
This is Item, which is attached to a GameObject meant to be used as… an Item.
public class Item : MonoBehaviour {
public ItemType type;
public Sprite icon;
//public string name;
public void Use()
{
}
}