Interfaces

Hi everyone,

I’m hoping someone can help me understand interfaces a little better by helping me with this example:

Let’s say we have the following:

IMove, with float Speed{get; } as its only property

PlayerMover: implements the interface, and passes a value to Speed

CharacterAnimation: implements the interface, and receives that value to drive the animation - great! All is working…but

What happens if there is another class that implements the IMove interface that also sets a value to the Speed parameter? How do classes Get the correct value if two or more classes are setting values to the same Interface parameter?

If I’m understanding your example correctly, PlayerMover is implementing the IMove interface, which means that it is implementing the property float Speed {get {return speed;}};
PlayerMover sets the speed (presumably based on input), and now property Speed is available for anybody looking at PlayerMover.

public class PlayerMover: IMove
{
     float speed;
     public float Speed {get => speed;}

    void Update
    {
        speed = whatever calculations you need to make to get the speed
    }
}

CharacterAnimation is getting the value from PlayerMover, so not implementing the interface, but using the interface to get the information…

IMove mover;
void Awake 
{
    IMove mover = GetComponent<IMove>(); 
}
void Update
{
    Set animation based on mover.Speed;
}

So the player would be set up with the PlayerMover and the CharacterAnimation components…
Your question is about a second class implementing the IMove interface, so let’s look at doing just that. We’re going to create a new class EnemyMover

public class EnemyMover: IMover
{
     float speed;
     public float Speed {get => speed;}

    void Update
    {
        speed = ai based calculations to derive speed
    }
}

Now this class, along with your CharacterAnimation class would go on the enemy.
Note that the CharacterAnimation class requires no changes, because EnemyMover is also an IMove, as far as that class is concerned there is no difference between a PlayerMover and an EnemyMover… it finds the EnemyMover in Awake, and gets the speed in Update to do its magic to animate the character.

The interface itself does not hold the reference to Speed. Each IMove based component is solely responsible for providing that value. IMove is just a way of communicating between components. Since all we’re interested in from the IMove components is the Speed, CharacterAnimation doesn’t have to care “was this a player? Was it an enemy?” The actual setting is in PlayerMover or EnemyMover, not in the interface itself.

Without the interface, CharacterAnimation would have to look like this:

public class CharacterAnimation
{
     PlayerMover player;
     EnemyMover enemy;
     void Awake()
     {
         player = GetComponent<PlayerMover>();
         enemy = GetComponent<EnemyMover>();
     }
    void Update
    {
        float speed = 0;
        if(player) speed = player.Speed 
          else if(enemy) speed = enemy.Speed;
        do all my fun logic with speed;
    }
}

While this doesn’t sound too horrible, this is just a small example… Interfaces allow us to have many different classes, all unrelated to one another except that they expose the required properties or methods in the interface. How those properties and methods are implemented is up to each class as it goes along, but any class that calls on that interface can be assured those methods will exist.

Thank you Brian!

This helped immensely, and made it “click”.

Seriously - I can’t thank you enough!

I completed the RPG course, and for whatever reason it just went over my head. You broken it down perfectly.

Glad I could help. It’s one of those concepts that can’t be explained in a sentence, yet often is… With more practice, you’ll find lots of ways to use interfaces, one of the pillars of the SOLID concept.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms