Full Version : Check all attachments of one type?
xmlspawner >>Q&A >>Check all attachments of one type?
<< Prev | Next >>
Lord Hog Fred- 10-24-2006
Can I make a script perform a check on all the attachments of a specific type on a player?
Say I want to check every XmlData attachment on a player can I tell a script to do that?
Thanks ,
ArteGordon- 10-24-2006
ArrayList alist = XmlAttach.FindAttachments(from, typeof(XmlData));
will return a list containing all of the XmlData type attachments on the player 'from'.
Then you can go through the attachments like

CODE
foreach(XmlData x in alist) { if(x != null && !x.Deleted) { // do what you want
}
}

Lord Hog Fred- 10-24-2006
Ah ok, I thought it would have something to do with an arraylist somewhere.
Is there a way to scan for each attribute of the attachment such as the XmlData.Name?
ArteGordon- 10-24-2006

QUOTE (Lord Hog Fred @ October 24, 2006 04:46 pm)
Ah ok, I thought it would have something to do with an arraylist somewhere. Is there a way to scan for each attribute of the attachment such as the XmlData.Name?

ArrayList alist = XmlAttach.FindAttachments(from, typeof(XmlData),"thename");
These are the overloaded methods available

QUOTE
public static ArrayList FindAttachments(object o) public static ArrayList FindAttachments(object o, Type type) public static ArrayList FindAttachments(object o, Type type, string name)

Lord Hog Fred- 10-24-2006

QUOTE (ArteGordon @ October 24, 2006 09:05 pm)

QUOTE (Lord Hog Fred @ October 24, 2006 04:46 pm)
Ah ok, I thought it would have something to do with an arraylist somewhere. Is there a way to scan for each attribute of the attachment such as the XmlData.Name?

ArrayList alist = XmlAttach.FindAttachments(from, typeof(XmlData),"thename");
These are the overloaded methods available

QUOTE
public static ArrayList FindAttachments(object o) public static ArrayList FindAttachments(object o, Type type) public static ArrayList FindAttachments(object o, Type type, string name)

The code I'm trying to use is:

CODE
public override void OnDoubleClick( Mobile from ) { ArrayList recipelist = XmlAttach.FindAttachments(from, typeof(XmlRecipe), this.RecipeName, this.RecipeID ); if ( !IsChildOf( from.Backpack ) ) from.SendLocalizedMessage( 1042001 ); // That must be in your pack for you to use it.
else
{
foreach (XmlRecipe xmlrecipe in recipelist)
{
if (xmlrecipe.Name == this.RecipeName && xmlrecipe.ID == this.RecipeID)
{
from.SendMessage("You already know this recipe.");
}
else
{
XmlAttach.AttachTo( from, new XmlRecipe(this.RecipeName, this.RecipeID) );
from.SendMessage("You learn the recipe.");
Delete();
}
}
}
}

I'm also trying to use the code in CraftItem:

CODE
//Xml Recipes XmlRecipe xmlrecipe = (XmlRecipe)XmlAttach.FindAttachment(from, typeof(XmlRecipe)); //End //Xml Recipes if( this.Recipe == null || !(from is PlayerMobile) || (xmlrecipe.ID == this.Recipe.ID) || (xmlrecipe.ID == 9999999) )
//End

Just giving you an idea of what im trying to do.
ArteGordon- 10-24-2006
there is no overload of FindAttachments that will take that extra fourth argument, so you would do it like this

CODE
public override void OnDoubleClick(Mobile from) { if (!IsChildOf(from.Backpack)) from.SendLocalizedMessage(1042001); // That must be in your pack for you to use it. else
{
ArrayList recipelist = XmlAttach.FindAttachments(from, typeof(XmlRecipe), this.RecipeName);
bool found = false;
foreach (XmlRecipe xmlrecipe in recipelist)
{
if (xmlrecipe != null && !xmlrecipe.Deleted && xmlrecipe.ID == this.RecipeID)
{
from.SendMessage("You already know this recipe.");
found = true;
break;
}
}
if (!found)
{
XmlAttach.AttachTo(from, new XmlRecipe(this.RecipeName, this.RecipeID));
from.SendMessage("You learn the recipe.");
Delete();
}
}
}

you dont need to check the name in the foreach since you already restricted the attachments to those with that name in the FindAttachments call.
I also added the break in the list since once you have found one, there is no need to continue going through the rest of them.
Also, I think that you want to add the recipe if none of the existing recipe attachments match, so I changed the loop and test a bit.
Also note that when going through the list, you need to check to make sure that the attachment has not already been deleted.