Object Model
Created | |
---|---|
Tags |
Overview
An object model is similar to a business model in a regular application.
In games, the object model is a representation of the elements that participate in the game world.
In some engines, the object model is restricted to the representation of visual objects in a scene.
Other engine adopt a broader object model in which any data can be represented.
Typically, a scene is built as an aggregation of game objects, or entities.
Editor and Runtime Models
Editor
- Representation of a model of the world for designers
- Object-oriented
- Flexible
- Designed as archetypes (templates, aspects, components, inheritance, ...)
- Can be introspected
- Have default properties at an instance or template level
Runtime
- Representation of an editor object
- Optimized for performances
- Memory usage
- Data access
- Batching
A one-to-one mapping between editor and runtime objects may not exist.
- Editor objects can be design-time only (splines, regions, ...).
- An editor objects can be expanded into multiple runtime objects.
- “Array-of-structs” at design-time could become “struct-of-arrays” at run-time.
Models
Hierarchy System
In the hierarchy system, the typical object-oriented design (OOD) of modeling data is used with objects in an inheritance hierarchy.
At the root of the hierarchy, the entity type defines an abstract object.
Derived types define more specific objects with additional data and behaviors.
ℹ️Also called monolithic class hierarchy.
✔️Easy to map the concepts to a concrete hierarchy.
❌Poor flexibility.
❌Difficult to share functionality among objects.
❌Difficult to reason about the chain of hierarchy (ie Renderable derives from Animatable or the other way around).
❌Common functionality tends to move up the hierarchy (root objects contain too much functionality and knowledge of the hierarchy).
❌Problem of multiple inheritance: multiple copies of the base class, multiple virtual table pointers.
Examples
- Unreal Engine 3
- CryEngine
Component System
In the component-based architecture, the entity object is a container of components.
Components provide custom data and behaviors.
As an entity contains multiple components, sharing of functionality is easy.
Allowing components to reference each other makes communication easy.
✔️Easy to implement new behaviors.
✔️Easy to share functionality among objects.
❌Complex objects include many components and communication becomes difficult.
❌Implementation of behaviors is not efficient because of poor data locality.
Variants
Relashionship
- An entity has zero-or-one component of each type.
- An entity has zero-or-many component of each type.
- A hybrid approach (some components support a zero-or-one relationship, and other a zero -or-many relationship).
Components
- The entity knows about every component types.
- The entity can provide easy access to the components (
GetTransform
,GetMeshComponent
, ...).
- The entity can provide easy access to the components (
- The entity doesn't know about the component types.
- The entity provide a generic access to the components (
GetComponent<T>
).
- The entity provide a generic access to the components (
- A hybrid approach with common components known by the entity.
Examples
- Unity
Hierarchy/Component Hybrid System
A hybrid of the hierarchy and component systems.
✔️Can specialize some concepts in dedicated objects.
❌Difficult to reason about the correct implementation (as a specialized object or a set of components).
Examples
- Unreal Engine 4
Entity Component System
An entity component system (or ECS) is a data-oriented design (DOD).
An entity is a simulation object that lives in some kind of simulation world.
A component contains data of a certain type.
An entity contains multiple components.
A system is responsible for implementing the behavior of an entity containing a specific set of components.
A system perform the update of the simulation in the world.
The data layout of components is optimized by storing the component data in arrays, and identifying the relationship between entity and components using unique identifiers and hashed maps.
✔️Easy to share functionality among objects.
✔️Easy to optimized batch operations.
✔️Better performances with efficient memory layout and batching of operations.
❌Implementation of behaviors is harder to reason about.
Examples
- Stingray Engine
- The Machinery
- Unity (ECS)
Mix-in System
A mix-in system is property centric.
Multiple presets of data are defined and entities are created as a set of these presets.
Examples
- Gamebryo LightSpeed
Synchronization
Brute Force
Iterate over all the entities and copy their state.
Can be optimized with hierarchical dirty flags.
Callbacks
Call a callback whenever an entity is changed to copy its state.
Deferred
Put the changed entities in a list.
Process the list of changed entities and copy their state.