Using a database in a game or just multiple objects?

Started by
11 comments, last by Krohm 11 years ago

Do the simplest thing that could possibly work. You will need some kind of serialisation if you intend to persist your stuff to disc.

You could use a third party serialisation library (boost is ok, I guess), or just write a really simple serialiser. It's up to you.

But don't make things more complicated than they need to be.

The use-case for a SQL database is mostly if you're storing a very large number of objects and don't want to keep them all in memory, but do want to use secondary indexes. Also interoperability.

If your database fits in memory, you plan to load it entirely in, and you don't want secondary indexing on-disc (if you don't know what this is, then you don't need it!) , then you probably don't need a SQL database.

Personally I'd probably write a really stupid serialiser that dumps things into text-files. Text files are really good because they're easy to debug.

Advertisement

One thing I was really thinking about trying the next time I needed a lightweight persistence solution was to give Google's protocol buffers a shot.

It's pretty cool, example from wiki:

Message format:

message Point {
required int32 x = 1;
required int32 y = 2;
optional string label = 3;
}

message Line {
required Point start = 1;
required Point end = 2;
optional string label = 3;
}

message Polyline {
repeated Point point = 1;
optional string label = 2;
}

Then used in code:


#include "polyline.pb.h"  // generated by calling protoc polyline.proto (defined above)
 
Line* createNewLine(const std::string& name) {
  Line* line = new Line;
  line->mutable_start()->set_x(10);
  line->mutable_start()->set_y(20);
  line->mutable_end()->set_x(30);
  line->mutable_end()->set_y(40);
  line->set_label(name);
  return line;
}
 
Polyline* createNewPolyline() {
  Polyline* polyline = new Polyline;
  Point* point1 = polyline->add_point();
  point1->set_x(10);
  point1->set_y(10);
  Point* point2 = polyline->add_point();
  point2->set_x(10);
  point2->set_y(10);
  return polyline;
}

I don't know if OP is still with us at this point anyway...

Basically I just need a way to describe an item. Give it a name, what type of clothing it is(head piece, gloves, etc.), and what stat changes it provides. Then in the game be able to grab the information and use it to alter the player's stats.

If someone can point me in the right direction I'm sure I can figure out some way to implement this properly.

There are two problems.

1st: a data structure must hold this data.

2nd: the data must get there.

While some people wrote about RDBMS (what?) an other about writing yet another domain specific language (c'mon), I'd rather consider the design problem first.

From your message, I have the impression you're considering examples... as a problem X instead of a problem Y. In generic terms. That's not the case. The first thing you must do is to sit down, and figure out what the game needs.

So, you have RPG elements. At the basic level say we deal about characters, stats and stat-boosting equipment.

An initial design might be (some fat trimmed and shortcuts taken)


struct Stat {
  String name;
  uint value;
  std::vector<StatModifier> effects;
  uint GetCurrentValue() const {
    uint ret = value;
    foreach(e : effects) ret += e.GetModifiers();
    return ret;
  }
};

class Character {
  std::vector<Stat> stat;
  Character() {
    stat.push_back(Stat(L"Strength", 10));
    stat.push_back(Stat(L"Hit points", 6));
  }
}

So, when equipping a pair of gloves giving STR+4 we look up for a "Strength" attrib and add a modifier.

Notice that an armor giving COS+20 would be wasted on a vampire as it has no "Constitution" attribute (not in D&D rule set at least).

You must resolve all the features you need to make your game work before starting to write. You don't need to figure them out in detail, but at least know in what direction you need to go.

For example, being copy-driven, the above method has an issue. If a specific object is disabled (say by enemy magic), what modifier do we need to change?

Notice at this point however, getting data there is rather straightforward. I suggest JSON to do that (it will take you a day). A JSON representation of the two objects above might be


gloves {
  "name" : "Empowering gloves"
  "attribEffects" : [
    { "str" : "4" }
  ]
}
armor {
  "name" : "Golem skin"
  "attribEffects" : [
    { "cos" : "20" }
  ]
}

I'm making this up right away so I cannot guarantee it will go through a proper JSON parser. But I'm confident it will, as JSON is so easy it will make you sick.

Now the problem is:

  • data to json: just iterate the various properties
  • json to data: more involved, must figure out the data structure to use, multiple data paths required.

I guess it's better to stop there now. This is a really open and complex problem, we could go on for ages.

Previously "Krohm"

This topic is closed to new replies.

Advertisement