GigaObjects

As with most other game engines you’ll see out there (Unreal, Godot, etc.), it is easiest to have a single base class that all other objects derive from. In addition to building in some basic functionality (like ToString functions, locking for multi-threading, etc.), it also comes in handy for things like scripting, where you can pass around pointers to the base object class until you need to statically or dynamically cast it up to its actual type. You’ll see examples of this in our scripting engine and in our Variant class, which allows us to hold a value of almost any type.

In our engine, we call this the GigaObject. Its only purpose, other than providing a base class, is to provide a mutex, which can be used to lock or unlock the object for multi-threading purposes later on.

Let’s look at the code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/**
 * Base object class for most objects in the instance
 */
class GIGA_API GigaObject {
public:
	virtual ~GigaObject() = default;
 
	/**
	 * Lock/unlock object for multi-threading
	 */
	void Lock();
	void Unlock();
 
protected:
	// Protected default constructor (no direct creation)
	GigaObject() = default;
 
protected:
	// Mutex
    std::shared_ptr m_mutex = std::make_shared();
};

As you can see, there’s not much here. We provide a simple Lock and Unlock function, which locks and unlocks our mutex for multi-threading:

1
2
3
4
5
6
7
void GigaObject::Lock() {
	m_mutex->lock();
}
 
void GigaObject::Unlock() {
	m_mutex->unlock();
}

And that’s it. You may also have noticed that we declare a protected constructor. This is a common strategy used in the classes of the engine that cannot be created directly.

Next, we’ll look at our Variant class, which allows us to pass around different data types and values (including GigaObjects) without inherently knowing what’s inside. This will come in handy later for generating metadata and for our scripting engine for passing parameters to functions.

Leave a Reply

Your email address will not be published. Required fields are marked *