Event Manager in Cocos Indie Game Development Framework (Billionaire Series B)

Introduction

This is part of the series by Cocos developer “Billion Dollar Programmer” on helping developers build their own games. We’ll be sharing his stories on the forum. You can read the originals from his website.

What is an event manager

An event manager in game development is a tool or system for handling communication and interaction between modules within a game. It allows different elements, objects or systems in the game to notify and respond to each other’s events, thus creating the interaction and dynamic change of game logic. Event managers play a key role in game development, helping to reduce code coupling, improve code maintainability and scalability, as well as making the game development process more flexible and efficient.

In game development, many different objects and modules need to communicate, such as player inputs, game internal state changes, collision detection, and so on. The event manager provides a centralized mechanism that allows these objects and modules to communicate by sending and listening to events without having to directly rely on each other’s specific implementation details. This helps to reduce dependencies between code, allowing game developers to focus more on the design and development of individual modules, thus improving code maintainability.

Event managers are usually implemented based on the Observer Pattern. The core idea is that an event manager maintains a series of observers (listeners). When an event occurs, it will notify all the registered observers of the event, which triggers the corresponding callback function or operation. In this way, different modules in the game can communicate and collaborate by listening to events without having to directly reference or call each other’s methods.

For example, in a role-playing game, a player may control a character to move. When the player presses the move button, the game can use the event manager to send a “move event”, and then the character control module can listen to this event and respond to it, causing the character to move according to the player’s input.

Importance of event managers

Module decoupling and reuse: In indie game development, game logic often consists of multiple modules, such as player control, enemy behavior, level switching, and so on. Event managers can decouple these modules to reduce dependencies, allowing each module to exist, be modified and reuse independently, improving code maintainability.

Flexible communication mechanism: The event manager provides developers with a flexible communication mechanism where different modules can listen to specific types of messages by subscribing to events. This means that developers can dynamically add or remove event listeners while the game is running, enabling real-time interaction and feedback.

Centralized Logic Management: Various logic events in the game, such as players getting props, quests completing, winning the game, etc., can be centrally managed through the event manager. In this way, developers can have clearer control of the whole game’s flow and state changes.

Rapid Iteration and Testing: The use of the event manager allows developers to iterate and test more quickly. By simulating the triggering of different events, developers can more easily verify whether the game logic is correct or not, and speed up the development cycle.

Multi-Platform Adaptation: In indie game development, many games may need to be adapted to different platforms, such as PC, mobile devices, and consoles. Event manager can help developers better manage game logic on different platforms and reduce the workload of platform adaptation.

Design Principles of Event Manager

Publish-subscribe model: The event manager should be based on a publish-subscribe model, where objects can subscribe to specific types of events and receive notifications when events occur.

Event Types: Developers should be able to define a variety of custom event types to suit different game logic requirements.

Flexibility: The event manager should have the flexibility to allow multiple subscribers to listen to the same event, as well as the ability for subscribers to optionally unsubscribe.

Data Delivery: The event manager should be able to deliver event-related data so that subscribers can respond based on the event context.

Event Manager Implementation

  1. New script EventMgr inherits cc.EventTarget

In Cocos Creator, cc.EventTarget is a class for event handling. It provides a mechanism that allows you to send and receive events between different objects for decoupling and better interactivity. So we can simply implement an event manager by inheriting it and wrapping it.

  1. Create a single instance of the event manager

Because the event manager key content engine has been built for us, then we only need to simply inherit, and then in the form of a single case, making it more convenient to call.

  1. Encapsulation interface

Subscribe to events.

Subscribe to an event (1 time only). Events subscribed through this interface are automatically unsubscribed after one reception.

Any subscribed event can be deleted through this interface.

Delete all subscriptions of the target. All subscribed events of the target can be deleted through this interface.

Dispatch Event. All targets subscribed to the specified event will receive the event and can respond.

  1. Demonstration of the effect

Test the on and once interfaces:

The test passes once the interface disappears after the first event is received and does not receive a second event:

Test the off interface:

The test passes, and the event is not received after unsubscribing:

Test the targetOff interface:

image

The test passes, no more events will be received after canceling all events from the target subscription:

Summarize

The way to neat code is decoupling. The event manager helps to maximize the decoupling between the code of each module and improve the maintainability of the code.

Source Code