Should Know The Modular Design Pattern For Next Game

Photo by Raphael Koh on Unsplash

Should Know The Modular Design Pattern For Next Game

Game development contains mostly repetitive tasks. Generally, we have to do the same task to add a new building, gun, or anything into our game. So, if I say we can reduce these repetitive tasks while improving our code and graphic quality, hmm? This magic is called “Modular Design”. Today we will talk about what modular design is and how to apply it in game development.

What is Modular Design?

Modular design is a design pattern for preventing repetitive coding and reducing development and maintenance costs. The modular design aims to subdivide the system into smaller independent parts called modules. Each part is designed to do a certain task. This structure increases the reusability of your code and also decreases your debugging cost.

Lets’ give an example to understand this concept more clearly.

In our game, we have a player and many enemy objects. If you define a new base class for each object in this scenario, you will be probably stuck when you want to add a new feature for all enemies when you see a bug on one of the enemy objects. Let’s look closely at these two states.

  • Adding a new feature for all enemies: You have to copy and paste the code of the feature and implement the necessary points of the enemy object for each enemy object.

    • Time-consuming for developing

    • A lot of repetitive tasks

  • See a bug on one of the enemy objects: If this bug is on the core features of the enemies, you can worry at this point. Because you have to solve the bug and implement the solution for all enemy objects. If this bug is on a certain enemy type, solving the bug for this enemy type probably will be enough.

    • Time-consuming for debugging

    • A lot of repetitive tasks

Now, let’s use modular design to create our game.

Class Diagram for an example modular design

Figure 1: Class Diagram

As you can see in the diagram above, firstly we define an abstract or base class for all entities in our game such as player and enemy. This class has all the core features for our entities. Then, we define 2 new classes by inheriting our “Entity” class, Player and Enemy. These two classes have all the features of the Entity class with their unique features. This means you don’t have to implement the same features for Player and Enemy classes. They have already taken these implementations from the Entity class.

After defining our player features in the Player class and core enemy features in the Enemy class, we can define our enemy types by inheriting our “Enemy” class. Now, let’s look into the two states in our scenario again.

  • Adding a new feature for all enemies: Implementing the feature into the “Enemy” class will be enough. Thanks to the power of inheritance, our all enemies will have this feature automatically.

  • See a bug on one of the enemy objects: If this bug is on the core features of the enemies, solving the bug in the “Enemy” class probably will be enough. If this bug is on a certain enemy type, solving the bug for this enemy type probably will be enough.

Benefits of Modular Design

We need bricks to build a house. If bricks and cement have good quality, our house will be good and strong. Designing a modular structure is like building a house. Abstract classes are the brick and the core of our game. If they are stable and flexible, our game will be stable. Some benefits of using modular design are;

  • Reduces maintenance and development time

  • Reduces the number of repetitive tasks

  • You can create reusable code pieces and build more complex objects by using these code pieces.

  • Increases your code quality

How to use modular design in game development

The power of Modular design comes from Object Oriented Programming (OOP). The major point of this concept is the categorization of features properly. In this step, obeying some rules makes it easier.

  • Each module has to do a certain task and this task has to be independent as soon as possible.

  • Base classes shouldn’t have a feature for a specific class. This is important for writing readable code and good documentation.

  • Your modules should have high cohesion and low coupling

Conclusion

Modular design is a design pattern that aims to subdivide the system into smaller parts. This design pattern reduces maintenance and development time and also increases your code quality.

The post Should Know The Modular Design Pattern For Next Game appeared first on GDBeginner.