Sign in to follow this  

RPG Item Database?

This topic is 2660 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Basically what I'm trying to do is make a database of all the possible items in the text RPG I'm making. The idea is to prevent me from typing redundant data.

At first, I tried a vector by push_back-ing the name then continuing to do so for each stat. So example, starting at 1, every 7 including and after that you'll always get the name of an item. Then starting at 2 and every 7 including and after that you'll always get the HP bonus, or lack of, of an item, etc. But I couldn't figure out how to run the result of the iterator through an if statement(so as not to display anything like "HP: 0").

Then I thought of making a class for them but the idea of having 500+ class objects while only using maybe 15 of them seemed extremely inefficient and I just threw that idea out. I feel like there's a more efficient way of implementing it via classes but I don't know how.

I then found linked lists, which seemed perfect. However after halfway implementing it, I realized that you can't random access it. So if I had 500 items, and the item I wanted was number 250, I would have to cycle through 250 items just to get to the one I wanted. Again seems too inefficient.

At this point, the time I've spent I could've just put in all the redundant data. lol. But I would like to know what the proper way of implementing this is. So my question is what's the most efficient and/or the proper way of doing this? I don't need any code written out, just point me in the right direction please. :D

Share this post


Link to post
Share on other sites
You seem to be confusing the concept of classes and data. If you can make the assumption in your vector-based implementation that the data you care about always contains 7 identical elements (or that it is acceptable for some properties to be "null" values) then you only need a single class that can be duplicated to hold any number of items you wish. In fact, if the assumption holds that any item has the same set of properties associated with it (Name, HP buffs, MP buffs, etc) then a a vector of such classes is a reasonable solution. Since you probably don't want to hard-code 500 items into the source code, you can use a simple file format to contain all item definitions and load them in when the game starts (presuming you actually need/want to keep the whole database in memory). You could use a spreadsheet program to create the file and save it to an easy-to-read format like .CSV, which is simply a textual list of values where each property is separated by a comma and each item is separated by a newline character -- super easy to read. If you don't have a spreadsheet program like Microsoft Excell, there are many free ones available (OpenOffice.org) or even free online ones like Google's -- you don't even have to install anything. Heck, its deadly simple to even create a CSV file in any text editor even.

Other format possibilites include XML, YAML, JSON, or any custom format you design yourself. Heck, you could even use an actuall embedded database like SQLlite -- many games these days use SQLlite for lots of things -- not just item inventory but for all kinds of data like NPC/Baddie stats, dialogue, sound effects and even saved games.

For runtime-storage, I might recommend something other than a basic vector though, just to optimize lookup performance. You might consider a hash map, a set, a sorted vector or binary tree or trie structure, and using the name as the key/lookup value (presuming names are globally unique).

Now, if items can have wildly-varied and arbitrary properties, then things become interesting. Then you would probably want something like a std::map where the key is the item name, and the "value" is another std::map -- in this second map the key value might then be the property name, and the value is some numeric type (probably double if both integer and floating point values are possible) assuming numeric data is sufficient -- otherwise it could be something like Boost's Any type.

There are other ways of organizing things, of course, but if you need more complexity than I've described here you're probably best off using SQLlite or some other embedded database.

Share this post


Link to post
Share on other sites

This topic is 2660 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this