Hi !
I've got some troubles by using a foreach loop and the player.inventory.GiveItem() function.
Here's my foreach loop :
"Loot" is a storageContainer.Code:foreach (Item item in loot.inventory.itemList) { SendReply(player, "next item : "); player.inventory.GiveItem(item); SendReply(player, item.info.displayName.english); }
The purpose of this loop is to give back all items to player before the box that contains them is destroyed.
I don't understand why but each time this loop is called she's stuck right after the first item.
I.E : I put an Assault Rifle and a Water Bottle in the box, I click on the button to destroy the box, the result in the chat will be :
next item :
Assault Rifle
And that's all, no water bottle and the code I got after this loop isn't read too.
I got others loop like this one but whitout the player.inventory.GiveItem() function and they work well btw.
Do you know what could be the root of this issue ?
Solved Foreach loop stuck by player.inventory.GiveItem()
Discussion in 'Rust Development' started by Pant0uflard, Jul 6, 2016.
-
It's most likely because the list is being modifying while it's looping. (The item is being removed from the container and put in another) Try either:
Or:Code:foreach (Item item in loot.inventory.itemList.ToArray())
I'd recommend the latter, since it's probably faster, but both should work.Code:for (int i = 0; i < loot.inventory.itemList.Count; i++) { var item = loot.inventory.itemList[i]; } -
Should have think about the modified list, it's obvious now ...
Well, the first one work perfectly but the second one still give me the same issue so I'll keep the first one ^^
Thanks a lot !
