Hi, i'm trying to add items to an airdrop (removing worked before) but I'm somehow missing something. I've been using http://oxidemod.org/plugins/betterloot.828/ as example and basically tried everything but apparently I'm missing something. Any thoughts? This is currently the code I have.
Code:void OnEntitySpawned(BaseNetworkable entity) { LootContainer container = entity.GetComponent<LootContainer>(); if (container == null) { return; } else if (container is SupplyDrop) { ConsoleSystem.Broadcast("chat.add", 0, "Airdrop Dropped"); container.minSecondsBetweenRefresh = -1; container.maxSecondsBetweenRefresh = 0; container.CancelInvoke("SpawnLoot"); foreach (Item item in createItemList()) { item.MoveToContainer(container.inventory, -1, false); } container.inventory.MarkDirty(); } private List<Item> createItemList() { List<Item> itemList = new List<Item>(); Item i1 = ItemManager.CreateByName("explosive.timed", 2); Item i2 = ItemManager.CreateByName("rifle_ak", 1); itemList.Add(i1); itemList.Add(i2); return itemList; }
Solved Add items to container
Discussion in 'Rust Development' started by DroneFW, Jun 1, 2015.
-
Somehow the last update fixed it. Ended up with this:
Code:void OnEntitySpawned(BaseNetworkable entity) { if (entity.name == "items/supply_drop") { LootContainer supplyDrop = entity.GetComponent<LootContainer>(); if (supplyDrop != null) { supplyDrop.inventory.itemList.Clear(); foreach (Item item in createItemList()) { item.MoveToContainer(supplyDrop.inventory, -1, false); } } } } private List<Item> createItemList() { List<Item> itemList = new List<Item>(); Item i1 = ItemManager.CreateByName("explosive.timed", 2); Item i2 = ItemManager.CreateByName("rifle_ak", 1); itemList.Add(i1); itemList.Add(i2); return itemList; }
-
Some code I use in my plugins that may be helpful:
Code:private void InsertIntoInventory(ItemContainer container, Item item) { if (item != null && container != null && item.IsValid()) { if (!item.MoveToContainer(container)) { item.Drop(container.dropPosition, container.dropVelocity); } } }
-
The issue might have been that there'd have been more items in the container's inventory than it may actually hold. The slot count you see in game isn't always the actual maximum allowed. Noticed that because you are now initially clearing the container in your latest code snippet.