Organizing data efficiently

Started by
5 comments, last by Zahlman 16 years, 2 months ago
I'd like to have a resource text file such that I can clearly and easily define and later change game large amounts of game data. I don't want to save this as a file that the user can easily change as I'm working on a multiplayer game. So what I would like to do is add a .txt resource file to my compiler (using VC++ 2008), then read this file and fill my class based on the info in the file. So my text file would look something like: Name...........Damage....Cost....Level Iron Sword........2.........250.....2 Wooden Sword...1..........50.....1 Steel Sword.......4.........550.....4 And then I would fill a class such as... class Item { char Name[50]; int Damage, Cost, Level; }; So what I'm tring to figure out is how do I get my compiler to read this resource file... any help would be appreciated... Thanks, Vanz [Edited by - rhuala on February 13, 2008 4:41:17 PM]
Advertisement
1) Do you already know how to read it from a plain file on disk? It won't be any easier from a resource.
2) Think about how to format the data to make it easier to read.
3) In C++, we use std::string to represent strings.
4) Extracting a resource file from an .exe isn't particularly difficult. Meanwhile, if this is going to be something run client-server where you're in control of the server (MMO style), then the obvious solution is to put it in a text file on the server.
5) Note that with a resource, you'll still have to rebuild every time you change the resource, which somewhat defeats the purpose of pulling it out into a file.
Quote:Original post by Zahlman
5) Note that with a resource, you'll still have to rebuild every time you change the resource, which somewhat defeats the purpose of pulling it out into a file.
I was going to point out the same thing... if you are building it into the exe anyway you might as well just explicitly initialize the structures in your code somewhere.
I use XML for my data files. It is a format that's structured and lets you maintain any kind of data easily, and you don't have to waste time writing a parser -- just use tinyxml, which only requires you to add 2 files to your project.

If it's a multiplayer game, most data files should be on the server, though. You should only distribute files that really don't matter to your users.
If someone changes the string "steel sword" to "widow maker", people may have a laugh, but no harm is taken. On the other hand, if a weapon's damage or required level is stored client-side, you'll be in trouble. This shouldn't ever be in a client-side file, not even in a file that is "not easy to change".
Original post by smitty1276
Quote:Original post by Zahlman
if you are building it into the exe anyway you might as well just explicitly initialize the structures in your code somewhere.


But this gets harder to read then a nice text file... I find a formatted text file is easy to read, update and edit - takes is a bit more coding working up front but to me it's worth it...

Quote:
1) Do you already know how to read it from a plain file on disk? It won't be any easier from a resource.


How do you read it from a resource please?

Thanks,

Vanz
http://msdn2.microsoft.com/en-us/library/ms632583(VS.85).aspx

You should be aware that storing your data as resource doesn't offer any protection at all. Anyone can read or edit that data with tools like PE resource explorer. The only advantage is that everything is one file. Apart from that, it's only more work and more painful to maintain.

If your in-memory structure looks like what you posted above, it's fixed size, and contains no pointers etc., so writing out the objects to a file once they're in memory is trivial, as is reading them back into memory.
If you make a "data file compiler" that reads in a human-readable file and writes out the binary structures, possibly adding some form of scrambling or encryption, and a checksum, then you have good maintainability and some degree of security (perfect security isn't possible anyway).
Like I said, XML has the nice property that it "just works", you don't need to write a parser or anything.
Quote:Original post by rhuala
Quote:if you are building it into the exe anyway you might as well just explicitly initialize the structures in your code somewhere.


But this gets harder to read then a nice text file... I find a formatted text file is easy to read, update and edit - takes is a bit more coding working up front but to me it's worth it...


Your apparent goals are (1) easy to read, (2) easy to edit, (3) hard for the end user to hack.

Text file: yes, yes, no.
Hard-coded: maybe, no, sort of.
Resource file: yes, not really, barely.

This topic is closed to new replies.

Advertisement