Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'd like to know if someone has found a way to build a component-based entity system in their game(s) without using IDs. I find that IDs tend to do away with one of the major (possible) advantages of OOP: not having to be aware of the type of a certain object.

share|improve this question
I have an ID member on my entity system, but I never bother using it. What language are you using? – The Communist Duck Jul 3 '11 at 16:57
IDs of what? Type of component? Unique to the entity? Unique to the component? – Tetrad Jul 3 '11 at 17:09
@Tetrad: Isn't my question clear enough? No IDs. – Paul Manta Jul 3 '11 at 17:24
@Paul, if you tell us what kind of IDs you're talking and what you need them for and why you think they're bad maybe we can help you. So no, your question isn't clear enough as-is. – Tetrad Jul 3 '11 at 18:53
@Paul: If an entity has an ID, you don't have to be aware of the particular subtype that the object has, only the type that it derives from (assuming the parent type contains the ID). This doesn't break polymorphism in any way. So I don't understand what you're trying to say in the last sentence of your question. The "problem" you've identified with the use of IDs is not really a problem. Perhaps that is one of the causes of downvotes? – Olhovsky Jul 3 '11 at 20:33
up vote 2 down vote accepted

Your entities need ID's in some form or another, if you want to persist them to disk, or pass them over a network. Thus, what you probably want is not to get rid of ID's, but to dynamically generate them so you don't have to set them manually in the code every time you create a new Entity-class.

I've done this in my latest C# project by creating a special List class that assigns a unique index to every item added to the list. Unlike a normal list, when an item is removed, the indices for the rest of the items don't change (Internally it's implemented using a Dictionary).

Thus, as long as the entities are added to the list in the same order every time the executable is run (or, for networking, between all clients), they will always have the same ID.

The problem with this solution is that it's not in a human-friendly format. To fix this, you could use reflection to prepend the type-name.

share|improve this answer

I use IDs (for entities, not components), though I could do without them if I didn't want weak referencing for purposes of persistence + lazy loading. Mine do not defeat polymorphism as you describe, though. Try having your IDs provided by a universal registry and accessed via a standard interface.

share|improve this answer
I'm not trying to make 'good' IDs. I'm trying to get rid of IDs. – Paul Manta Jul 3 '11 at 17:25
@Paul: So get rid of them. Of course, the obstacle to that is whatever actual purpose they're serving for you, but you haven't told anybody what that is, so we can't answer your actual question, which is "how do I meet the needs that IDs are fulfilling for me in an alternate fashion?" I was, however, addressing the fact that your stated motivation for getting rid of IDs -- that they ruin polymorphism -- is an artifact of your implementation, not an inherent part of their nature. If the reason you want to get rid of them is addressed but you still want to get rid of them, what's going on? – chaos Jul 3 '11 at 18:37

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.