Object types and factories

Classes that derive from Object contain type-identification, they can be created through object factories, and they can send and receive events. Examples of these are all Component, Resource and UIElement subclasses. To be able to be constructed by a factory, they need to have a constructor that takes a Context pointer as the only parameter.

Object factory registration and object creation through factories are directly accessible only in C++, not in script.

The definition of an Object subclass must contain the URHO3D_OBJECT(className, baseTypeName) macro. Type identification is available both as text (GetTypeName() or GetTypeNameStatic()) and as a 32-bit hash of the type name (GetType() or GetTypeStatic()).

To register an object factory for a specific type, call the RegisterFactory() template function on Context. You can get its pointer from any Object either via the context_ member variable, or by calling GetContext(). An example:


To create an object using a factory, call Context's CreateObject() function. This takes the 32-bit hash of the type name as a parameter. The created object (or null if there was no matching factory registered) will be returned inside a SharedPtr<Object>. For example:

SharedPtr<Object> newComponent = context_->CreateObject(type));