Cargo Container Inventory 0/5 (14)

cargoinventory

The script below goes through the inventory of a cargo container and outputs a list of all the items to an LCD Panel.

Change the two constants at the top of the script to match the panel name that you want to use to show the inventory and the name of the cargo container whose inventory you want.

  1. const string PANEL_NAME = "LCD Panel 2";  
  2. const string CONTAINER_NAME = "Large Cargo Container 2";  
  3.  
  4. void Main()
  5. {
  6.     List<IMyTerminalBlock> work = new List<IMyTerminalBlock>();  
  7.     GridTerminalSystem.SearchBlocksOfName(PANEL_NAME, work);      
  8.     IMyTextPanel panel = (IMyTextPanel)work[0];    
  9.     GridTerminalSystem.SearchBlocksOfName(CONTAINER_NAME, work);      
  10.     IMyCargoContainer container = (IMyCargoContainer)work[0];    
  11.     panel.WritePublicText(container.CustomName + " Inventory\n------------------------------------------------------\n", false);
  12.      
  13.     var containerItems = ((IMyInventoryOwner)container).GetInventory(0).GetItems();  
  14.    
  15.     List<String> list = new List<String>();
  16.     for(int j = containerItems.Count - 1; j >= 0; j--)      
  17.     {      
  18.         String txt = decodeItemName(containerItems[j].Content.SubtypeName, containerItems[j].Content.TypeId.ToString()) + "  -  ";
  19.         String amt = amountFormatter((float)containerItems[j].Amount,containerItems[j].Content.TypeId.ToString());
  20.         txt += amt;
  21.         txt += "\n";
  22.         list.Add(txt);
  23.     }    
  24.     list.Sort();
  25.     List<String>.Enumerator e = list.GetEnumerator();
  26.     while (e.MoveNext()) {
  27.         panel.WritePublicText(e.Current, true);
  28.     }
  29.  
  30.     panel.ShowTextureOnScreen();  
  31.     panel.ShowPublicTextOnScreen();  
  32. }
  33.  
  34. String amountFormatter(float amt, String typeId) {
  35.     if (typeId.EndsWith("_Ore") || typeId.EndsWith("_Ingot")) {
  36.         if (amt > 1000.0f) {
  37.           return "" + Math.Round((float)amt/1000,2) + "K";
  38.         } else {
  39.           return "" + Math.Round((float)amt,2);
  40.         }
  41.     }
  42.     return "" + Math.Round((float)amt,0);
  43. }
  44.  
  45. String decodeItemName(String name, String typeId)  
  46. {
  47.     if (name.Equals("Construction")) { return "Construction Component"; }
  48.     if (name.Equals("MetalGrid")) { return "Metal Grid"; }
  49.     if (name.Equals("InteriorPlate")) { return "Interior Plate"; }
  50.     if (name.Equals("SteelPlate")) { return "Steel Plate"; }
  51.     if (name.Equals("SmallTube")) { return "Small Steel Tube"; }
  52.     if (name.Equals("LargeTube")) { return "Large Steel Tube"; }
  53.     if (name.Equals("BulletproofGlass")) { return "Bulletproof Glass"; }
  54.     if (name.Equals("Reactor")) { return "Reactor Component"; }
  55.     if (name.Equals("Thrust")) { return "Thruster Component"; }
  56.     if (name.Equals("GravityGenerator")) { return "GravGen Component"; }
  57.     if (name.Equals("Medical")) { return "Medical Component"; }
  58.     if (name.Equals("RadioCommunication")) { return "Radio Component"; }
  59.     if (name.Equals("Detector")) { return "Detector Component"; }
  60.     if (name.Equals("SolarCell")) { return "Solar Cell"; }
  61.     if (name.Equals("PowerCell")) { return "Power Cell"; }
  62.     if (name.Equals("AutomaticRifleItem")) { return "Rifle"; }
  63.     if (name.Equals("AutomaticRocketLauncher")) { return "Rocket Launcher"; }
  64.     if (name.Equals("WelderItem")) { return "Welder"; }
  65.     if (name.Equals("AngleGrinderItem")) { return "Grinder"; }
  66.     if (name.Equals("HandDrillItem")) { return "Hand Drill"; }
  67.     if (typeId.EndsWith("_Ore")) {
  68.         if (name.Equals("Stone")) {
  69.             return name;
  70.         }
  71.         return name + " Ore";
  72.     }
  73.     if (typeId.EndsWith("_Ingot")) {
  74.         if (name.Equals("Stone")) {
  75.             return "Gravel";
  76.         }
  77.         if (name.Equals("Magnesium")) {
  78.             return name + " Powder";
  79.         }
  80.         if (name.Equals("Silicon")) {
  81.             return name + " Wafer";
  82.         }
  83.        return name + " Ingot";
  84.     }
  85.     return name;
  86. }

Please rate this post

22 Responses

  1. It might be a stupid question, but I’m not yet very well in programming with C#.
    Can you publish a version that keeps refreshing itself every 10 seconds or something?

    With kind regards,

    Svenator

    PS: Thnx for the wonderfull script 😀

    • Unfortunately no… the game doesn’t allow that…

      The workaround is to use a timer block set for 10 second delay, and under the actions put:

      Run Programming block

      Start Timer (itself)

      That way the timer is the one that does the looping.

      Hope this helps!

  2. When I’m checking the code I get: Name COMPONENT_CONTAINER does not exist in the actual context

  3. Top of the morning to you fine sir,

    First I must say I fancy this script quite a lot.

    Unfortunately I have run into a strange problem:
    Assume you have eight containers and displays (“Warehouse Display A” and “Warehouse Large Cargo Container A”, through to the letter “H”).
    For some strange reason the script will not execute for group “G” and “H” and let me know about an error. The thing is, that every set of container and display runs in a seperate executable which were all copied and pasted and there is no mistake in the names of the group “G” and “H” (either in container, display or script).

    So basically my problem is that I have run into a dead end and I am wondring if there may be any known bug or limit for the amount of requestes or names..

    P.S.: I even double checked for case sensetivity and redundand spaces.

    Best regards,

    Wintermute

  4. Only problem that I can think is that you have other blocks where the name is colliding.

    Example: If your panel is call “Warehouse G” and your cargo container is called “Warehouse G Cargo Container”, the script breaks because when it looks for the panel it searches for blocks named “Warehouse G” (and finds both).

    Either use full names (i.e. “Warehouse G Panel” and “Warehouse G Cargo Container”) or in some other fashion make sure that the block search will not get confused. This problem arises because the “SearchBlocksOfName” method finds any blocks that have the string specified in ANY part of the name.

    So if it’s searching for “Warehouse G”, it will find any blocks that have that ANYWHERE in the name: “Warehouse G Panel”, “Warehouse G Timer”, “Warehouse G Programming Block”, “Warehouse G Cargo Container”, “Text Panel for Warehouse G”, etc.

    So try messing around with the names and see what happens…

    Hope this helps!

  5. […] script below is similar to the Cargo Container Inventory script. The difference is that this one will scroll the inventory list upwards if there are too […]

  6. Hi, Im a beginner at this.. So, here’s a dumb question:

    How do I fix so that one LCD display shows multiple containers, such as if my drills on ship is full or what is inside them rather.

    Line 1 and 2
    const string PANEL_NAME = “LCD Panel 2”;
    const string CONTAINER_NAME = “Large Cargo Container 2”;

    How do I add multiple containers to the bottom one?

    Thank you!

    • Name them all the same: “Miner Cargo Container”. Don’t need to have a number after the name.

  7. SpookyHmmer

    this seems like a great mod but i keep having one problem.
    It keeps on giving me this problem:
    Caught exception during execution of script:Index was out of range. Must be non-negative and less than the size of the collection.
    Parameter name: index

    What is causing this?

  8. SpookyHmmer

    I found the 2 lines that make the error.
    These two are the reason.

    IMyTextPanel panel = (IMyTextPanel)work[0];
    IMyCargoContainer container = (IMyCargoContainer)work[0];

    Does this help?

  9. SpookyHmmer

    i found it, it’s becouse i didn’t “own” the cargo container :/ … sorry for taking your time

  10. While compiling get the following error,

    “Line 15: The type or namespace name “IMyInventoryOwner” could not be found, are you missing a using directive or an assembly reference?”

    Any ideas?

    • Last update changed some stuff with IMyInventory. I’m working on fixing the scripts. If you want to fix: wherever there is something that starts with IMyInventory change it to VRage.ModAPI.Ingame.IMyInventory

      • Worked!!

        Only changed one line, Line 13.

        From:
        var containerItems = ((IMyInventoryOwner)container).GetInventory(0).GetItems();

        To:
        var containerItems = ((VRage.ModAPI.Ingame.IMyInventoryOwner)container).GetInventory(0).GetItems();

        Thank you very much!!

        • Changed the names of three cargo containers to the same name, following the instructions previous to display multiple containers as one.

          Only displaying the first.

          • This version only shows one container (the idea was you would have an LCD on the container listing its inventory, kind of a visual way to find stuff). If you want to consolidate multiple containers on a single panel, use the “Consolidated Inventory – Improved” script.

            Haven’t fixed the IMyInvetory error on that one yet, so you’ll have to fix it like you did this one.

  11. How do i get the inventory to go down, like move the screen down to see all of the containers contents?

Leave a Reply