Handling different types gracefully

Anthony Goins neontotem at gmail.com
Mon Jul 1 08:26:32 PDT 2013


On Monday, 1 July 2013 at 12:03:25 UTC, Roderick Gibson wrote:
> I'm asking because I'm doing some game development in D and 
> I've come upon the entity component architecture, and it looks 
> like a good way to handle the complexity and interdependency 
> that games seem to have.
>
> The components wind up being plain old data types (I currently 
> use structs), with systems that actually modify and interact 
> with them on a per-entity basis. The basic idea is that you 
> compose entities (basically just an id) with various components 
> to give the specific functionality you need for that particular 
> game object.
>
> The whole entity system is just a database for the different 
> subsystems to query for entities (just an id) which have 
> components that fulfill their criteria (for example a physics 
> subsystem might only be interested in entities with the 
> components position, movement, and collision, while a render 
> subsystem might only be interested in entities with components 
> position, mesh, sprite). There's also the reverse where the 
> subsystems register which components they are looking for and 
> then the entity system serves up entities that match the 
> criteria.
>
> The standard way to store components is just to subclass them 
> from a base class "Component" and then store a pointer to them 
> in a Component[] in an overall entity manager. But then you 
> have to do things like type casting the pointers when you 
> return the queries, which seems a bit rough. It also feels 
> wrong to make them inherit a class for the SOLE reason of 
> getting around the type system.
>
> Anyway, I think I'm just rubber ducking a bit here, but I'm 
> wondering if there's another way to do this, if someone has any 
> experience with this sort of system.
>
> As an example of what I'm looking for, say there is a class 
> Entities, with some type of container that could hold arrays of 
> different component types (this is the part that really stumps 
> me). I'd like to be able to do something like:
>
> ...
> auto entities = new Entities();
> auto entity_id = entities.createEntity();
> entities.addComponent!(position)(entity_id, pos);
> entities.addComponent!(movement)(entity_id, mov);
> entities.addComponent!(collision)(entity_id, col);
> auto physics_data = 
> entities.getEntitiesWithComponents!(position, movement, 
> collision)();
>
> The two big requirements are some kind of regular, queryable 
> structure to hold the components (of different types), and the 
> ability to filter by type. Is anything like that remotely 
> possible?

Waiting for multiple "alias this" ??
And opDispatch to respond to unimplemented components.


More information about the Digitalmars-d-learn mailing list