Hello everyone.
I'm currently working on some game engine and have been scratching my head quite a while on how to handle multiple entity types.
To be on the same page, an entity is to me an element in the scene/level, for instance:
- Hero / Player
- Various sort of enemies and their respective behavior
- Inanimate elements
- Animated elements
- Terrain
...
I spent some time looking at the literature and modern trends. It seems that a lot of game engines are still with the OOP mindset, but are transitioning from classic inheritance to things such as component based entities.
Casey previously wrote some articles about a new editor feature for The Witness and disclosed some code for the occasion:
->
http://mollyrocket.com/casey/lister_panel.cpp
In this code, one could guess how entity are coded. There's apparently a generic entity class and other subclasses (Grass, door, etc.) for which Casey just do a simple cast in order to access specific class members. But I might be completely wrong on this (some predefined void* data pointer at the end of the original entity class that gets molded into another specific entity class when casting?).
To the question: How would you handle multiple entity types?
Considering that:
- distinct entity types could be represented as different data structure (the grass entity would have a number of instance for example), though some common base would be present (uid, position, etc.)
- distinct entity types could be handled by different code logics (i.e. the rendering for grass would use instance-based rendering).
- all of this without falling into the OOP trap and keeping a performant data-oriented code.
I'm sure there's no perfect answer, but I wanted to collect some thoughts on the matter and improve my current code base keeping in mind & following what Casey has been teaching us so far :)