Immediately after the biome and resource maps generation, next is the city placement. We're getting near the first "interaction" with the entity system, as at the end of this step, we should have city entities. Therefore, it becomes apparent that there needs to be a basic ECS machinery in place.
ECS using ScriptableObject
Doubt No 1: Custom machinery for efficient component access.
In the C++ version of the entity system, each component type had their own storage vector, and the entity system did the following to access a component:
- Use the entity ID to look-up its component-index data, and the required component ID to look-up if the entity has valid index for the component, and if it does, use the retrieved index to look-up in the component storage
So this does involve a few lookups/jumps, so I wouldn't call it too optimized or cache-friendly.
Doubt No 2: Entities adjust their components at runtime.
Typical ECS allows for runtime composition of components, using the aforementioned machinery. Adding a component is as simple as allocating a component and storing the index in the "component index" entry of the entity. Question is, when do we need that? Answer is, depends on how granular the components are, and how dynamic some of these component types can be. From my theoretical design so far, adding components dynamically is a solution to a future problem, while it creates other problems now. Problems such us opportunities for nonsensical component combinations (city having a character sheet, adventure location having movement component, etc).
Components as ScriptableObjects
Reading about Unity one reads a lot about "the tyranny of MonoBehaviour" and how heavyweight MonoBehaviours are. At the same time, it looks like the consensus says "Use the fancy new scriptable objects". Therefore, wanting to go the suggested route, and because components should just store data, ScriptableObjects become a quite reasonable solution (so far) to that issue.
Entities as ScriptableObjects, interfaces and components
C# is an interface-happy language, so I decided to go that route. Here's the current line of thought:
- The base Entity class now is a ScriptableObject
- The entity-has-such-and-such-components composition is implemented via interfaces
- There are subclasses like CityEntity, DungeonEntity, CreatureEntity, etc. These subclasses implement the interfaces they need
The Entity types and interfaces code is generated from a json file and looks like:
Now we can check if an entity called "someEntity" implements an interface to see if it has a location component simply by "
someEntity is ILocation" The above code was autogenerated from python using a small dictionary:
To wrap up with the ECS basics we still need to define the systems and where they live, as well as message handling, but that's for next time. Also in the future I'll possibly need to sort out the access so that's it's generally read-only, but that might be tricky.