Chucl

Member
ServUO Version
Publish Unknown
Ultima Expansion
None
Hello so i have this method:

What it does:
it looks in a range of 300 for Corpses then attach an XmlValue attachment to ALL of them


What i would like it to do;
Look in a range of 300 for Corpses then attach the XmlValue attachment to ONE corpse randomly, not all of them
Method:
Point3D location2 = this.Location;
      
                       Map map = this.Map;
                    List<Item> itemsFound = new List<Item>();
              
                    foreach (Item item in this.Map.GetItemsInRange(location2, 300))
                    {
                        if (item is Corpse)
                            itemsFound.Add(item);
                    }
              
                    for (int j = itemsFound.Count - 1; j >= 0; --j)
                    {
                          XmlAttach.AttachTo(itemsFound[j], new XmlValue("test",100));
                         }
                    }
 
Last edited:
C#:
int corpsesCount = itemsFound.Count;

if(corpsesCount > 0)
{
    Item corpse = itemsFound[Utility.Random(corpsesCount)];
    XmlAttach.AttachTo(corpse, new XmlValue("test",100));    
}
 
Last edited:
Hello again i tried your method and it works, now im trying to make a Random Xmlspawner respawn, it throws an error


C#:
   List<Item> ToShow = new List<Item>();
 
            foreach (Item item in World.Items.Values)
            {
                if (item != null && item is XmlSpawner)
                  {
               
                     ToShow.Add(item);
                    }                                              
                                                                   
            }


             foreach (Item i in ToShow)
            {
                  int corpsesCount = ToShow.Count;
 
                    if(corpsesCount > 0)
                       {
                         
               Item xml_item2 = ToShow[Utility.Random(corpsesCount - 1)];
     
                        xml_item2.Respawn();

                       }
           
            }


CS1061: Line 1084: 'Server.Item' does not contain a definition for 'Respawn' and no extension method 'Respawn' accep
ting a first argument of type 'Server.Item' could be found (are you missing a using directive or an assembly reference?)


Since it was giving me an error i wanted to test the method so i changed:

xml_item2.Respawn(); to xml_item2.ItemID = 7800

And it changes the itemid of 3 or 4 xmlspawner to 7800 instead of randomly just one, why is that?



xmlspawners.png
 
Last edited:
You need to convert the type before use its methods. Change the:
C#:
Item xml_item2 = ToShow[Utility.Random(corpsesCount - 1)];
to
C#:
XmlSpawner xml_item2 = ToShow[Utility.Random(corpsesCount - 1)] as XmlSpawner;
Or just create the list of List<XmlSpawner> instead of List<Item>
 
You need to convert the type before use its methods. Change the:
C#:
Item xml_item2 = ToShow[Utility.Random(corpsesCount - 1)];
to
C#:
XmlSpawner xml_item2 = ToShow[Utility.Random(corpsesCount - 1)] as XmlSpawner;
Or just create the list of List<XmlSpawner> instead of List<Item>
Thanks that works, but yeah it picks 4 spawners (2 of them twice), instead of just one:


aaayyyy.png
 
You don't need second "foreach" for one random value.
Just use next:
C#:
                    if(ToShow.Count > 0)
                       {
                      XmlSpawner xml_item2 = ToShow[Utility.Random(ToShow.Count - 1)] as XmlSpawner;
  
                        xml_item2.Respawn();
                       }
 
You don't need second "foreach" for one random value.
Just use next:
C#:
                    if(ToShow.Count > 0)
                       {
                      XmlSpawner xml_item2 = ToShow[Utility.Random(ToShow.Count - 1)] as XmlSpawner;
 
                        xml_item2.Respawn();
                       }
that was the issue yes, thank you so much
 

Active Shards

Donations

Total amount
$0.00
Goal
$1,000.00
Back