Using a database in a game or just multiple objects?

Started by
11 comments, last by Krohm 10 years, 12 months ago

I'm working on a game in SDL and C++ with RPG aspects, such as stats, and gear. What I am confused about is how to implement so many different types of gear with different stats. Such as , should each different type of item be a new object in the programming, or should is it somehow possible to create a database I can use and update at later times when I want to add new items to the game. I've also heard about scripting and XML but I am just not sure where to go with this. (I'm still pretty new to all this, all I've done so far is Blackjack, tetris, and pong.)

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.

This is my first original game idea and I'm still in the planning stages really. Thank you for any advice and help!

Advertisement

If it's a one man project (i.e. you are the coder and you're also the person who's going to be setting the values for all your items), then personally, I wouldn't bother trying to do anything fancy with spreadsheets, XML or databases. I'd just define the tables of data directly in code. I would, however, make sure that access is strictly controlled through functions so that I could later on implement loading later on if it becomes worthwhile.

The moment you go data driven, there's a moderate amount of work writing code to load in your data files and to check for errors (missing fields, etc) which you can simply skip by keeping it simple. However, if you do decide to do it properly, then all approaches are fine, but I'd probably recommend using a spreadsheet, which you can save out as a .csv file (comma separated values), and load that in code. The loading code can be fiddly, but a spreadsheet is a reasonably nice way to view and edit your data, and you might find uses for formula functionality (e.g. calculating total cost of all your items to assist with balancing your economy).

Databases for games are a bit taboo unless you're talking like a dedicated server. Databases are just really bulky constructs designed for constant pulling and pushing of data like happens in an MMO or something, for a standalone game where you're just adding data you're not really going to be doing it on the fly so using just files would probably be best.

Keep in mind files are just data, if you're worried about simplicity of editing you can always design a tool for editing the files that mimicks what you would get from a database basically.

That said the above poster has some valid points, going file driven usually means writing up a fancy editor and data formats, it's -much- easier to put everything in code unless you're working on a team or something that needs immediate access to tweak data, or it's a particularly LARGE amount of items or something that you're editing on a regular basis.

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.

Just use regular structures and enums.

Have one enum for type of the item : enum EObjectType {None=0, Weapon, Armour, Potion }

And have one struct that will keep all the relevant info about the item (whether it is a weapon, armour or potion):

struct SObject

{

int ID; // global unique ID for the object - autoincrementing it, when filling up the internal DB is enough

EObjectType ObjectType;

.....

int AddToStrength;

int AddToDexterity;

int AddToAccuracy;

...

int RequiredStrength;

int RequiredDexterity;

int RequiredAccuracy;

}

Then, when using the item from the inventory, you just add those AddToStrength/Dexterity/Accuracy values to your stats values and you are done.

Personally, I am using text files for the database of the game objects. I wrote a very simple parser in an afternoon and never had to touch it since, so I guess it was a reasonable effort. This way, I can keep all my weapons, armours, potions in separate *.TXT files that are very easy to edit in Notepad.

When loading all items from the file, I just put them to a std::vector <SObject> Weapons, std::vector<SObject>Armour, std::vector<SObject> Potions. This way they are easy to work with (iterate, search, add/remove).

There is one more important advantage for having the "database" of items in TXT files - when you are testing, you will want to have multiple sets of these files so that you can test the different gameplay due to different objects/stats.

It will prove invaluable, when all the testers need to do, is just to replace the TXT files with a different set of TXT files. Even if that tester is just you.

VladR My 3rd person action RPG on GreenLight: http://steamcommunity.com/sharedfiles/filedetails/?id=92951596

I would consider using JSON. There are a ton of parses available in dozens of languages, it's a simple format that solves many if the encoding files you could run into.

Think of JSON like a light weight ,vastly saner XML

Use http://www.sqlite.org/

Anybody telling you that databases for games in any capacity are "too heavy" or "taboo" doesn't have experience writing real-world performance-critical applications reliant on volumes of data that need to be organized in a coherent manner.

SQL Lite is going to solve your problem.

Software Engineer | Credited Titles: League of Legends, Hearthstone

SQLite is also going to cause several new problems, eg. how to get data into the database in the first place, and then how to get a row of loosely-typed data into a class or struct in your game. Once you write all the code to convert back and forth, you may find you've wasted a lot of time compared to just storing classes in a std::map.

Use http://www.sqlite.org/

Anybody telling you that databases for games in any capacity are "too heavy" or "taboo" doesn't have experience writing real-world performance-critical applications reliant on volumes of data that need to be organized in a coherent manner.

SQL Lite is going to solve your problem.

Just because it is possible, doesn't mean it is actually a good idea.

I do happen to have the experience you are talking about and that is exactly the reason why I would never even think about it.

That is, of course, unless you are being paid by an hour and your goal is to make it take as long to implement as possible, so that you could invoice more.

Which, I think, is not the case here with the OP...

VladR My 3rd person action RPG on GreenLight: http://steamcommunity.com/sharedfiles/filedetails/?id=92951596

A database in the sense of a SQL Server installation or a MySQL installation would not only be massive overkill for your needs, but also wouldn't offer any compelling advantages over the simpler solutions in your case. I would take the approach recommended several times already and store all your item data in JSON text files. There are lots of great, simple parsers out there (such as picojson if you're using C++). A SQLite database may be a reasonable compromise between the two, but I still wouldn't recommend it in this case. It does not seem warranted.

This is really funny.

We already spent more time arguing than it should take him to write a simple text parser smile.png

VladR My 3rd person action RPG on GreenLight: http://steamcommunity.com/sharedfiles/filedetails/?id=92951596

This topic is closed to new replies.

Advertisement