Quick answer: Godot 4 C# enumerating Godot.Collections.Array throwing 'collection modified' when items are removed during iteration? Iterate over a snapshot or build a removal list.
Inventory cleanup loop: foreach item, remove if expired. Exception mid-loop; items partially removed.
Snapshot before iterating
foreach (var item in items.ToArray()) {
if (expired) items.Remove(item);
}ToArray materializes; iteration is stable; removal targets the original.
Or two-pass
First pass: collect removable items. Second pass: remove them. Two simple loops.
Use LINQ Where + ToList
Functional style. items = items.Where(i => !expired).ToList(). Clean; allocates.
“Mutate-while-iterate is universal Bad Idea. Godot collections inherit it.”
Establish 'never mutate while iterating' as a code review rule. The exception is the cheapest signal; tools catch it.