Rebel Fork Framework
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));

Calls to Context::CreateObject are thread-safe. However factory registration and removal is not thread-safe and user must ensure that no thread calls Context::CreateObject during factory registration or removal.

SharedPtr< Object > CreateObject(StringHash typeNameHash)
Create an object by type. Return pointer to it or null if no reflection is found.
Definition: ObjectReflection.cpp:264
void RegisterFactory(ea::string_view category={})
Deprecated. Use AddFactoryReflection, AddAbstractReflection or AddReflection instead.
Definition: Context.h:97