So I’m trying to rely on interfaces instead of variables by checking which interfaces a class implements, but the problem that I’ve got is that I have to specify which interfaces to check if it has in order to make a comparison between two classes, if that makes any sense. More specifically, when I try to see if two classes inherit from the same interfaces, I have to type out every single interface two times like this “if (class1 is Iinterface1 && class2 is Iinterface1)”, instead I want to just use one method or something to find out which interfaces it inherit, and then check if those are the same as the other class. Does anyone know if what I’m describing exists, or if its possible to make one myself?
Sound so like you need to dive into reflection.
Thanks, saved me a few headaches! I tried to get the “getInterfaces” method to work earlier simply because its name sounded like what I wanted, but couldn’t get it to work for the life of me. I guess I’ve got a few videos and articles to watch and read in order to understand all of this
What is the use case for this?
Can you show us an example?
There may be a more elegant solution that doesn’t rely on testing if an object is something.
I have an ammo pouch, it can only be loaded up with items that uses the interfaces “Iammo” and “Ibullet”, so im trying to create a script that compares the requirements of the inventory (in this case ammo pouch) to the item being added, if those aren’t identical, it can’t be added to the inventory
Now I could just create a new string on the item, and call it something like “type” and say that it is “bullet ammo” and check that, but I think it would be better to just use the infrastructure I’ve already got, with interfaces describing almost every property of an item
I think you might be bending interfaces a little outside of what they are…
What’s the difference between IAmmo and IBullet? Do these interfaces have different methods/functions on them that make them distinct? Because your mention of adding a property to your item that just describes it as a bullet implies to me that IBullet solely exists to describe it as a bullet, and not uncover any actual methods/functions.
Also, I wouldn’t use a string if I decided to create the type as a property of the ammunition. I’d use an enum if I were to go a route of identifying things this way.
Something like this:
public class AmmunitionPouch
{
public AmmoType AcceptedTypes = AmmoType.Bullet | AmmoType.Shell;
private List<IAmmunition> _ammo = new List<IAmmunition>();
public bool AddAmmo(IAmmunition ammo)
{
if((ammo.Type & this.AcceptedTypes) != 0)
{
_ammo.Add(ammo);
return true;
}
else
{
return false;
}
}
}
public interface IAmmunition
{
AmmoType Type {get;}
float Range {get;}
float Damage {get;}
AmmoGauge Gauge {get;}
}
[System.Flags()]
public enum AmmoType
{
Bullet = 1,
Shell = 2,
Arrow = 4
}
I don’t know your exact situation though…
I’ll second this. Whenever the answer is reflection, the question is often wrong.
You can have a IBullet that isn’t an IAmmo? Seems a little strange.
Remember interfaces can implement other interfaces.
Well, I’m trying to make my inventory system as versatile as possible, and Ibullet basically just contains the dimensions of the bullet (9x19mm for example), while Iammo specifies its payload and weight, so I can create bullets by just using the two interfaces, and at the same time create things like arrows or rocks by just using the Iammo interface, since its doesn’t really need to know the dimensions of it, just the payload and weight.
Thanks for showing me enum, I will probably use it on either this or later on. The main problem with using enum though, would be that I would have to keep updating the enum list every time I added new interfaces to my items, which is why I think using reflection might be better in this case
Oh, thanks! Didn’t know that, will fix it right away
Yup. If IBullet is alway IAmmo then something like this may solve all your problems.
public interface IBullet : IAmmo {
// Now IBullet will also implement IAmmo.
}
This still seems off, interfaces should be adjective named and decribe what something does, and not what it is.
It works great for me though and I was recommended by another user to use this so I don’t see the drawbacks to using interfaces in this way, it’s really simple to use too
https://forum.unity3d.com/threads/unity-unique-items-in-list-management.441576/
EDIT: corrected grammar
interfaces being adjectives isn’t some hardline rule or anything.
Take the collection interfaces like IList and ICollection.
Or when you create a WCF service, you need to define an interface for your service… ISomeService.
Or IAsyncResult for threading tokens.
Interfaces are for defining the interface of things… and sometimes that thing is generalized as a noun.
Meh. Call them IUsableAsAmmo and IUsableAsBullets. Its just symantics.