Scrolling Cargo Inventory on LCD Panel 5/5 (1)

This 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 many items to show on the LCD Panel.

Also I formatted the numbers (with comma separators on thousands; i.e. 123,456,789.00).

If you use a larger, or smaller, font (I’m using 0.8), you probably will have to adjust the PANEL_LINES constant at the beginning of the script to the number of lines that fit on the panel with your font.

To make it scroll, just set up a Timer Block that runs the Programmable Block and then restarts the Timer Block. I use a delay of 1 second, but you can use whatever you want.

Enjoy!

const string PANEL_NAME = "LCD Panel 2";  
const string CONTAINER_NAME = "Large Cargo Container 2"; 
const int PANEL_LINES = 22; 
int lineOffset = 0;
void Main() 
{ 
    List<IMyTerminalBlock> work = new List<IMyTerminalBlock>();   
    GridTerminalSystem.SearchBlocksOfName(PANEL_NAME, work);      
    IMyTextPanel panel = (IMyTextPanel)work[0];    
    GridTerminalSystem.SearchBlocksOfName(CONTAINER_NAME, work);      
    IMyCargoContainer container = (IMyCargoContainer)work[0];    
     
    var containerItems = ((IMyInventoryOwner)container).GetInventory(0).GetItems();   
    
    List<String> list = new List<String>();
    for(int j = containerItems.Count - 1; j >= 0; j--)      
    {      
        String txt = decodeItemName(containerItems[j].Content.SubtypeName, containerItems[j].Content.TypeId.ToString()) + "  -  "; 
        String amt = amountFormatter((float)containerItems[j].Amount,containerItems[j].Content.TypeId.ToString()); 
        txt += amt; 
        list.Add(txt);
    }     
    list.Sort();
    list.Insert(0,"------------------------------------------------------");
    list.Insert(0,container.CustomName + " Inventory");
    for (int o=0; o < lineOffset; o++) {
        String shiftedItem = list[0];
        list.RemoveAt(0);
        list.Add(shiftedItem);
    }
    panel.WritePublicText(String.Join("\n",list.ToArray()), false);
 
    panel.ShowTextureOnScreen();  
    panel.ShowPublicTextOnScreen(); 
    if (list.Count > PANEL_LINES) {
        lineOffset++;
        if (list.Count - lineOffset < PANEL_LINES) {
           lineOffset = 0;
        }
    }
} 
 
String amountFormatter(float amt, String typeId) { 
    if (typeId.EndsWith("_Ore") || typeId.EndsWith("_Ingot")) {
        if (amt > 1000.0f) {
          return "" + Math.Round((float)amt/1000,2).ToString("###,###,##0.00") + "K"; 
        } else {
          return "" + Math.Round((float)amt,2).ToString("###,###,##0.00"); 
        }
    }
    return "" + Math.Round((float)amt,0).ToString("###,###,##0"); 
} 
 
String decodeItemName(String name, String typeId)  
{ 
    if (name.Equals("Construction")) { return "Construction Component"; } 
    if (name.Equals("MetalGrid")) { return "Metal Grid"; } 
    if (name.Equals("InteriorPlate")) { return "Interior Plate"; } 
    if (name.Equals("SteelPlate")) { return "Steel Plate"; } 
    if (name.Equals("SmallTube")) { return "Small Steel Tube"; } 
    if (name.Equals("LargeTube")) { return "Large Steel Tube"; } 
    if (name.Equals("BulletproofGlass")) { return "Bulletproof Glass"; } 
    if (name.Equals("Reactor")) { return "Reactor Component"; } 
    if (name.Equals("Thrust")) { return "Thruster Component"; } 
    if (name.Equals("GravityGenerator")) { return "GravGen Component"; } 
    if (name.Equals("Medical")) { return "Medical Component"; } 
    if (name.Equals("RadioCommunication")) { return "Radio Component"; } 
    if (name.Equals("Detector")) { return "Detector Component"; } 
    if (name.Equals("SolarCell")) { return "Solar Cell"; } 
    if (name.Equals("PowerCell")) { return "Power Cell"; } 
    if (name.Equals("AutomaticRifleItem")) { return "Rifle"; } 
    if (name.Equals("AutomaticRocketLauncher")) { return "Rocket Launcher"; } 
    if (name.Equals("WelderItem")) { return "Welder"; } 
    if (name.Equals("AngleGrinderItem")) { return "Grinder"; } 
    if (name.Equals("HandDrillItem")) { return "Hand Drill"; } 
    if (typeId.EndsWith("_Ore")) {
        if (name.Equals("Stone")) {
            return name;
        }
        return name + " Ore";
    }
    if (typeId.EndsWith("_Ingot")) {
        if (name.Equals("Stone")) {
            return "Gravel";
        }
        if (name.Equals("Magnesium")) {
            return name + " Powder";
        }
        if (name.Equals("Silicon")) {
            return name + " Wafer";
        }
       return name + " Ingot";
    }
    return name; 
}

Please rate this post

4 Responses

  1. This is really helpful, thanks)!

  2. I get error:
    Line 27: Preprocessor directives must appear as the first non-whitespace character on a line
    I dont know anything about c+ programming or i would figure it out myself. Thanks for this code and thanks in advance for any help.

    • Sorry about that… My scripts are getting all messed up… being encoded in HTML is a pain…

      Correct line is: String shiftedItem = list[0];

  3. ty admin

Leave a Reply