Remove water from a jug?
Hi,
Any code snippet to remove water from a jug?
or is the best way just:

Srcitem.contents.AddItem(ItemManager.FindItemDefinition(-1779180711), (-count));
I would recommend finding the existing item and reducing/removing it. Something like the following. Not tested.

private void ReduceJugWater(Item waterJug, int removeAmount)
{
	var waterItem = waterJug.contents.FindItemByItemID(waterJug.contents.onlyAllowedItem.itemid);
	if (waterItem != null) 
		waterItem.UseItem(removeAmount);
}​

Not sure if you can store salt water in them so onlyAllowedItem may not work or may give you a different water type than what is in the container. There are a few other ways to get the water item such as looping through contents to find the first item since afaik there can be only one item in a jug.

Thanks....
I had been thru the salt water contamination routines..... looking for pointers..
I already had an
"foreach (var itemContains in Srcitem.contents.itemList)"

that's why I think my  "negation" works, in that they are iterating round contents and doing additions.

That's the problem with all this, is trying to figure out where stuff is and how they are doing it.
Someone sat down and wrote something..... then it got patched, changed,re-organised & finally released.


I had tried "reducing it" or even emptyingit and refilling it, nether of which work when called in same routine.
I had deleberatly avoided killing the object & regenerating it with a new base amount due to the impact of continually creating objects that the system then has to despose of.
I recommend not interacting with itemList directly. Instead, use a for loop from 0 to contents.capacity and go a GetSlot(index) call at each iteration. That is the way the game code usually does it. Removing an item from a list while iterating through it will also not work with foreach, but you can do it with a for loop as long as you account for the size changing mid loop.

If I recall correctly, to remove items instantly from a container, you have to call RemoveFromContainer() in addition to Remove(). Assuming there's only one item, you could probably do something as simple as the following.

var waterItem = waterJug.contents.GetSlot(0);
if (waterItem != null)
{
    waterItem.RemoveFromContainer();
    waterItem.Remove();
}​
ok.. i got it all figured out......
Christ...... what a rigmarole......

ill take a  look at your second section code.. in a few hours when i recover...  I need to play test it.

but what i found, was that  using  modfires on the parent object has all sorts of side effects...  like "1" keep being added   since it does not like "empty" contents.

But anyway thanks for takin the time......

Merged post

yep...
 I preferred the :
.contents.GetSlot(0);​

 

Just I was unsure of the rust system & if it utilised the other slots for water....., that's why I ended up itterating a list.
But as you say, it's a damned risky business playing with internal lists, in a multi threadded app.

anyway again for your help....