Skip to content

How to use the Registry

Ben Brenkman edited this page Sep 17, 2018 · 10 revisions

The Registry is a static class aimed at allowing certain instances of objects to be globally visible. This gets rid of needing to excessively pass objects around through function parameters.

What is the Registry

The Registry is a global registration of variables that can be used throughout the code without the need to explicitly pass the needed variable through a function. Some examples of using the Registry would be the rendering. The Render Engine instance is not passed around the Game Engine code. It can either be accessed through direct access to the Game Engine or can be accessed through the Registry.

Also the registry in future versions will handle different types of registration. Any ideas let us know what you'd like to use it for.

One thing to note about the Registry is that is does not do any memory management. Pointers added to the Registry will not be deleted by the Registry, it's only purpose is to increase the scope of objects from local to globally available.

How to Use the Registry

The Registry has a simple intuitive API, and since it is a static class you can very easily add instances of your own and get previously registered instances.

Getting Registered Entry's

To use the Registry add this include to the top of your .h

#include "Registry.h"

Then use the following code to get the desired entry.

Registry::GetEntry<[class type]>::GetRegistryEntry(const char* [name of the entry]);

This function is defined as a template function, The [class type] is the class that the entry is, all registered entries are inherited from the base class RegiatryEntry. So when the registry gives you a specific entry it must be cast into another object before being able to use it. If it is unable to cast it will throw an exception. You can either catch the exception or let it pass and crash the game. Registered objects are stored as pointers in a hashmap with the [name of entry] as the key. Therefore retrieving an object is as simple as knowing the class type and name in the registry.

Registering your own entries

If you want to replace an entry or even use the Registry to register your own objects and make them globally available, the API is super easy.

Every object that is registered in the Registry is inherited from the base class RegistryEntry.h this allows us to use the base class as the storage type and also explains why we need to cast objects before you can use them. The base class doesn't have any methods. Just extend your class with it and then you are ready to register your object.

To register use this following template.

Registry::Register(std::string name, RegistryEntry* objectPointer)

This will store the pointer of the RegistryEntry in a hashmap using the "name" as the key.