Skip to content

Instantly share code, notes, and snippets.

@ZerothAngel
Created August 2, 2015 17:24
Show Gist options
  • Save ZerothAngel/3d47f70a62e7645f6538 to your computer and use it in GitHub Desktop.
Save ZerothAngel/3d47f70a62e7645f6538 to your computer and use it in GitHub Desktop.
Space Engineers Script: Split container stacks
private const float StackSize = 10000.0f;
public bool SplitContainerContents(IMyCargoContainer container)
{
var inventory = ((Sandbox.ModAPI.Interfaces.IMyInventoryOwner)container).GetInventory(0);
var items = inventory.GetItems();
// NB We only check the first item
var item = items.Count > 0 ? items[0] : null;
var amount = item != null ? (float)item.Amount : 0.0f;
if (amount >= StackSize * 2.0f)
{
// Move to new stack
VRage.MyFixedPoint newStackAmount = (VRage.MyFixedPoint)StackSize;
inventory.TransferItemTo(inventory, 0, targetItemIndex: items.Count,
stackIfPossible: false, amount: newStackAmount);
return true;
}
return false;
}
@ZerothAngel
Copy link
Author

Call for each container you care about. Returns false if container can't be split anymore (e.g. first stack is less than 2*StackSize), true otherwise.

(I call this method for each container on each simulation tick to avoid complexity errors. Hence why it isn't just a straight "for" loop.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment