Cargo Loot
How to generate prefab from grids
How to give prefabs random loot
SBC format explained
XML syntax explained
The ContainerTypes.sbc file is the loot table for all random loot you can find in NPC ships and structures.
Prefabs that have blocks with inventories can each reference one of those ContainerType Definitions to get loot spawned inside them, randomized each time the prefab is spawned.
There are two ways of giving blocks the needed entity component for this to work, pick one of them:
Option 1: Profiler build
Using the Profiler build of the game you can access the F12 debug menu.
Tip: If F12 is bound to something you don't want to spam (like Steam screenshots), you can combine it with Alt and it will still work for in game.
Once there, from the Game tab enable the Containers one and Esc to close.
As the right side GUI says, holding ALT will allow you to interact with it.
You first need to aim at any interactive object from the block you want to set the loot to, it will also remember the last aimed one so be careful.
To close the right side window, open F12 again and uncheck the Containers.
Then to export as prefab, copy the grid and F11 to export clipboard as prefab. Or in F12's Game -> Prefabs you can export the entire world as a spawngroup! Asteroids and everything.
Option 2: Editing prefab SBC
You first need to have the prefab sbc (Converting between Prefab and ShipBlueprint).
Open the .sbc file and find the first block that you wish to have loot.
This can be helped by adding something unique in the terminal name, but if it's not a terminal block (like a dead body or box) then maybe some unique skin which you can then remove from the sbc once found.
All block sections start with <MyObjectBuilder_CubeBlock xsi:type="...">.
Since it has an inventory it should have a <ComponentContainer> which looks something like:
<ComponentContainer>
<Components>
<!-- Add your component here! -->
<ComponentData>
<TypeId>MyInventoryBase</TypeId>
<Component xsi:type="MyObjectBuilder_Inventory">
<!-- stuff and things -->
</Component>
</ComponentData>
<!-- ... or add it here! -->
</Components>
</ComponentContainer>
</MyObjectBuilder_CubeBlock>
Make sure you don't break the existing component(s).
Now insert the following in the place mentioned above:
<ComponentData>
<TypeId>MyRandomCargoEntityComponent</TypeId>
<Component xsi:type="MyObjectBuilder_RandomCargoEntityComponent">
<ContainerType>TheLootTableSubtypeId</ContainerType>
</Component>
</ComponentData>
Where TheLootTableSubtypeId must be replaced with a SubtypeId from ContainerTypes.sbc (can be any name, it does need to be the ContainerType Definition type though).
Now repeat the same for all the blocks you wish to have loot tables and save the file.
Finally, delete the .sbcB5 for it to load your .sbc which will then generate .sbcB5 again from it.
If you don't want to use the vanilla loot tables then copy the file to your mod, leave only one definition, change its SubtypeId to something very unique and start changing things! And of course use that subtypeId in your prefab.