Welcome to Keen Software House Forums! Log in or Sign up to interact with the KSH community.
  1. You are currently browsing our forum as a guest. Create your own forum account to access all forum functionality.

displaying cargo inventory separately

Discussion in 'Programming Questions and Suggestions' started by not-a-cylon, Dec 17, 2016.

Thread Status:
This last post in this thread was made more than 31 days old.
  1. not-a-cylon Trainee Engineer

    Messages:
    39
    I'm trying to write a script that display the contents of each cargo container separately.

    For example, container 1 contains 100xCobalt, container 2 contains 100xIce.
    LCD should show:

    Container 1
    100xCobalt

    Container 2
    100xIce

    Instead, my code below outputs something like this.

    Container 1
    100xCobalt
    100xIce

    Container 2
    100xCobalt
    100xIce

    I've commented where I am stuck/where I messed up. I think my biggest problem is a lack of understanding in how to properly create nested lists and how to access the data in the nested lists. I just started to read about classes, and I think that might be an alternate way to fix the problem, but I'm not understanding yet how to use them.

    How can I fix the code and what are alternate solutions to the problem?

    Thank you!


    Code:
            public StringBuilder sb = new StringBuilder();
            public void Main(string argument)
            {
                var testLCD = GridTerminalSystem.GetBlockWithName("testLCD") as IMyTextPanel;
                var containerList = new List<IMyCargoContainer>();
                GridTerminalSystem.GetBlocksOfType(containerList);
                var containerInventoryList = new List<List<IMyInventoryItem>>();
    
                for (int i = 0; i < containerList.Count; i++)
                {
                    var containerInventory = new List<IMyInventoryItem>();
                    containerInventoryList.Add(containerList[i].GetInventory(0).GetItems());
                    containerInventoryList.Add(containerInventory); //I think the problem is here
                   
                }
    
                for (int i = 0; i < containerList.Count; i++)
                {
                    sb.Append(containerList[i].CustomName);
                    sb.AppendLine();
                    foreach(List<IMyInventoryItem> list in containerInventoryList)
                    {
                        var containerInventory = new List<IMyInventoryItem>();
                        containerInventory.AddList(list);
                        foreach (IMyInventoryItem item in containerInventory)
                        {
                            sb.Append(item);
                            sb.AppendLine();
                        }
                        containerInventory.Clear();
                    }
                }
                testLCD.WritePublicText(sb.ToString(), false);
                testLCD.ShowPublicTextOnScreen();
    
                sb.Clear();
    
            }
    --- Automerge ---
    I figured it out! Posting for knowledge's sake. My problem was making a bunch of lists and nesting them and then trying to access the data. I just skipped making new lists and instead made the list and accessed it only when needed here:
    Code:
    foreach (IMyInventoryItem item in containerList[i].GetInventory(0).GetItems())
    Code:
     public StringBuilder sb = new StringBuilder();
            public void Main(string argument)
            {
                var testLCD = GridTerminalSystem.GetBlockWithName("testLCD") as IMyTextPanel;
                var containerList = new List<IMyCargoContainer>();
                GridTerminalSystem.GetBlocksOfType(containerList);
    
    
                for (int i = 0; i < containerList.Count; i++)
                {
                    sb.Append("Cargo Container Inventory: " + (i + 1).ToString());
                    sb.AppendLine();
                    foreach (IMyInventoryItem item in containerList[i].GetInventory(0).GetItems())
                    {
                     
                        sb.Append(item.ToString());
                        sb.AppendLine();
                    }
                    sb.AppendLine();
                }
                testLCD.WritePublicText(sb.ToString(), false);
                testLCD.ShowPublicTextOnScreen();
    
                sb.Clear();
    
            }
     
Thread Status:
This last post in this thread was made more than 31 days old.