Writing Classes to File

Chad J gamerChad at _spamIsBad_gmail.com
Mon May 8 20:16:21 PDT 2006


I think you might want to check out http://www.runuo.com/
It emulates a server for the massively multiplayer game Ultima Online. 
I mention it only because it has a system for doing just this sort of 
thing - they call it serializing (saving) and deserializing (loading). 
Their setup may not be any better than the struct thing, but it is an 
alternative, and it's worked for this very large project.  I say it may 
not be any better because I have had my share of frustration with 
serializing and deserializing.  If you want, I'll write an example, but 
it would be quite lengthy (and I have differential equations homework to 
do) so maybe later if you say you want it.

That struct thing is interesting, and I'd like to see what kind of ideas 
there are for handling this task.

Also, beware the object reference...

class Mobile
{
    struct fields
    {
        int x, y;
        int health;

        char[] name;           // <---   There be monsters here!
        Container backpack;    // <---
    }
    fields myVars;

    ...

    void saveTo(BufferedFile someFile) {
        someFile.writeExact(&myVars, myVars.sizeof);

        // better add something like:
        someFile.writeExact(myVars.name.ptr, myVars.name.length);
        backpack.saveTo(someFile);
    }
    // careful when loading...
}

class Container : Item
{
     struct fields
     {
         Item[] contents;
         ...
     }
     fields myVars;
     ...
}

For handling the references, BCS, did you come up with something 
different by any chance?



More information about the Digitalmars-d mailing list