D2 dev diary 001 – Entity system

Getting back to D2 development after a year of focusing mostly on Tiny Player and Tiny Player for Mac.

Some of the C++ code is not so great. It’s been a while…

The property system is “awesome” but it seems like I was trying to be very clever and now it all looks pretty complicated for what it does. But at least it works, so let’s keep it for now.

First, I’m going to rework the entity system and make it simple and solid.

We won’t store entity pointers directly anywhere in the game. Instead, let’s store an index into the entity array and use a getter to retrieve the pointer when we need it. To make sure the array slot actually contains the object we expect, we’ll add a “generation” index, which gets increased each time a new entity is added. That way, we can query the entity list using a handle and get a valid pointer or NULL if it’s gone.

EntityID {
  index
  generation
}

EntityID AddEntity( Entity* e );
Entity *GetEntity( EntityID id );
void RemoveEntity( EntityID id );