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.

Another question about the obsolete IMyInventoryOwner

Discussion in 'Programming Questions and Suggestions' started by BumbleGrum, Jan 14, 2018.

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

    Messages:
    11
    I've search the forum and found some answers, but I'm still not sure what I have to change in the following piece of script! I hope someone can point me to what changes are needed....

    for (int i = 0; i < containerList.Count; i++)
    {
    if (containerList is IMyCargoContainer)
    {
    var containerInvOwner = containerList as IMyInventoryOwner;
    var containerItems = containerInvOwner.GetInventory(0).GetItems();
    for(int j = containerItems.Count - 1; j >= 0; j--)
    {
    String itemName = decodeItemName(containerItems[j].Content.SubtypeName,
    containerItems[j].Content.TypeId.ToString()) + "|" +
    containerItems[j].Content.TypeId.ToString();
    float amount = (float)containerItems[j].Amount;
    if (!consolidated.ContainsKey(itemName))
    {
    consolidated.Add(itemName, amount);
    }
    else
    {
    consolidated[itemName] += amount;
    }
    }
    }
    }
     
  2. Malware Master Engineer

    Messages:
    9,511
    First of all, you're trying to cast the entire list - not just one block - as a cargo container. As for IMyInventoryOwner... simply stop using it.
    Code:
    for (int i = 0; i < containerList.Count; i++)
    {
    	var cargo = containerList[i] as IMyCargoContainer;
    	if (cargo != null)
    	{
    		var containerItems = cargo.GetInventory(0).GetItems();
    		for (int j = containerItems.Count - 1; j >= 0; j--)
    		{
    			String itemName = decodeItemName(containerItems[j].Content.SubtypeName,
    								  containerItems[j].Content.TypeId.ToString()) + "|" +
    							  containerItems[j].Content.TypeId.ToString();
    			float amount = (float)containerItems[j].Amount;
    			if (!consolidated.ContainsKey(itemName))
    			{
    				consolidated.Add(itemName, amount);
    			}
    			else
    			{
    				consolidated[itemName] += amount;
    			}
    		}
    	}
    }
    
    As an aside and not very important, just some advice: One should always follow the common coding standards of the language one is writing for. You generally don't use the Pascal-cased name for string. You use the keyword string. Secondly, method names in C# are Pascal cased.
     
  3. BumbleGrum Trainee Engineer

    Messages:
    11
    Thanks for that Malware. That solves the problem. Much obliged.
     
Thread Status:
This last post in this thread was made more than 31 days old.