class ObstacleSpawner : public Eclipse::Component
{
COMPONENT_BASE_2(ObstacleSpawner, 3)
public:
void Start() override;
void Update() override;
private:
SERIALIZED_FIELD(Eclipse::Assets::Prefab, ObstaclePrefab);
SERIALIZED_FIELD(float, ObstacleSpawnInterval, 2);
float obstacleSpawnTimer = 0.f;
};
It worked, and it let the team move on to other parts of the engine. The cost was that each field
stored its own copy of the type and variable metadata. Every instance of a class carried the same
data again and again, and SpriteRenderer2D grew to about 912 bytes
per instance.
class ObstacleSpawner : public Eclipse::Component
{
MAKE_REFLECTABLE(ObstacleSpawner);
REFLECTION_FLAG(COMPONENT);
COMPONENT_BASE_2(ObstacleSpawner, 3)
public:
void Start() override;
void Update() override;
private:
[[SerializeField]]
Eclipse::Assets::Prefab ObstaclePrefab;
[[SerializeField]]
float ObstacleSpawnInterval = 2;
float obstacleSpawnTimer = 0.f;
};
The class still needs a couple of macros to set itself up, but the fields are plain variables again.
template<> struct ReflectionRegistrar<ObstacleSpawner>
{
static inline void Register()
{
Reflection::ReflectionRegistry::RegisterType(
TypeEntry{
.name = "ObstacleSpawner",
.size = sizeof(ObstacleSpawner),
.id = Type::GetTypeID<ObstacleSpawner>(),
.create = [](){ return reinterpret_cast<void*>(new ObstacleSpawner); },
.createAtAddress = [](unsigned char* addr){ return reinterpret_cast<void*>(new (addr)ObstacleSpawner); },
.destroy = [](void* data){ delete reinterpret_cast<ObstacleSpawner*>(data); },
.flag = static_cast<Reflection::ReflectionFlags>(1),
.fieldOrder = { "ObstaclePrefab", "ObstacleSpawnInterval" }
}
);
{
FieldEntry entry =
{
.name = "ObstacleSpawnInterval",
.owner = "ObstacleSpawner",
.offset = offsetof(ObstacleSpawner, ObstacleSpawnInterval),
.size = sizeof(ObstacleSpawner::ObstacleSpawnInterval),
.type = Type::GetTypeID<decltype(ObstacleSpawner::ObstacleSpawnInterval)>()
};
Reflection::ReflectionRegistry::RegisterType<decltype(ObstacleSpawner::ObstacleSpawnInterval)>();
entry.attributeContainer.Add<SerializeField>();
Reflection::ReflectionRegistry::RegisterField(
"ObstacleSpawner",
entry
);
}
// ...and one block like this for every other reflected field
}
};
Because the generated code uses sizeof,
offsetof and decltype, the
compiler fills in the real layout. The tool only has to find names. It never has to work out sizes or
offsets itself.
struct ComponentsStaticInitiator
{
ComponentsStaticInitiator()
{
ReflectionRegistrar<Canvas>::Register();
ReflectionRegistrar<SpotLight2D>::Register();
ReflectionRegistrar<AudioEmitter>::Register();
ReflectionRegistrar<Camera>::Register();
ReflectionRegistrar<RigidBody2D>::Register();
ReflectionRegistrar<AmbientLight2D>::Register();
ReflectionRegistrar<DirectionalLight2D>::Register();
ReflectionRegistrar<PointLight2D>::Register();
ReflectionRegistrar<ParticleSystem2D>::Register();
ReflectionRegistrar<Transform2D>::Register();
ReflectionRegistrar<SpriteRenderer2D>::Register();
}
};
inline ComponentsStaticInitiator __componentsStaticInititator = {};
The game is a dynamic library that the editor loads, so it works a little differently. There the
tool generates an exported RegisterReflectedTypes function, which the
editor calls right after loading the library.
void Set(const std::string& typeName, const std::string& fieldName, void* obj, const void* data)
{
const FieldEntry& field = ReflectionRegistry::GetField(typeName, fieldName);
::memcpy(reinterpret_cast<char*>(obj) + field.offset, data, field.size);
const OnChange* onChange = field.attributeContainer.TryGet<OnChange>();
if (onChange)
{
onChange->event(obj);
}
}
void* Get(const std::string& typeName, const std::string& fieldName, void* obj)
{
const FieldEntry& field = ReflectionRegistry::GetField(typeName, fieldName);
return reinterpret_cast<char*>(obj) + field.offset;
}
This is all the editor needs to show and change a value without knowing the class it is editing.
struct SerializeField {};
struct Range
{
float min, max;
};
struct Step
{
float step;
};
struct OnChange
{
std::function<void(void*)> event;
};
The tool doesn't need to know what an attribute means. It copies the name and arguments straight into
the generated code, so [[SerializeField, Step(0.1f)]] becomes:
entry.attributeContainer.Add<SerializeField>();
entry.attributeContainer.Add<Step>(0.1f);
Each field stores its attributes by type, and any system can ask whether a field has one. This is how
the inspector decides how much a value changes when you drag it:
const Step* stepAttr = attrs.TryGet<Step>();
if (stepAttr) step = stepAttr->step;
Adding a new attribute means declaring a struct and reading it where it's needed. The reflection
system itself never has to change. That was the extensibility goal from version 2.