Sign in to follow this  
Aqua Costa

Data-Driven Property-centric architecture

Recommended Posts

Aqua Costa    3691



I'm implementing a data-driven property-centric architecture to manage game objects on my engine.

Basically I have multiple arrays, each containing a single property of every game object, so a game object is simply an index used to access the right position of each property array.


However I'm wasting quite a bit of memory because not every game objects uses every property but they still need to have it so the indices of the other objects access the correct value.




Game object 0 - A door

Game object 1 - Player



Positions array - (x1,y1,z1) | (x2,y2,z2)

Health array     - 0 | 100 | ...


Even though the door doesn't need a health property the index in the health array must be wasted, in order to access the player's health correctly (health[player] = 100)


As the number of properties increases so does the memory wasted, any idea on how to reduce the wasted memory?



Edited by TiagoCosta

Share this post

Link to post
Share on other sites
DrColossus    767

You could just keep different sets of arrays per object type, chances are you want to do different things with them anyway. 


For example:

struct Doors
    std::vector<vec4> positions;

struct Players
    std::vector<vec4> positions;
    std::vector<float> healthValues;

You can then either write functions that operate on these structs or on the internal data:

void updatePlayerPositions( Players& players )
    //iterate just over player positions

void updatePositions( vec4* positions, size_t count )
    // iterate over positions of any type

updatePlayerPositions( players );
updatePositions(, players.positions.size() );

Share this post

Link to post
Share on other sites
Waterlimon    4398

Yeah, you need to handle different objects differently if they are too different, but if the difference is not great, wasting some space might be better than the overhead from differentiating between the different objects.


So, in your case, if you had 50 types of entity, and one didnt use the health component, it might be better to just let the single type waste some space by not using the health component, rather than making a completely new set of arrays for that entity type, or adding a layer of redirection.


The layer of redirection would basically be having a pool of health components, and each object has an index to this pool. The objects will all still have the index, and if they dont have the health component, the index is invalid (like a zero pointer).


You have three approaches:

1. Do nothing. Let there be wasted space.

*Cost: wasted space


2. Have a separate sets of arrays for different groups of similiar objects to minimize wasted space.

*Cost: maintenance of multiple sets of arrays, have to process multiple arrays of the same object type instead of 1


3. Add a layer of redirection to make the cost of not using a component smaller memory wise, at the cost of redirection on access and maintaining a pool of components, along with the memory cost of maintaining an index to this pool for each entity.

*Cost: maintenance of a pool of components + an index to the pool for each entity (0 for ones without the component) + redirection to access the component through the entity ID


Pick the one with the lowest cost.

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this