Hi Friends,

I am attempting to make my first real script from scratch. I am making a trading mechanism that allows a human player to tag items he or she wants in an NPCs pack, target items willing to give in the human's backpack, and then the script compares the value and if it is similar it will trade items from one pack to another. I got it working perfectly when the trade involved only one item; however, this presents obvious limitations. I have two things that I have been working with and if there is an existing script that handles this or if I am just wasting time let me know and I'll try something else. Thanks!

1. On lines 39 and 40 I have two lists to find items in both backpack's (NPC and Player). However "parent.AddtoBackpack(giveItems);" and "from.AddToBackpack(takeItems);" do not work because they are still lists.

2. How to I use lists to retrieve a new property value I created in item.cs (int ItemValue)? Once I figure this out I can insert a conditional operator before moving the items.

Code:
 public class ItemTrade : Target
        {
            public ItemTrade() : base(2, false, TargetFlags.None) { }

            protected override void OnTarget(Mobile from, object targeted)
            {
                if (targeted is Item)
                {
                    ((Item)targeted).TradeItem = !((Item)targeted).TradeItem;
                    from.Target = new ItemTrade();
                }

                if (targeted is Mobile)
                {                
                    Mobile parent = null;
                    parent = ((Item)targeted).RootParent as Mobile;
                    Container pack = parent.Backpack;
                    List<Item> takeItems = parent.Backpack.FindItemsByType<Item>();

                    foreach (Item t in takeItems)
                    {
                        if(t.TradeItem == true)
                        {
                            takeItems.Add(t);
                        }
                    
                    }                

                    List<Item> giveItems = from.Backpack.FindItemsByType<Item>();
                 
                    foreach (Item g in giveItems)
                    {
                        if (g.TradeItem == true)
                        {
                            giveItems.Add(g);
                        }
                    }

                    parent.AddtoBackpack(giveItems);
                    from.AddToBackpack(takeItems);
                }
            }
        }
 
Really cool concept. I like it!

You'll have to use a loop to add all of the items in the list to the new parent container. Example:
Code:
foreach(Item item in giveItems)
{
  parent.AddToBackpack(item);
}
foreach(Item item in takeItems)
{
  from.AddToBackpack(item);
}

As for #2, please do not add a property to Item.cs unless you don't need to serialize it. Trust me, you'll thank yourself later. Here is the pattern we typically use:
Code:
// Item.cs
public virtual int ItemValue { get { return 0; } }

// SomeOtherItem.cs
public override int ItemValue { get { return 100; } }
 
Back