Skip to main content
added 368 characters in body
Source Link

I think a better aproach would be to store the quantity of an item, instead of storing the stackable itens repeatdly in a list. Because when we are talking about large amounts of stacking items, it wouldn't work;

using UnityEngine;
using System.Collections.Generic;
public class StackExchange : MonoBehaviour
{
    List<InventItem> rawInventory;
    List<List<InventItem>>List<InventSlot> groupedInventory; 

    void Start ()
    {
        rawInventory = new List<InventItem>();
        rawInventory.Add(new InventItem("apple" , true));
        rawInventory.Add(new InventItem("apple", true));
        rawInventory.Add(new InventItem("carrot", true));
        rawInventory.Add(new InventItem("knife", false));
        rawInventory.Add(new InventItem("knife", false));
        rawInventory.Add(new InventItem("knife", false));

        groupedInventory = new List<List<InventItem>>List<InventSlot>();
        foreach (InventItem i in rawInventory){
            List<InventItem> list = getInventSoubgroupproccessItem(i.name);
        }

    if (list== null || !iDebug.isStackableLog(getPrintableList(rawInventory){);
        string s = "Grouped list:\n";
    list = new List<InventItem> foreach (InventSlot slot in groupedInventory);{
            s += "{\n";
  list.Add(i);
          for (int i = 0; i groupedInventory< slot.Add(listquantity; i++);
            }    s += slot.item.output();
            elses if+= (i.isStackable)"}\n";
        }
        listDebug.AddLog(is);
        }

       string Debug.Log(getPrintableList (rawInventory)List<InventItem> list);{
        string soutput = "Grouped list:"{\n";
        foreach (List<InventItem>var listv in groupedInventorylist){
            soutput += getPrintableListv.output(list) ;
        }
        Debug.Log(s);
  output += "}
 \n";
    string getPrintableList (List<InventItem> list){ return output;
    }


    stringvoid outputproccessItem =(InventItem "item){\n";
        foreach (var vinventSlot in listgroupedInventory){
            outputif +=(inventSlot.item.name v== item.output()name ;&& inventSlot.item.isStackable){
        }
        outputinventSlot.quantity++;
 += "}\n";
        return output;
    }

 return;
    List<InventItem> getInventSoubgroup (string name){     }
        foreachgroupedInventory.Add (List<InventItem> listnew inInventSlot groupedInventory(item));
     }

 }

class InventSlot{
    public ifInventItem (list.Countitem;
 > 0 && list[0].namepublic ==int name)quantity;
    public InventSlot (InventItem item){
        this.item return= list;item;
        returnquantity null;= 1;
    }
 
 }

class InventItem
{
    public string name;
    public bool isStackable;

    public InventItem (string name , bool isStackable){
        this.name = name;
        this.isStackable = isStackable;
    }

    public string output (){
        return "\tInventoryItem - name:" + name + ", stackable:" + isStackable + "\n";  
    }
}
using UnityEngine;
using System.Collections.Generic;
public class StackExchange : MonoBehaviour
{
    List<InventItem> rawInventory;
    List<List<InventItem>> groupedInventory;
    void Start ()
    {
        rawInventory = new List<InventItem>();
        rawInventory.Add(new InventItem("apple" , true));
        rawInventory.Add(new InventItem("apple", true));
        rawInventory.Add(new InventItem("carrot", true));
        rawInventory.Add(new InventItem("knife", false));
        rawInventory.Add(new InventItem("knife", false));

        groupedInventory = new List<List<InventItem>>();
        foreach (InventItem i in rawInventory){
            List<InventItem> list = getInventSoubgroup(i.name);
            if (list== null || !i.isStackable){
                list = new List<InventItem>();
                list.Add(i);
                groupedInventory.Add(list);
            }
            else if (i.isStackable)
                list.Add(i);
        }

        Debug.Log(getPrintableList(rawInventory));
        string s = "Grouped list:\n";
        foreach (List<InventItem> list in groupedInventory){
            s += getPrintableList(list);
        }
        Debug.Log(s);
    }
 
    string getPrintableList (List<InventItem> list){
        string output = "{\n";
        foreach (var v in list){
            output += v.output() ;
        }
        output += "}\n";
        return output;
    }


    List<InventItem> getInventSoubgroup (string name){
        foreach (List<InventItem> list in groupedInventory)
            if (list.Count > 0 && list[0].name == name)
                return list;
        return null;
    }
 
 }

class InventItem
{
    public string name;
    public bool isStackable;

    public InventItem (string name , bool isStackable){
        this.name = name;
        this.isStackable = isStackable;
    }

    public string output (){
        return "\tInventoryItem - name:" + name + ", stackable:" + isStackable + "\n";  
    }
}

I think a better aproach would be to store the quantity of an item, instead of storing the stackable itens repeatdly in a list. Because when we are talking about large amounts of stacking items, it wouldn't work;

using UnityEngine;
using System.Collections.Generic;
public class StackExchange : MonoBehaviour
{
    List<InventItem> rawInventory;
    List<InventSlot> groupedInventory; 

    void Start () {
        rawInventory = new List<InventItem>();
        rawInventory.Add(new InventItem("apple" , true));
        rawInventory.Add(new InventItem("apple", true));
        rawInventory.Add(new InventItem("carrot", true));
        rawInventory.Add(new InventItem("knife", false));
        rawInventory.Add(new InventItem("knife", false));
        rawInventory.Add(new InventItem("knife", false));

        groupedInventory = new List<InventSlot>();
        foreach (InventItem i in rawInventory){
            proccessItem(i);
        }

        Debug.Log(getPrintableList(rawInventory));
        string s = "Grouped list:\n";
        foreach (InventSlot slot in groupedInventory){
            s += "{\n";
            for (int i = 0; i < slot.quantity; i++)
                s += slot.item.output();
            s += "}\n";
        }
        Debug.Log(s);
    }

    string getPrintableList (List<InventItem> list){
        string output = "{\n";
        foreach (var v in list){
            output += v.output() ;
        }
        output += "}\n";
        return output;
    }


    void proccessItem (InventItem item){
        foreach (var inventSlot in groupedInventory)
            if (inventSlot.item.name == item.name && inventSlot.item.isStackable){
                inventSlot.quantity++;
                return;
            }
        groupedInventory.Add ( new InventSlot (item));
    }

 }

class InventSlot{
    public InventItem item;
    public int quantity;
    public InventSlot (InventItem item){
        this.item = item;
        quantity = 1;
    }
}

class InventItem
{
    public string name;
    public bool isStackable;

    public InventItem (string name , bool isStackable){
        this.name = name;
        this.isStackable = isStackable;
    }

    public string output (){
        return "\tInventoryItem - name:" + name + ", stackable:" + isStackable + "\n";  
    }
}
Source Link

using UnityEngine;
using System.Collections.Generic;
public class StackExchange : MonoBehaviour
{
    List<InventItem> rawInventory;
    List<List<InventItem>> groupedInventory;
    void Start ()
    {
        rawInventory = new List<InventItem>();
        rawInventory.Add(new InventItem("apple" , true));
        rawInventory.Add(new InventItem("apple", true));
        rawInventory.Add(new InventItem("carrot", true));
        rawInventory.Add(new InventItem("knife", false));
        rawInventory.Add(new InventItem("knife", false));

        groupedInventory = new List<List<InventItem>>();
        foreach (InventItem i in rawInventory){
            List<InventItem> list = getInventSoubgroup(i.name);
            if (list== null || !i.isStackable){
                list = new List<InventItem>();
                list.Add(i);
                groupedInventory.Add(list);
            }
            else if (i.isStackable)
                list.Add(i);
        }

        Debug.Log(getPrintableList(rawInventory));
        string s = "Grouped list:\n";
        foreach (List<InventItem> list in groupedInventory){
            s += getPrintableList(list);
        }
        Debug.Log(s);
    }

    string getPrintableList (List<InventItem> list){
        string output = "{\n";
        foreach (var v in list){
            output += v.output() ;
        }
        output += "}\n";
        return output;
    }


    List<InventItem> getInventSoubgroup (string name){
        foreach (List<InventItem> list in groupedInventory)
            if (list.Count > 0 && list[0].name == name)
                return list;
        return null;
    }

 }

class InventItem
{
    public string name;
    public bool isStackable;

    public InventItem (string name , bool isStackable){
        this.name = name;
        this.isStackable = isStackable;
    }

    public string output (){
        return "\tInventoryItem - name:" + name + ", stackable:" + isStackable + "\n";  
    }
}