MarciXs
Sorceror
Container.FindItemsByType
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.
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 );
}
}
}
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.