- Home /
I'm not fully understanding the concept of "interfaces" and what's up with the IEnumerator interface...
So I'm new with C# and unity and I thought it would be a good idea to take a deep and hard look at every single one of the scripting tutorials... and I'm kind of stuck on this one: http://unity3d.com/learn/tutorials/modules/intermediate/scripting/interfaces.
From what I can understand, interfaces are kind of like "templates" that you can make to "force" your classes to have specific variables and/or functions... for example, I can have two classes, say class Enemy and class NPC, and I want both of them to be able to be killed, so I can create an interface to "force" both of them to have what is necessary for it, kind of like this:
public interface IKillable
{
public float life;
public void Die();
}
public class Enemy: Ikillable
{
public float life{ } //forced to put this
public void Die(){ } //forced to put this
}
public class NPC: Ikillable
{
public float life{ } //forced to put this
public void Die(){ } //forced to put this
}
Everything great thus far...... BUT.... what's up with "IEnumerator" then? Keep in mind I understand perfectly how to use Coroutines and the yield statement but what I don't understand is how an interface can be a return type for a function, which I assume is what happens when using coroutines. If having a function return an "IEnumerator" is possible, is it also possible for me to create a function somewhere that returns my "Ikillable" interface? if so... what would it be returning or what would it be its purpose? since my interface only has empty shells of variables and methods?
I know even the question itself is confusing as hell but I hope I was clear with what my doubt is... thanks in advance for any help with this! :)
for viewing all the tutorials and asking questions as you get stuck. The quality of this site would be dramatically improved if everyone did that.
Answer by Kiwasi · Nov 22, 2014 at 04:41 AM
Welcome to polymorphism. A coroutine can return any object that implements the IEnumerator interface. IEnumerator is kind of a special case, so I'll focus on Ikillable.
It would be perfectly possible for you to define a method that returns an Ikillable. This would mean your method can return any object that implements Ikillable. You can then create a variable of the Ikillable type to store the variable. You can call the methods that belong to Ikillable on it. All of this without ever knowing what the actual object was.
Its very neat, very powerful, and one of the fundamental pillars of object orientated programming.
wow that was fast! thank you so much!... however, if I understand you correctly, it means I could do something akin to this:
Ikillable my$$anonymous$$illableVariable = new Ikillable();
my$$anonymous$$illableVariable.Die();
my$$anonymous$$illableVariable.life;
but how would the program know which Die() function or life variable I'm refering to? weather it is the one in the Enemy class or the NPC class? thank you sooo much! and sorry for the noob questions :/
You can't call new on an interface. New invokes the constructor. Interfaces cannot implement any methods, including a constructor.
You could do this. (Pseudo code)
public class Enemy: Ikillabable {...}
// In another class
Ikillable my$$anonymous$$illableVariable = new Enemy;
my$$anonymous$$illableVariable.Die();
my$$anonymous$$illableVariable.life;
oooooooooohhhhhhhh ... thank you!... but... bear with me please :( ... does that mean this would be valid? :
Ikillable SomeFunction()
{
return Enemy;
}
Enemy myVar = SomeFunction(); //is this even syntactically correct? :S
myVar.life;
also I assume the "yield" statement is some kind of "return object-of-a-class-that-uses-the-IEnumerator-interface" statement then? again, thanks a lot, being self-taught can be a pain when no one is around to ask questions :/
They syntax is correct, but it would not compile. The reason is because the compiler cannot guarantee that the value returned by SomeFunction is an Enemy.
The compiler knows that every Enemy is an Ikillable. However not every Ikillable is an enemy. You could get around this by casting, as follows. However casting in this manner is typically not good practice.
Ikillable SomeFunction() {
return Enemy;
}
Enemy myVar = (Enemy)SomeFunction();
Your answer
Follow this Question
Related Questions
Calling IEnumerators 1 Answer
Freaky - can't return; out of an IEnumerator 1 Answer
Respawn Time 1 Answer
trying to run a co routine function gets error 1 Answer
Why Won't My Coroutine Yield? 2 Answers