Created
August 2, 2015 17:24
-
-
Save ZerothAngel/3d47f70a62e7645f6538 to your computer and use it in GitHub Desktop.
Space Engineers Script: Split container stacks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.)