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.

Deducing Item Type From IMyInventoryItem

Discussion in 'Programming Questions and Suggestions' started by chaoticweg, Dec 3, 2016.

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

    Hello all, I'm trying to pass the time and get to know the in-game scripting API by writing a script that will display items stored anywhere in the current station, separated by item. I have everything working, except the logic that separates ores from ingots from components, etc.

    Right now, I'm trying to use the "is" keyword to deduce what type of item we're dealing with, e.g. -

    foreach (IMyInventoryItem item in tmpItems) {
        // ... snip
        if (item.Content is VRage.Game.MyObjectBuilder_Ore)
            // handle ore
        else if (item.Content is VRage.Game.MyObjectBuilder_Ingot)
            // handle ingot
        // ... etc, etc
    ... but classes VRage.Game.MyObjectBuilder[x] are all prohibited. Is there an established way to do this? I'm trying to be a little less ham-fisted than parsing item names, but I've spent a good half-hour Googling around and looking through sources and documentation.

    For reference, I'm not new to programming or to C#, just to this API. Thanks in advance!

    Edit: in the spoiler below is the output that I'm going for. I can get everything there, just can't seem to figure out the problem described above.
    Last edited: Dec 3, 2016
  2. Rachek Trainee Engineer

    I had the same goal and the same problem. Through trial and error, I managed to get this class put together. Feel free to use and abuse it as you wish.

    It works on inventories primarily - you create a ResourceSearcher instance, tell it what you want tallied by calling LookFor and passing in the 'item id' which will be something like "MyObjectBuilder_Ore.Iron", and then give it the inventories to search using InspectInventory. After you're done, call GetAmount with the item id to get the total amount across all the inventories.

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