How to code game data

Started by
6 comments, last by DekuTree64 10 years, 6 months ago

Hello!

At the begining I'd like to present myself as this is my first post here. I'm Tomek [Tom, Tomas etc], I'm 18 from Poland, I know some of C++, mostly basics.

I just have couple of questions that I can't find clear answer for it, or maybe just can't find it.

1. How do I code game inventory just like RPG games?

2. How to create item data?

I was trying to create it in dat file. Using fstream for that, but I find it difficult to manage line by line, it's not that simple.

I just want to know if I'm going right way with fstream, maybe there are bettter ways you used?

What I had in my mind, I want it to be sorted by, for example: ID / Name / Level / Class... so each item has their own properties.

I was trying to make registration system and stuff, but I can't get SQLite lib to work with VS12 [keep getting errors] so I will leave that for later.

I want to mention I just need key words from you, so I can google and learn it. I don't need you to explain me every detail, just if I will have troubles in understading I will ask you to do. This is not gonna be game at the begining, it's just for experiencing. Not gonna go farer than console app at the moment.

Thank you in advance.

Advertisement

Google 'serialization', 'JSON', 'XML'

A player's inventory could be as simple as: std::vector<YourItemClass>

I'd also like to add, if your intent is to have a fairly extensive database of items, you're absolutely going to want some form of easy to use item editor utility that will allow you to easily add items to your database and edit items in your database. This idea eventually extends to nearly every aspect of your game. Class editors, monster/npc editors, level editors, quest system editors, etc. These are preferably all wrapped up into a single game editor that gets used to create all the various data that composes the tangibles of your game.

You should also look into the factory pattern (and abstract factory pattern) for a popular method of wrapping and abstracting these content databases from your game code.

Most large games I've worked on do it like this:

1. The data is entered into a spreadsheet or SQL database using whatever app you want (Excel, OO, Google Spreadsheet, SQL). You can lay this out however you want (simple row/column grid all the way up to a fully normalized database).

2. The spreadsheet or database is scanned for the data, and stored however you feel is most appropriate for your game (custom binary form, protobuf, XML, JSON, etc). This step typically takes database-style table-based data (i.e. if an inventory item has a variable length list of crafting ingredients, the crafting ingredients would be normalized as a separate sheet/table in the input spreadsheet/database) and turns it into more typical hierarchical data structures (i.e. "Item.CraftingIngredients[0]").

3. The game loads the data into the data structure you feel is most appropriate for your game (a simple list, an associative array with unique IDs, a full-on SQLite database, etc). Excessively large data sets may be hosted on a web service that can be fetched from 100 items (or so) at a time.

The large games I've worked on have all used Excel spreadsheets for data entry and associative arrays (Dictionary<string,InventoryItem>) for runtime storage.

Most large games I've worked on do it like this:

1. The data is entered into a spreadsheet or SQL database using whatever app you want (Excel, OO, Google Spreadsheet, SQL). You can lay this out however you want (simple row/column grid all the way up to a fully normalized database).

2. The spreadsheet or database is scanned for the data, and stored however you feel is most appropriate for your game (custom binary form, protobuf, XML, JSON, etc). This step typically takes database-style table-based data (i.e. if an inventory item has a variable length list of crafting ingredients, the crafting ingredients would be normalized as a separate sheet/table in the input spreadsheet/database) and turns it into more typical hierarchical data structures (i.e. "Item.CraftingIngredients[0]").

3. The game loads the data into the data structure you feel is most appropriate for your game (a simple list, an associative array with unique IDs, a full-on SQLite database, etc). Excessively large data sets may be hosted on a web service that can be fetched from 100 items (or so) at a time.

The large games I've worked on have all used Excel spreadsheets for data entry and associative arrays (Dictionary<string,InventoryItem>) for runtime storage.

So from my understanding, please correct me if I'm wrong, I should start off writing the item data in app like excel [just example, excel seems to be less complicated than sql - I cant get the SQLite lib to work with my IDE], then it's being scanned for data and stored in XML file [aswell example, I already went deeper into XML] and then the data is loaded in game.

Just a question, how is the XML handling data from excel?

Usually you have to save the Excel file out as some other format before beginning step 2. Dealing with Excel's native file format is kind of a pain, and writing an Excel plugin is no fun either.

The easiest is probably CSV, but "XML Spreadsheet 2003" (XMLSS) is also pretty easy to read and supports more of Excel's formatting information.

Step 2 consists more-or-less of the following sub-steps:

2a. Open the file in Excel, then "Save As" using the file type drop-down "XML Spreadsheet 2003". If you want, you can permanently keep the Excel spreadsheet in this format for editing as well, but it only supports a subset of Excel's features. For simple data entry and calculations, it will work fine.
2b. Use a program that you've written to load the .xml file, read each worksheet, row, and cell.
2c. Convert the data however you need to.
2d. Save the converted data in a file that your game will load later.


You'll want to examine the XMLSS file by hand to familiarize yourself with the format, and then write a loader which can read the portions you care about. There are a bunch of sections you won't care about at all. It's fairly human readable. The spec is here in case you need it: http://msdn.microsoft.com/en-us/library/office/aa140066(v=office.10).aspx

One important keyword for you, aside from the already mentioned by above posters, is hash table (hash map).

I was going to recommend an SQL library for C++ that was a single file, a single header to be precise, you could even copy and paste it on your program. But apparently I lost its name. I'll never forgive myself if I don't find its name...

You might find this interesting: http://www.boost.org/doc/libs/1_53_0/libs/serialization/doc/index.html

I just make an enum of item ID values, a table of structs matching up to it where I enter data for each item, and then the player's inventory can be one of two styles:

a) The player can have at most 99 of each item, but can have 99 of every item in the game all at the same time (no inventory limit). The inventory is just an array of bytes that corresponds to the item ID enum, which tells how many of each item the player currently owns. When displaying the inventory, don't show any that are 0. Final Fantasy 6 does this style (although it lets you change the display order)

b) Each slot in the inventory is an item ID and number of how many. In this style you can carry more than 99 of the same item type, by having multiple bundles (e.g. 99 cure potions in one slot, 20 more in another slot). Most games that use this style have a fairly limited number of slots, so you often have to sell old weapons and armor to make room for new ones. Final Fantasy 4 does this style. Some games, such as Earthbound on SNES, only let you store one item in each slot. So if you have two cookies, it takes two slots. And you only have 20 slots or something total. Too frustrating, IMO.

This topic is closed to new replies.

Advertisement