ServUO Version
Publish Unknown
Ultima Expansion
None
I'm trying to create a simple function to remove items at a location, but the server just crashes. If I remove the 't.Delete();' line, everything else works as intended.

Code:
IPooledEnumerable items = map.GetItemsInRange( new Point3D( x, y, 0 ), 0 );

                foreach ( Item t in items )
                {
                    t.Delete();
                }

The Crash Message:

Crash:
Exception:
System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
   at System.ThrowHelper.ThrowArgumentOutOfRangeException()
   at System.Collections.Generic.List`1.get_Item(Int32 index)
   at System.Collections.Generic.List`1.System.Collections.IList.get_Item(Int32 index)
   at Server.Map.TypedEnumerator.MoveNext()
   at Server.AVGenerator.AddItem(Boolean wipe, Int32 itemid, Int32 x, Int32 y, Int32 z, Map map, String name, Int32 hue)
   at Server.AVGenerator.AVGenerate()
   at Server.Commands.CommandSystem.Handle(Mobile from, String text, MessageType type)
   at Server.Mobile.DoSpeech(String text, Int32[] keywords, MessageType type, Int32 hue)
   at Server.Mobiles.PlayerMobile.DoSpeech(String text, Int32[] keywords, MessageType type, Int32 hue)
   at Server.Network.PacketHandlers.UnicodeSpeech(NetState state, PacketReader pvSrc)
   at Server.Network.MessagePump.HandleReceive(NetState ns)
   at Server.Network.MessagePump.Slice()
   at Server.Core.Main(String[] args)

EDIT:
You may need the entire script come to think of it...

Script:
using System;
using Server;
using Server.Commands;
using Server.Items;
using Server.Mobiles;

namespace Server
{
    public class AVGenerator
    {
        public static void Initialize()
        {
            CommandSystem.Register( "AVGen", AccessLevel.Administrator, new CommandEventHandler( AVGen_OnCommand ) );
        }

        [Usage( "AVGen" )]
        [Description( "Generates AV World" )]
        public static void AVGen_OnCommand( CommandEventArgs e )
        {
            AVGenerate();
        }

        private static Map m_Map;

        public static void AVGenerate()
        {
            World.Broadcast( 0x35, true, "Generating AV World, please wait..." );

            Network.NetState.FlushAll();
            Network.NetState.Pause();

            AddItem( true, 1801, 5951, 343, -22, Map.Trammel, "", 0);

            Network.NetState.Resume();

            World.Broadcast( 0x43, true, "AV World generation complete." );
        }
        private static void AddItem( bool wipe, int itemid, int x, int y, int z, Map map, string name, int hue )
        {
            if(wipe)
            {
                IPooledEnumerable items = map.GetItemsInRange( new Point3D( x, y, 0 ), 0 );

                foreach ( Item t in items )
                {
                    t.Delete();
                    World.Broadcast( 1150, true, "TM: Item was detected!" );
                }
            }
            Item item = new GreySlateFloor();
            item.ItemID = itemid;
            if(name != "")
                { item.Name = name; }
            if(hue != 0)
                { item.Hue = hue; }

            item.MoveToWorld(new Point3D( x, y, z ), map);
            
        }

    }
}
 
Last edited:
"foreach" is used only for read-only operations. You cannot modify the enumeration during this time.
To modify collection you can use "for" operator.

Example #1:
C#:
IPooledEnumerable items = map.GetItemsInRange(new Point3D(x, y, 0), 0);
var itemList = items.ToList(); // Convert to a list to avoid issues during deletion

for (int i = 0; i < itemList.Count; i++)
{
    itemList[i].Delete();
}

items.Free();
Example #2:
C#:
IPooledEnumerable items = map.GetItemsInRange(new Point3D(x, y, 0), 0);
List<Item> itemsToDelete = new List<Item>();

foreach (Item t in items)
{
    itemsToDelete.Add(t);
}

// Now iterate over the list of items to delete
foreach (Item t in itemsToDelete)
{
    t.Delete();
}

items.Free();
 
What was said about "for" and "foreach" did make sense, ty for educating it. However, Example #1 came back with errors which are listed below:
Error:
Errors:
 + Misc/Custom/AVGenerator.cs:
    CS0246: Line 297: The type or namespace name 'var' could not be found (are you missing a using directive or an assembly reference?)
    CS0117: Line 297: 'Server.IPooledEnumerable' does not contain a definition for 'ToList'
    CS0021: Line 300: Cannot apply indexing with [] to an expression of type 'var'

Doesn't have a definition for 'Count' either...

Section:
IPooledEnumerable items = map.GetItemsInRange( new Point3D( x, y, 0 ), 0 );
    var itemList = items.ToList();
    for ( int i = 0; i < itemList.Count; i++ )
    {
        itemList[i].Delete();
        World.Broadcast( 1150, true, "TM: Item was detected!" );
    }
    items.Free();

UPDATE:
I was able to get Example #2 working by adding "using System.Collections.Generic;" to the script. But I do not know what I need to add for Example #1 to work
 
Last edited:
var is a newer c# syntax, it is a variable that assigns based on the data type, so it can be a string, int etc etc so in the first example it is being used in place of List, so replace with the explicit variable type! Since you are using an older framework, you also don't have Generics! This is the main problem with using older versions of ServUO or RunUO.

The solution can be done in another way, as mentioned, you can't modify the collection while cycling through it, as if you remove one item in the list, the list size changes, which caused the out of range error. So best to first run over the list and grab the handle of each items you wish to delete, then delete them with a for in reverse!
 
Ah alright, so example #1 can't work because I'm using an older version of RunUO. "var" should be "List<Item>" instead, but doesn't matter because "ToList" and "Count" also isn't available to use in older versions?

Luckily, example #2 does work on old versions and is the work around in this case. Thank you for educating me further, it does make sense to me, basically, can't modify something that is still being used by the code!
 
Right, and since you are on RunUO, you'll have to do the old way, loop through the array, grab ref to what you want to delete, than outside loop, delete and than repeat for additional items to delete! Basic Solution!
 

Active Shards

Donations

Total amount
$0.00
Goal
$500.00
Back