RunUO Community

This is a sample guest message. Register a free account today to become a member! Once signed in, you'll be able to participate on this site by adding your own topics and posts, as well as connect with other members through your own private inbox!

Container.FindItemsByType

Status
Not open for further replies.

MarciXs

Sorceror
Container.FindItemsByType

Code:
private static void RecurseFindItemsByType( Item current, Type type, bool recurse, List<Item> list )
		{
			if ( current != null && current.Items.Count > 0 )
			{
				List<Item> items = current.Items;

				for ( int i = 0; i < items.Count; ++i )
				{
					Item item = items[i];

					[COLOR="Red"]if ( type.IsAssignableFrom( item.GetType() ) )/[/COLOR]/ item.GetType().IsAssignableFrom( type ) )
						list.Add( item );

					if ( recurse && item is Container )
						RecurseFindItemsByType( item, type, recurse, list );
				}
			}
		}
The area in red. You may wonder what's the problem?
Well, let's assume we have a class A;
public class A{} public class B : A {}
now typeof(A).IsAssignableFrom(typeof(B)) would return true.
however, typeof(B).IsAssignableFrom(typeof(A)) would return false.
So you wonder where's the problem? Well if you had a class SuperArrow that you derrived from Arrow which you would normally do. If you had a script that would look for Arrows, hence FindItemsByType(Consume method uses it by the way) then because Arrow isn't abstract the Arrow can be assigned from SuperArrow thus you might consume not just Arrows but also SuperArrows.

So to put in an example.
RecurseFindItemsByType(...,typeof(Arrow)
type.IsAssignableFrom( item.GetType()
typeof(Arrow).IsAssignableFrom(typeof(SuperArrow)); // Will return true.Since SuperArrow derives from Arrow aka the A,B example. Clearly if you'll look for SuperArrows it won't return Arrows.

So the modification is rather simple.
if(item.GetType() == type)

and saves time for doing this
Item i = container.FindItemByItem(typeof(SuperArrow));
if(i != null && i.GetType() == typeof(SuperArrow))
// do whatever

Hope it makes sense, I did try to explain as detailed as possible.
 
the problem with that is, that many times you want the sub stuff to be found in the search

i.e. if searching for arrows of anytype then yu use that methoed and it gets arrows and supper arrows, like it should

but your methoed would only return arrows

many times you want anything in the group to be returned, like food for example, or any type of log

so it you want to restrict it to just a certain item type only and not sub type, then use your method in the script itself when looking, instead of using the core method
 

Jeff

Lord
This modification is pointless.... now if you want to shoot some arrows, you have to check for EVERY type of arrow in the Bow script...
Code:
if(FindItemsByType(typeof(SuperArrow))...
...
else if (FindItemsByType(typeof(Arrow))
...
 

MarciXs

Sorceror
Jeff;865940 said:
This modification is pointless.... now if you want to shoot some arrows, you have to check for EVERY type of arrow in the Bow script...
Code:
if(FindItemsByType(typeof(SuperArrow))...
...
else if (FindItemsByType(typeof(Arrow))
...

Why?...
Currently unless you mod your base bows, they would shoot whatever Arrows you had derived from Arrow class the player had.
There's a Type AmmoType property in the BaseRanged, which it uses to find the ammo... so I can't see the problem that you mentioned ... the only problem that I see is that If you had IceBow and IceArrow, while IceBow would shoot as it is, the IceArrow if the AmmoType is set, while with simple bow you would be able to shoot not just Arrow but also IceArrow.
 
the problem comes in like i had said

for example, you wanrt to craft some dough, but you need liquid (any type works)
all you have to do is put in basebeverage and any beverage would work
if it was your method, you would have to list to make dough fro each type of beverage there is seperately

when scripts check to see if you have a type of shield or a type of weapon or basearmor
each and everyone would have to be modified to check for each and every sub type then
 

Jeff

Lord
MarciXs;866144 said:
Why?...
Currently unless you mod your base bows, they would shoot whatever Arrows you had derived from Arrow class the player had.
There's a Type AmmoType property in the BaseRanged, which it uses to find the ammo... so I can't see the problem that you mentioned ... the only problem that I see is that If you had IceBow and IceArrow, while IceBow would shoot as it is, the IceArrow if the AmmoType is set, while with simple bow you would be able to shoot not just Arrow but also IceArrow.

Why can't you just admit there is an inherent problem in what you proposed? This modification would break ALOT of peoples code, it will never get put into RunUO... face the facts man. You always propose these hair brained ideas... leave it be. If you can't live without the change, use it for you code, but this thread is done.
 
Status
Not open for further replies.
Top