Storage - reeseschultz/godex GitHub Wiki
Storage
In Godex all the data, like the components and the databags are stored inside the World. The World organize and stores the components inside the storage; it has one storage per component type.
There are different storage type, each with a different behavior, and it's possible to specify the storage method a component has to use.
- DenseVectorStorage This is the most used one. Iteration is really efficient and the data is stored contiguously. Add and remove data is fast.
- BatchStorage It's using the
DenseVector
, and in addition to the above allow to store more than 1 component of the same type per each Entity. This comes in two flavors: Static sized / Dynamic sized. - HierarchicalStorage Used to implement the Hierarchy mechanism, this one can be used when a component has to be combined with its parent; like for
TransformComponent
. - SteadyStorage This storage allocates a pool of memory, that it uses to store its components. The peculiarity of this storage is that once a component is created, its memory address never change until it's freed. Extraordinary useful to integrate non ECS libraries.
- SharedSteadyStorage This storage has the same properties of the SteadyStorage, but allow to share the same component between many entities. Extraordinary useful to integrate non ECS libraries.
In editor storage config
Godex supports in editor storage config, so you can customize, per each component, exactly how the storage behaves:
Steady storage
The SteadyStorage
main peculiarity is that the memory address of a component never changes. When integrating a library, this is a lot useful because it's possible to store the library objects directly inside a component, so it will follow all the ECS rules: even if the library is not ECS friendly.
For example, btRigidBody
is an object of Bullet Physics that represent a RigidBody. Usually you create it using new btRigidBody
: however, this would allocate the object somewhere in the memory. Thanks to this storage, I can allocate directly inside a component, in this way:
struct BtRigidBody {
COMPONENT(BtRigidBody, SteadyStorage)
btRigidBody body = btRigidBody(mass, nullptr, nullptr, btVector3(0.0, 0.0, 0.0));
};
The RigidBodies
are now allocated near each other. Also, the library is now integrated in an ECS friendly way, so easily usable.
💡 Notice, thanks to the nature of ECS, the library is naturally decoupled from the rest of the Engine, but however directly used. Taking the best of both worlds, the boilerplate code is minimized.
⭐ This storage is also useful when you have to deal with big sized object, like images, audio files, etc... In all these case where the used memory is so big, you can use this storage to avoid memory movements.
Shared Steady storage
The SharedSteadyStorage
is the little brother of SteadyStorage. It has the additional feature that allow to share the same component between multiple Entities
.
For example, many RigidBody in Bullet Physics can share the exact same Shape. Here in Godex we use this storage to allow it, so we have the following component, that can be shared between multiple Entities.
struct BtRigidShape {
COMPONENT(BtRigidShape, SharedSteadyStorage)
btCollisionShape shape;
};
📝 The
SharedSteadyStorage
uses theSID
(shared component id) parameter, to reference the component.
💡 The memory is handled and organized by Godex, so operate on this component is safe and efficient.
This feature is followed by an in editor UI, that allow to handle the shared component as usually it's done in Godot: