Search Unity

Lock and Unlock item slot of inventory

Discussion in '2D' started by jtojung, Sep 5, 2019.

  1. jtojung

    jtojung

    Joined:
    Sep 1, 2019
    Posts:
    2
    if you see update function(last) I want to press m to increase unlockSlot but the number still 1. and when i click play and I try to add number but it is not enable to us.


    protected virtual void Start()
    {
    for (int i = 0; i < ItemSlots.Count/5 * unlockSlot; i++)
    {
    ItemSlots.OnPointerEnterEvent += slot => EventHelper(slot, OnPointerEnterEvent);
    ItemSlots.OnPointerExitEvent += slot => EventHelper(slot, OnPointerExitEvent);
    ItemSlots.OnRightClickEvent += slot => EventHelper(slot, OnRightClickEvent);
    ItemSlots.OnBeginDragEvent += slot => EventHelper(slot, OnBeginDragEvent);
    ItemSlots.OnEndDragEvent += slot => EventHelper(slot, OnEndDragEvent);
    ItemSlots.OnDragEvent += slot => EventHelper(slot, OnDragEvent);
    ItemSlots.OnDropEvent += slot => EventHelper(slot, OnDropEvent);
    }
    }
    private void EventHelper(BaseItemSlot itemSlot, Action<BaseItemSlot> action)
    {
    if (action != null)
    action(itemSlot);
    }
    public virtual bool CanAddItem(Item item, int amount = 1)
    {
    int freeSpaces = 0;
    foreach (ItemSlot itemSlot in ItemSlots)
    {
    if (itemSlot.Item == null || itemSlot.Item.ID == item.ID)
    {
    freeSpaces += item.MaxStacks - itemSlot.Amount;
    }
    }
    return freeSpaces >= amount;
    }
    public virtual bool AddItem(Item item)
    {
    for (int i = 0; i < ItemSlots.Count; i++)
    {
    if (ItemSlots.CanAddStack(item))
    {
    ItemSlots.Item = item;
    ItemSlots.Amount++;
    return true;
    }
    }
    for (int i = 0; i < ItemSlots.Count; i++)
    {
    if (ItemSlots.Item == null)
    {
    if (item.ID == item.ID)
    {
    ItemSlots.Item = item;
    ItemSlots.Amount++;
    return true;
    }

    }
    }
    return false;
    }
    public virtual bool RemoveItem(Item item)
    {
    for (int i = 0; i < ItemSlots.Count; i++)
    {
    if (ItemSlots.Item == item)
    {
    ItemSlots.Amount--;
    return true;
    }
    }
    return false;
    }
    public virtual Item RemoveItem(string itemID)
    {
    for (int i = 0; i < ItemSlots.Count; i++)
    {
    Item item = ItemSlots.Item;
    if (item != null && item.ID == itemID)
    {
    ItemSlots.Amount--;
    return item;
    }
    }
    return null;
    }
    public virtual int ItemCount(string itemID)
    {
    int number = 0;
    for (int i = 0; i < ItemSlots.Count; i++)
    {
    Item item = ItemSlots.Item;
    if (item != null && item.ID == itemID)
    {
    number += ItemSlots.Amount;
    }
    }
    return number;
    }
    public void Clear()
    {
    for (int i = 0; i < ItemSlots.Count; i++)
    {
    if (ItemSlots.Item != null && Application.isPlaying)
    {
    ItemSlots[i].Item.Destroy();
    }
    ItemSlots[i].Item = null;
    ItemSlots[i].Amount = 0;
    }
    }

    private void Update()
    {
    if (Input.GetKeyDown(KeyCode.M))
    {
    unlockSlot += buySlot;
    Debug.Log(unlockSlot);
    }
    }
    }[/i][/i][/i]
     
  2. bloodwolftico

    bloodwolftico

    Joined:
    Nov 13, 2017
    Posts:
    100
    Hi. Suggestion: please use the "Code" function in the forum so your code is easier to read, understand and pick apart for solutions :). Like this:

    Code (CSharp):
    1.   protected virtual void Start()
    2.     {
    3.         for (int i = 0; i < ItemSlots.Count/5 * unlockSlot; i++)
    4.         {
    5.             ItemSlots.OnPointerEnterEvent += slot => EventHelper(slot, OnPointerEnterEvent);
    6.             ItemSlots.OnPointerExitEvent += slot => EventHelper(slot, OnPointerExitEvent);
    7.             ItemSlots.OnRightClickEvent += slot => EventHelper(slot, OnRightClickEvent);
    8.             ItemSlots.OnBeginDragEvent += slot => EventHelper(slot, OnBeginDragEvent);
    9.             ItemSlots.OnEndDragEvent += slot => EventHelper(slot, OnEndDragEvent);
    10.             ItemSlots.OnDragEvent += slot => EventHelper(slot, OnDragEvent);
    11.             ItemSlots.OnDropEvent += slot => EventHelper(slot, OnDropEvent);
    12.         }
    13.     }
    14.     private void EventHelper(BaseItemSlot itemSlot, Action<BaseItemSlot> action)
    15.     {
    16.         if (action != null)
    17.             action(itemSlot);
    18.     }
    19.     public virtual bool CanAddItem(Item item, int amount = 1)
    20.     {
    21.         int freeSpaces = 0;
    22.         foreach (ItemSlot itemSlot in ItemSlots)
    23.         {
    24.             if (itemSlot.Item == null || itemSlot.Item.ID == item.ID)
    25.             {
    26.                 freeSpaces += item.MaxStacks - itemSlot.Amount;
    27.             }
    28.         }
    29.         return freeSpaces >= amount;
    30.     }
    31.     public virtual bool AddItem(Item item)
    32.     {
    33.         for (int i = 0; i < ItemSlots.Count; i++)
    34.         {
    35.             if (ItemSlots.CanAddStack(item))
    36.             {
    37.                 ItemSlots.Item = item;        
    38.                 ItemSlots.Amount++;          
    39.                 return true;
    40.             }
    41.         }
    42.         for (int i = 0; i < ItemSlots.Count; i++)
    43.         {
    44.             if (ItemSlots.Item == null)
    45.             {
    46.                 if (item.ID == item.ID)
    47.                 {
    48.                     ItemSlots.Item = item;
    49.                     ItemSlots.Amount++;
    50.                     return true;
    51.                 }
    52.            
    53.             }
    54.         }
    55.         return false;
    56.     }
    57.     public virtual bool RemoveItem(Item item)
    58.     {
    59.         for (int i = 0; i < ItemSlots.Count; i++)
    60.         {
    61.             if (ItemSlots.Item == item)
    62.             {
    63.                 ItemSlots.Amount--;
    64.                 return true;
    65.             }
    66.         }
    67.         return false;
    68.     }
    69.     public virtual Item RemoveItem(string itemID)
    70.     {
    71.         for (int i = 0; i < ItemSlots.Count; i++)
    72.         {
    73.             Item item = ItemSlots.Item;
    74.             if (item != null && item.ID == itemID)
    75.             {
    76.                 ItemSlots.Amount--;
    77.                 return item;
    78.             }
    79.         }
    80.         return null;
    81.     }
    82.     public virtual int ItemCount(string itemID)
    83.     {
    84.         int number = 0;
    85.         for (int i = 0; i < ItemSlots.Count; i++)
    86.         {
    87.             Item item = ItemSlots.Item;
    88.             if (item != null && item.ID == itemID)
    89.             {
    90.                 number += ItemSlots.Amount;
    91.             }
    92.         }
    93.         return number;
    94.     }
    95.     public void Clear()
    96.     {
    97.         for (int i = 0; i < ItemSlots.Count; i++)
    98.         {
    99.             if (ItemSlots[i].Item != null && Application.isPlaying)
    100.             {
    101.                 ItemSlots[i].Item.Destroy();
    102.             }
    103.             ItemSlots[i].Item = null;
    104.             ItemSlots[i].Amount = 0;
    105.         }
    106.     }
    107.  
    108.     private void Update()
    109.     {
    110.         if (Input.GetKeyDown(KeyCode.M))
    111.         {
    112.             unlockSlot += buySlot;
    113.             Debug.Log(unlockSlot);
    114.         }
    115.     }
    116. }
    Can you please share the rest of the code w us? The variable names and all.