What are interfaces in C-sharp

Interfaces, in c-sharp syntax not to be confused with anything the user interacts with, are one of the most important concepts in C# and programming in general. They’re like a contract between you, the programmer, and your code. And just like in any other contract, both parties have to follow the rules or things can get messy.

Think of an interface like a blueprint for a house. Just like a blueprint, an interface specifies some of the principles a house must adhere to ie there must be a door, there must be walls and roof. This makes it possible for anyone going to a house, to assume that there is a door. But it doesn’t specify how to build it. That’s up to the developer to decide.

In C#, an interface defines a set of methods, properties, and events that a class shall implement. This ensures that classes that implement the interface behave in a certain way. When you define an interface, you’re essentially saying, “Any class that implements this interface shall have these methods, properties, and events.”

Here’s an example of a simple interface in C#:

interface IClickable
{
    void Interact();
}

And here’s how you would implement the interface in a class:

class Clickable : IClickable
{
    public void Interact()
    {
        Console.WriteLine("Doing something");
    }
}

The Clickable class implements the IClickable interface. This means that it must have the Interact method. If it doesn’t, you’ll get a compile-time error.

Interfaces are great for a number of reasons. They help to enforce a common behavior across classes. They also make it easy to change the implementation of a class without affecting the code that uses it. And they make it easier to write code that works with multiple classes, even if those classes have different implementations.

In short, interfaces are like a trusty sidekick to your C# code. They keep your code organized and help ensure that everything works as it should. So, the next time you’re writing code in C#, don’t forget about your trusty sidekick, the interface!

Typical usecases for an interface in game development

Let’s talk about how they can be used in game development.

One of the ways I use interfaces in game development is when delivering damage to objects. Often in computer games, you end up damaging a variety of objects eg enemies, vehicles and crates. You could argue that these objects have several properties in common. One could be damage. However, when we hit the object, we dont want whatever weapon we use to register the collision find out if the collider hit belongs to an enemy or vehicle or a chest. What happens when we scale the game and introduce new objects which can be damaged ie doors. Now we would have to go through all weapons and implement a way to see if the collision is a door.

In this example it is much more convenient to simply check to see if the collider the weapon hits implements the IDamagable, and if so, apply TakeDamage(). Whatever the object, we know that it implements a TakeDamage method through the interface.

What happens with the enemy, the vehicle, crate or door when the TakeDamage() is called on them, is up to the particular class, perhaps a crate calls a destroy animation and spawns the contents of the crate, perhaps damaging an enemy will trigger the enemy ai to alter behaviour, which is not neccesary for the crate.

This is an example of what the code for this scenario might look like:

interface IDamagable
{
    public void TakeDamage(int amount);
}

class Skeleton : IDamagable
{
    int hitpoints = 10;
    
    public void TakeDamage(int amount)
    {
        if(amount > hitpoints)
        {
            Die();
        }else{
            hitpoints -= amount;
            Console.WriteLine("Skeleton took damage");
        }
    }
}

class Crate : IDamagable
{
    public void TakeDamage(int amount)
    {
        CallDestroyAnimation();
        SpawnLoot();
        Console.WriteLine("Crate took damage");
    }
}

Another use case for interfaces in game development is for objects that can be picked up or collected. For example, let’s say you have various types of collectible items in your game, such as coins, gems, and power-ups. You could create an interface called ICollectible with a Collect method. Then, you could create classes for each type of collectible that implement the ICollectible interface. This way, the code that handles collecting items doesn’t need to know the specifics of each type of collectible, it only needs to know that it can call the Collect method on any object that implements the ICollectible interface.

Interfaces are a powerful tool in game development and can make your code more organized and easier to maintain. Just remember, they are like a blueprint for your code, so make sure to follow the rules of the contract and things will go smoothly. Happy coding!