TheDoc

Member
Shouldn't list.Clear() be safe to use to only remove item names from a list and not the actual item from game?

For example calling it here causes all items in pack to be deleted..

Code:
List<Server.Item> items = this.Backpack.Items;
						
                        for ( int i = items.Count - 1; i >= 0; --i )
                        {
							
                            Item item = (Item)items[i];

                            if ( item != null && item is BaseWeapon || item is BaseClothing || item is BaseArmor || item is BaseJewel )
                            {
                                Backpack.DropItem( item );
                                this.EquipItem(item);
								
                            }
							
                        }
						items.Clear();
                    }
 
It doesn't delete them, they just become leaked, their Parents are not properly updated and the container contents packet no longer finds them in the container Items list, so it can't update them to your client.

You don't need to manipulate any Item.Items list directly, it is strongly advised that you don't.

Your code will actually automatically remove the items from the list when they are moved to another container and the proper procedure is done by the core to update the item's Parent and send you the new update packets. This is why the for-loop has to be iterated in reverse, to avoid collection modified exceptions from the items being moved.

Remove the list.Clear()
 
My issue is I call this code repeatedly to make a mobile change into random gear, but the list doesn't change so i wad trying to find a way to clear the list each call.
 
If you don't want to change the way your code works, then you'll need to initialize a new list;

List<Server.Item> items = new List<Server.Item>( this.Backpack.Items );

That should work just fine then.

Alternatively, I have something I can share with you that has the random equipping ability you can reference... This is taken from a BaseCreature based NPC script context. It switches weapons based on the current AI.

Code:
		#region Equipment Switching
		protected virtual void Undress(params Layer[] layers)
		{
			Items.Not(i => i == null || i.Deleted || !i.Movable || i == Backpack || i == FindBankNoCreate() || i == Mount)
				 .Where(i => _EquipLayers.Contains(i.Layer))
				 .Where(item => layers == null || layers.Length == 0 || layers.Contains(item.Layer))
				 .ForEach(Backpack.DropItem);
		}

		protected virtual void SwitchEquipment()
		{
			if (Deleted || Map == null || Map == Map.Internal || Location == Point3D.Zero)
			{
				return;
			}

			Undress(Layer.OneHanded, Layer.TwoHanded);

			Item weapon;
			Item shield = null;

			switch (AI)
			{
				case AIType.AI_Archer:
				{
					weapon = Backpack.FindItemsByType<BaseRanged>(true, w => w != null && !w.Deleted && w.CanEquip(this)).GetRandom();

					if (weapon != null)
					{
						var ranged = (BaseRanged)weapon;

						if (ranged.AmmoType != null)
						{
							var ammo = Backpack.FindItemByType(ranged.AmmoType, true);

							if (ammo == null || ammo.Deleted || ammo.Amount <= 0)
							{
								ammo = ranged.AmmoType.CreateInstanceSafe<Item>();

								if (ammo != null)
								{
									ammo.Amount = 100;
									ammo.LootType = LootType.Blessed;

									Backpack.DropItem(ammo);
								}
							}
						}
					}
				}
					break;
				case AIType.AI_Mage:
				{
					weapon = Backpack.FindItemsByType<Spellbook>(true, w => w != null && !w.Deleted && w.CanEquip(this)).GetRandom() ??
							 new Spellbook(Int64.MaxValue)
							 {
								 LootType = LootType.Blessed
							 };

					var regs = Backpack.FindItemByType<BagOfReagents>(true);

					if (regs == null || regs.Deleted || regs.Amount <= 0 || regs.Items.Count <= 0)
					{
						regs = new BagOfReagents(100)
						{
							LootType = LootType.Blessed
						};

						Backpack.DropItem(regs);
					}
				}
					break;
				case AIType.AI_Melee:
				{
					weapon =
						Backpack.FindItemsByType<BaseMeleeWeapon>(true, w => w != null && !w.Deleted && w.CanEquip(this)).GetRandom();

					if (weapon != null && weapon.Layer != Layer.TwoHanded)
					{
						shield = Backpack.FindItemsByType<BaseShield>(true, s => s != null && !s.Deleted && s.CanEquip(this)).GetRandom();
					}
				}
					break;
				default:
					weapon = Backpack.FindItemsByType<BaseWeapon>(true, w => w != null && !w.Deleted && w.CanEquip(this)).GetRandom();
					break;
			}

			if (weapon != null)
			{
				EquipItem(weapon);
			}

			if (shield != null)
			{
				EquipItem(shield);
			}

			Undress(_EquipLayers);

			var equip = new List<Item>(_EquipLayers.Length);
			var packItems = Backpack.FindItemsByType<Item>(true, item => item != null && !item.Deleted && item.CanEquip(this));

			foreach (var item in _EquipLayers.Select(l => FindItemOnLayer(l) ?? packItems.GetRandom()))
			{
				equip.AddOrReplace(item);
				packItems.Remove(item);
			}

			packItems.Free(true);

			foreach (var item in equip.Where(item => item != null && !item.IsEquipped()))
			{
				EquipItem(item);
			}

			equip.Free(true);
		}
		#endregion
 

Active Shards

Donations

Total amount
$0.00
Goal
$1,000.00
Back