I've been studying game programming with SDL and I came across a very interesting subject which is the Data-driven design. I searched around the web and I found lots of definitions and approaches about it and I didn't understand it very well. What is it exactly? How can it be used in game programming?
There's two similar terms floating around, so just to be clear I'll explain 'em both.
data-driven design: behavior should be specified by data, not code.
A data-driven approach might have a file like:
// orc.json
{
"name": "orc",
"hp": 150,
"armor": 10
}
While a non-data driven approach might do something terrible like:
// orc.cpp
class Orc : public Monster {
public:
string_view getName() const { return "orc"; }
int getHp() const { return 150; }
int getArmor() const { return 10; }
};
The latter approach is bad. It hard-codes the orc into the game. A designer must be an engineer and have the ability to recompile the game to make a simple change. After making the change, the game must be completely restarted to see the change; it cannot be edited while the game is running.
The former approach uses a data file. A designer can edit this file. A nice GUI for editing these files can be produced, or tools that import it into a database or CSV for each comparison with other monsters and to making tuning and balancing easier. The file could be reloaded while the game is running. etc.
There'd still be code, of course, but it should be general and non-specific. The code specifies _how_ something is done, but the data specified _what_ is done. e.g., in a data-driven approach, you might have:
// monsterdata.cpp
struct MonsterData {
string name;
int hp;
int armor;
};
// load from a JSON file
bool LoadMonsterData(string path, MonsterData& data);
// save to a JSON file
bool SaveMonsterData(string path, MonsterData const& data);
and there the data is a simple struct you can edit with AntTweakBar or a custom editor GUI. It can be loaded, saved, etc. Much more flexible. You can take this even further and not have a specific Monster class but rather a general purpose set of components or attributes (so anything can have HP, not just monsters, for example) which now lets designers create whole new types of objects out of small pieces rather than needing an engineer to write code for new types of objects.
Again, let the code decide _how_ things are done (HP/kill tracking, etc.) and let data decide _what_ is done (who has HP, how much, what causes damage, how much damage is done, etc.).
data-oriented design: think about the data being operated on while writing code.
Since this isn't what you asked about (I just know a lot of people confuse the terms) I'll give the short version.
Basically, data-oriented design says that you should "code to your data" while contemporary OO-ish teachings tell you to "code to the abstraction". Abstractions typically force you to operate on data in overly-complicated and inefficient ways while directly manipulating data is less reusable or flexible.