jeudi 17 mars 2016

Can a single array be organized with 2 indexes to track active + inactive + pooled objects?

I was reading up on Data Locality (http://ift.tt/IXJlbK) and the example of the Particles caught my interest (array of active vs inactive objects).

Summary - It discusses how you can use an index flag to track where the array splits the used VS unused objects, making for-loops more efficient by eliminating if-conditions that checks whether or not an object is active.

Before:

enter image description here

After:

After Data Locality

Now, if one was to make an ECS game engine, on top of tracking which objects are used / not-used, would it be possible to track which objects are inactive?

To clarify, take a movement-component for example:

  • First you have to get one (from the object-pool), and mark it as active (ie: swap it to the portion of the array with 'active' objects).
  • Maybe at some point in the game you wish to temporarily pause the movement (deactivate the component), but you don't want to lose the reference between the component and its entity, so you don't recycle it just yet (ie: don't swap it back to the object-pool portion of the array, put it in the inactive portion instead).
  • A few seconds later, you re-activate the movement (swap it back from the inactive part of the array to the active part).
  • Finally, your entity gets hit by... oh, lets say a rocket launcher. Now you dispose and recycle the movement-component (ie: swap it back to the object-pool portion).

Visually, the array would look something like this:

Array with three different portions: active, inactive and pooled

So my question is, can this setup work in theory, or would it be too prone to reference errors / over-complicate things? Would it be better off storing the components in 3 separate Arrays (one for actives, inactives, and then the pooled objects)?

EDIT: Title was originally "Can a single array be organized with 3 indexes..." but just realized afterwards it only needs 2, since the active objects are assumed to start at 0 and go on until it reaches the index for inactive objects.

Aucun commentaire:

Enregistrer un commentaire