c++ and Excel?

Started by
16 comments, last by ericrrichards22 8 years, 12 months ago
Hi!
I am working on a Inventory and loot system for a Text-based RPG.
I want to use an excel spreadsheet in order to list my items and stats modifiers.
My goal to be able to read and write items and variables to the spreadsheet and make an easy and more accessable way of editing the loot system.
My thought is to have some sort of Random generated stat to all items and a drop rate system.
Kind of like the drop system in diablo 1.
Are there any tutorials that might shine a bit more light on the subject?
So far I've only found tutorials on how to read/write strings to excel files, but never a tutorial on how to handle integers in such way.
I've also heard that managing .xlsx are quite hard and that i should use .csv instead.
Since i'm more of like a beginner at programming (I've managed to make a fully working text-based RPG though. Trying to advance by finding
different solutions and making the game more complicated such as in-game currency rates.) i need to find something that isn't to advanced to understand.
Advertisement
Don't use excel.

You're going to be using a sledgehammer to crack a peanut. A simple ini file will suffice and it won't require the same level of development.

Also to read excel you usually need to use COM which means that the user of your game will need a license for office. In my opinion that's a big assumption to make because office isn't essential.

Also office is about as performant as a snail stuck in salty treacle. It won't scale for a game inventory.

This seems to be two issues, using Excel to populate data used by your game, and having Diablo-style random generated loot at game run-time.

In the first place you could use xlsx spreadsheets to keep track of the data and when it's ready for your game, export snapshots of your spreadsheet to a csv files. Then your game would read the csv file in and treat the data as integers or text as you deem appropriate. It depends on exactly what benefits you think using Excel gives you, since you could use a text file if you wanted to.

The second issue, having Diablo-style random generated loot, is a combination of data and code. If you want a weapon to have range of stats randomly assigned you can use the spreadsheet to give a weapon a certain spread. Weapon damage (or whatever) could be assigned a low value of 10 and a high value of 15. Then your code would read that in from the spreadsheet and generate the random number between 10 and 15. You might want to look up into the different random number distributions. For example, say you want your items to have a high probability of being 10 and lower probabilities of getting higher numbers. You could use a Poisson distribution for this. Or you want a high probability of 12 occurring and numbers lower/higher than 12 being less and less likely the further you get form 12 (a binormal distribution).

C++: A Dialog | C++0x Features: Part1 (lambdas, auto, static_assert) , Part 2 (rvalue references) , Part 3 (decltype) | Write Games | Fix Your Timestep!

Following nobodynews, herw's what you could do (/ what I do):

- make stuff, order data in your Excel file
- copy paste all columns and rows to an ascii file (or export/ use csv)
- simply read in the ascii file, for example with a standard ifstream

This works pretty easy and fine, just make sure not to put rubbish between the logical data.
A column switch in excel is exactly like a tab in ascii and a row like a newline

Crealysm game & engine development: http://www.crealysm.com

Looking for a passionate, disciplined and structured producer? PM me

Don't make your game read Excel files. Way too complicated. Do what the previous posters said and export from Excel to a CSV file or something simple like that. Then convert THAT into a binary form that your game actually loads.

It is also possible to create a custom exporter for Excel, if csv doesn't cut it for some reason.

Office plugin development can now be done for free - first, download the Visual Studio 2013 Community Edition, and then the Office Tools for VS 2013.

That said, the CSV way is somewhat easier if it fits your needs :)

Niko Suni

The xlsx format is not impossible to work with either. It is effectively a zip file that contains XML files (and media) that describe the document contents.

The format is not as trivial to read as a flat file, though. One of the reasons for the complexity is that you have a "shared string" table which is used to look up strings for the cell values. The values of cells that have a type of "s" need to be looked up from said table using an ordinal number stored with the cell.

All this means that you can't simply enumerate over the sheets like flat records. Which is what a format like CSV trivially enables.

Niko Suni

Sure if you want to just read the raw content, using tools for excel is fine. However if you want formulas to be evaluated as you read cell values you must use excel itself via interop or COM. Excels xml format is dog ugly and not straightforward at all. As others have said you are best sticking with csv.
Excel does cache calculated values with the cell entities, but I agree: Csv or similar would probably work better in this case.

Niko Suni

As previous commenters stated, excel may not be the right format for your data. I also think, that CSV or a custom format (maybe XML/JSON mixed with CSV) would be a better choice.

But for future reference (or if you really want to use excel for you inventory system), there exists a Qt5 library for reading and writing from/to excel spreadsheets (.xlsx files): QtXlsx. The library doesn't need office to read spreadsheets and is distributed under the MIT license (see its documentation), so you don't have to worry about licensing issues. I have not tried the library, but it looks capable enough to parse spreadsheets correctly. It depends on you if you want to introduce Qt5 as a dependency though.

This topic is closed to new replies.

Advertisement