MVC, my friend
As I was finishing high level design of scene management, I realized it was starting to resemble the Model-view-controller pattern. The (hopefully) more or less final design is sort of a hybrid of the alternatives I already presented:
- Entities contain arbitrary data in attributes. These compose the Model.
- Entities can contain any amount of features; these are the controllers.
- Subsystems (for lack of a better name) link entity attributes to lower-level systems - such as the renderer, audio mixer and physics simulation - and thus represent views into the scene.
Features are normal tasks in the engine, based on cooperative multitasking. They can read from and write to attributes and create, trigger and listen to signals (an implementation of the observer pattern), which is how they communicate with each other. Subsystems also listen to signals to get notified of attribute changes. Attributes can be shared between features; for example transform is a common attribute probably used by almost all features and subsystems. Sharing of attributes is based purely on their names, there's no actual cooperation between features and subsystems except for signals and of course documentation.
I think a rough example is in order again:
- A ship gets hit, triggering the hit signal.
- The damage controller feature is listening to this signal and gets notified.
- Damage controller decreases ship health accordingly and notices it's below zero: it adds a destruction sequence feature to the ship entity and stops listening to hit events.
- Destruction sequence adds an audio source attribute to the ship entity (if it doesn't already exist), spawns particle effects for a while and finally marks the ship entity for removal.
- The audio subsystem notices the new audio source and adds it to the low level audio mixer.
What do you think? Input is still welcome, I haven't actually started implementing this yet.
And I still have at least a couple of open questions:
- How should signals be triggered externally, for example by features in other entities? Should they just trigger the signals directly, or should there be an "external API" consisting of callable attributes?
- Should features occupy named slots like attributes, or should they just reside in a generic list? I'm currently leaning towards the latter approach and reserving names just for attributes, since feature don't have to directly know about each other.
I still have to design the rendering pipeline; I already have a general idea of what I want, but details are kind of blurry. But that's the topic of another post.
Between designs
Yesterday and this morning I've read a lot of material on various topics related to scene management, rendering and a host of other subjects. TomF's Tech Blog was especially useful with rendering. I think I now have a relatively good idea about what I need in scene management and what features it should have. However, at the moment I'm torn between two designs, which are roughly equal to the first and third alternatives in my last post. I wrote small and rough examples on both to illustrate, and input would be really welcome at this point. The example is a space ship with an AI, a rigid body for physics, an audio source for positional sound, a particle effect for the engine and a turret, which in turn contain its own AI.
1. Data container entities with single-instance subsystems handling all the logic
ship.visual = ship_mesh
ship.rigid_body = ship_body
ship.logic = ShipAI()
ship.audio_source = AudioSource()
effect = Entity()
effect.visual = ParticleEffect()
effect.parent = ship
effect.transform = engine_transform
turret = Entity()
turret.visual = turret_mesh
turret.logic = TurretAI()
turret.parent = ship
turret.transform = turret_slot_transform
2. Feature (aka component) container entities, with all the logic and data contained in the features
ship.add_feature(ShipAI())
ship.add_feature(ship_mesh)
ship.add_feature(ship_body)
ship.add_feature(AudioSource())
effect = Entity()
effect.add_feature(Link(ship))
effect.add_feature(ParticleEffect())
effect.transform = engine_transform
turret = Entity()
turret.add_feature(turret_mesh)
turret.add_feature(TurretAI())
turret.add_feature(Link(ship))
turret.transform = turret_slot_transform
Which do you like more? In my opinion, the first option has the advantage of being perhaps more intuitive and clearer to use: you just assign features to attributes, whose name tells what they are for. On the other hand, the second interface depends less on magic and is perhaps easier to extend with new feature types, as there are no predetermined names for feature "slots" (in the first option, the names are defined by the subsystems that handle the logic of entities). Of course you could also use subsystems in the second option and "intelligent components" in the first option, but these two interfaces seem to fit those two underlying designs best, at least in my opinion.
If you have any questions or general comments about either of the designs, feel free to post them, all input is welcome.
Research phase
I've been doing some research and brainstorming for both the engine (still without a new name) and Red Nebula. I'm trying to design a new scene management package for the engine, including a new renderer. What's certain at this point is that it's going to be based on entities with components. Entities are more or less dumb containers for components, which describe the behaviour of the entity. Data lives in either the entity, its components, or both, depending on the exact design. Evolve Your Hierarchy is a good article on what it's about and why you should do it.
This thread on Gamedev.net has been really helpful for getting them brain juices flowing, and as I wrote there, I've got a couple of alternatives I want to think about further:
- Very similar to the misguided attempt of creating a unified scene graph for Spineless, but on a higher level, based on game entities, not low-level scene graph nodes. Entities contain just data, while subsystems (or workers as I called them) contain all the logic of how to deal with the data. There are no components per se.
- Components define only behaviour, while entities contain components and their (possibly shared) data.
- Entities are solely containers for components, while components contain both behaviour and data.
At the moment I'm mostly leaning towards the first alternative, though the other two have their advantages too, and all of them seem quite elegant to me. I'll have to think more about this first, and input is of course always welcome.
Brainstorming for Red Nebula has piqued my interest in reading sci-fi again, so I started reading Iain M. Banks' Use of Weapons. I really like his stories and writing style; they're definitely "high sci-fi" in the sense that technology is really advanced - space battles generally last only fractions of a second - and there are actions sequences and such, but they're not corny adventure stories or anything. I think the genre is called "hard sci-fi". The other sci-fi writer I really like is Philip K. Dick, though his style is completely different and not that much centered on technology.
I've also done some coding, but not much. Mostly just stubs for the new scene package and small fixes here and there. I should get resource management out of the way at some point, before starting to really work on scene management.
