Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Binary "Database" for a game - Need a how-to =) 1

Status
Not open for further replies.

Acidon

Programmer
May 1, 2004
21
Hello and thanks in advance to anyone who offers help.

To make this simple, let's say I have a game which has 100 different monsters. I want a binary data file to hold all of these. It would have the index number of the mob, name, size, attributes, etc. I can easily create an editor to edit the records in this file, as soon as I know how to create this file and be able to find a specific monster in the file and fill, say, text boxes full of the information regarding that specific monster.

I hope I am writing this in a way that you can understand fully what I mean. Let's say I have a data file full of monsters, and the one I want to know about is in the middle of the file, say record number 33. I want to be able have my program look for the monster named "Dragon" and then retrieve all information that goes with that monster.

I was able to do this easily in VB6. I even made an editor that could go through the different monsters, with their information populating text boxes as I browsed through them. but for the life of me I can't figure out how to do this in C#. I know how to read and write binary files, but that's about it.

So, to my question. How could I do this? I want all records in a single binary file and the ability to locate specific ones and get their info.

Thanks in advance. I apologize for the book I wrote. I just wanted to make sure it was clear what I was looking for.

Daedric
 
Use the data file you already have, and make a class that stores and handles the data the same way your old VB program does. If you don't use a Type in VB for each monster, you've missed out on a logical way to store the info, and an easy algorithm to convert to C# from.

Lee
 
Thank you for the reply.

There is the problem. I don't know how to tell C# to read the the information on the second monster and so on. In VB, I was able to tell it which record to retrieve in the command that was reading the file, that is what made it easy in VB.

In VB, in a separate code file I made a Struct called MonsterData. In the struct were the various things that made up the records (such as MonsterName, MonsterSize, etc).

Before handling the data file I would create an instance of the struct and tell it, via the "read data file command", which record in the file to read, and threw the info into variables or into text boxes.

I am not very good with technical terms (as you can see), so hopefully you understand what I am trying to say when I describe these things.

I don't even know what you meant by, "If you don't use a Type in VB for each monster, you've missed out.."

Do you understand everything i've tried to explain, or am I hopeless? :)
 
A Type (note capitalization) is a data structure in VB. Same thing as a struct in C. C# has them as well, but you're better off using a class, for various reasons.

This is where I would say (uncomfortably) that while you're an expert in your game, you need some education in computer science, data structures, and program architecture. A decent place to look would be in the source code for one of id's games -- the Doom source is available on their site, and shows the use of datastructures to organize groups of related data.

I would also look at a book like Microsoft .NET Distributed Applications: Integrating XML Web Services and .NET Remoting ISBN: 0735619336

While it's not game-specific, it does show a good example of how to divide an application up into separate layers of responsibility, so that it becomes easier to understand and maintain.

Chip H.


____________________________________________________________________
Click here to learn Ways to help with Tsunami Relief
If you want to get the best response to a question, please read FAQ222-2244 first
 
Thanks chiph,

Don't hold back, I know that I am no programmer. This is pure fun for me. The games I develop will never see a computer besides mine.

At this point in time, I am not looking to master any languages, I am just looking to design and create some fantasy type games.

While I appreciate your honest and direct post, with suggestions on where to proceed in my C# learning, what I would really appreciate is a "picture", so to speak. :)

Please bear with me here. You have the ability to answer my question with ease and send me on my merry lil way.

Let's say I have want 10 monsters in a data file. each monster has:

-Number
-Name
-Hitpoints
-Power

I know how I can store all of these into a binary dat file, but here's what would really help me.

How can I store them so that I may access them in my program. Let's say I want to get monster #4, I want to get his name, hitpoints and power. How would I go about this? How can I tell C# to get that specific monster record out of that dat file?

Should I use a 2D array to hold everything and then write that to disk? Then when I read from the file, place the entire file back into a 2D array for use in my program?

Is there a better way? I know that I should be able to figure this out for myself, but to be honest I have been stuck here for 2 days. I just wanna make my dumb game. :)

If you could just provide me with the best way to go about this, and maybe even a snippet of code, I would be forever grateful. I know that I can deal with everything else that will come up in this program, it's just the data storage and retrieval that I have a problem with.

Thanks for putting up with my noobiness. =)

Acidon
 
Lots of ways you can do this.

If you have loads, then I'd create a packed file format and some kind of resource manager (or use one from the net)
i.e.
You could write a table at the start of your file, that had the name of each monster, and then a number. This number indicates the number of bytes into the file to read to start the data of your monster.

Or, if you dont have 1000's of monsters, why not read them all into memory in an array at the start of the game, memory access is quicker than disk access. You still need to know how to store each creature in the file.

In VB, how did you seperate the fields, was each field say 4 bytes long, so you know a creature was 16 bytes? or was there a lookup table, or a character that indicated the next record?
 
I would write a monster class that looked something like this:
Code:
[Serializable]
class Monster : ISerializable
{
  private int _number;
  private string _name;
  private int _hitPointsRemaining;
  private int _originalHitPoints;
  private int _power;

  public Monster()
  {
  }

  public Monster(int number)
  {
    _number = number;
    _name = string.Empty;
    _hitPointsRemaining = 0;
    _originalHitPoints = 0;
    _power = 0;
  }

  public int Number
  {
    get { return _number; }
    set { _number = value; }
  }
  //
  // more get/set properties here
  //

  protected Monster(SerializationInfo info, StreamingContext context)
  {
    _number = info.GetInt32("number");
    _name = info.GetString("name");
    _hitPointsRemaining = info.GetInt32("hitPointsRemaining");
    _originalHitPoints = info.GetInt32("originalHitPoints");
    _power = info.GetInt32("power");
  }

  [SecurityPermissionAttirbute(SecurityAction.Demand, SerializationFormatter=true)]
  public virtual void GetObjectData(SerializationInfo info, StreamingContext context)
  {
    info.AddValue("number", _number);
    info.AddValue("name", _name.ToString("{0,50}"));
    info.AddValue("hitPointsRemaining", _hitPointsRemaining);
    info.AddValue("originalHitPoints", _originalHitPoints);
    info.AddValue("power", _power);
  }
}
This class implements a custom serializer that gets called whenever the object is stored/read from disk. Note that the string property "Name" is serialized as a fixed-length string of 50 characters so that all records on disk are the same length.

To cause your object to be serialized, you create a BinaryFormatter, and pass it to it.

Note that the Serialize method wants a stream object. This can be any of the stream types in the System.IO namespace, but in your case using Stream will work.
Code:
File f = new File("c:\somefile.bin");
Stream s = f.Open(FileMode.Create);
Monster m = new Monster(100);
m.Name = "Scary Monster";
IFormatter formatter = new BinaryFormatter();
formatter.Serialize(s, m);
s.Close();
Because you'll want to have more than one monster in the file, you'll need a way to navigate to a particular monster. Use the Seek method on the Stream object for this. You'll need to know the size of your Monster object in order to calculate the correct offset. Once you've created your Monster object, create some temp code to serialize one to a file, and that will tell you the size.

Hope this helps. Obviously, there's a lot more to it than meets the eye, and learning the framework will take you far -- pretty much anything you'll need should be in there.

Chip H.


____________________________________________________________________
Click here to learn Ways to help with Tsunami Relief
If you want to get the best response to a question, please read FAQ222-2244 first
 
chiph,

Thank you very much. That is exactly what I was looking for (and a lot more). A lot of it I don't understand, but it will be fun researching this solution and it's part so that I know what I'm doing when I apply it to my program. Again, thank you for taking the time to do that for me. I have you a nifty star too. :)



Kalisto,

It would take a lot of space to show you the basics of how I did this in VB6. I was actually pretty proud of myself back then when I came up with the whole system for it. :) If you like, I can email or message you with some of the code if you really want to take a look. I'm sure I can dust it off if you want.


Daedric

"Non-Programmers think programming is hard.
Average programmers think programming is easy.
Good programmers KNOW programming is hard."
-Unknown
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top