Best for managing modular classes

My game base consists of a series of modules organized as classes that are created, updated and interact as needed.

A few examples might be: CWindowManager , CGraphicsManager , CPhysicsManager , etc.

I am ashamed to say that I am currently using global pointers for them ( extern CWindowManager* g_WindowManager; ), and I know that this is probably bad.

In any case, the fact is that these modules must be created and deleted dynamically, and this is in the correct order, of course. The problem is also that modules like CPhysicsManager are scene dependent, so they are deleted when the scene switches and then is re-created.

Now I would like to abandon the use of globals to work with modules in my game.

I am not afraid of refactoring, but I cannot think of what would be the best alternative to globals.

I was thinking of creating a CModuleManager class and storing the module instances there as members, which are then produced from the CModule base class. Although I can not imagine how this will work in detail.

This seems like a common problem in software development and especially in game development, therefore:

- What is the best option for managing modules compared to simply using global pointers?

+6
source share
1 answer

Some things to consider when using global data:

  • multithreading. You must be careful if global data can be accessed by different threads at the same time.
  • verifiability. If you write unit tests, then global data must be initialized before any code accesses global data.

An alternative to using global data is to pass instances of the objects that each class / method requires as a parameter. This has the advantage that all class dependencies are visible from the API. It also simplifies the process of writing block tests. The downside is that the code can get messy if you pass objects everywhere.

Your idea of ​​having a ModuleManager sounds like a Locator Service template, which, in my opinion, is a valid solution. This link may help: http://gameprogrammingpatterns.com/service-locator.html .

If you decide to use global pointers, then the dynamic deletion of global data can be achieved in C ++ using smart pointers (auto_ptr).

+1
source

Source: https://habr.com/ru/post/893935/


All Articles