Results 1 to 10 of 10

Thread: Saving data

  1. #1

    Saving data

    Hey everyone! Glad the sites back up...


    Ive got a bit of a question... How would you guys go about saving data to a file (for say data for a Map file for your game)? My map file uses alot of dynamic arrays so you cant just save the type or anything. How i've gone about doing it is simply setting up a string and filled it with all the data i need, split up by dilimeters i can later split when i need to load, and just save that string's data. Is there a better way to do this? Maybe more professional and not so tacky.


    Thanks!
    I have a 2005 CRF 250 so <^>(>&lt<^>
    <br />http://www.gtrpg.com/

  2. #2

    Saving data

    Use blockwrite and blockread to save the dynamic array into a binary file.
    I don't remember whether blockread automatically resizes the array when you read a block?
    Look it up at google and you'll find loads of examples
    Peregrinus, expectavi pedes meos in cymbalis
    Nullus norvegicorum sole urinat

  3. #3
    Legendary Member cairnswm's Avatar
    Join Date
    Nov 2002
    Location
    Randburg, South Africa
    Posts
    1,537

    Saving data

    I use a TStringList descendant that functions as a Database. I use the Name part of the string as a Table.Field.PK structure and the Value as the value of the field

    So for example a user would be represented as
    User.Name.1=William
    User.Surname.1=Cairns
    User.Location.1=South Africa

    And it works well. For Maps (Mine are static arrays) I just use Text Files.
    William Cairns
    My Games: http://www.cairnsgames.co.za (Currently very inactive)
    MyOnline Games: http://TheGameDeveloper.co.za (Currently very inactive)

  4. #4

    Saving data

    as JSoftware says, use BlockRead/BlockWrite. You will need to SetLength your array when loading as it will not do this automatically. Thing to do is to save the size of the array at the start of the file, when it comes to loading, load the array size value and use SetLength to set array to the right size. Use AssignFile to initialise the file and then use Reset and Rewrite commands. Reset loads an existing file (Use this on your loading routine). Rewrite creates or overwrites an exisiting file (Use this on your save routine).

    Here's a quick example

    Code:
    Type
      TTile = Record
        ImageIndex&#58; Integer;
        Position&#58; TPoint;
        Blocked&#58; Boolean;    
      end;
    
    var
      Map&#58; Array Of TTile;
    
    procedure SaveMap&#40;Filename&#58; String&#41;;
    var
      F&#58; File;
      X,Y&#58; Integer;
      L&#58; Integer;
    begin
      AssignFile&#40;F,Filename&#41;
      Rewrite&#40;F,1&#41;;
    //Write the map size to file
      L&#58;=Length&#40;Map&#41;;
      BlockWrite&#40;F,L,SizeOf&#40;Integer&#41;&#41;;
    //Itterate through each tile and save it to a file
      For X&#58;=Low&#40;Map&#41; To High&#40;Map&#41; Do begin
        BlockWrite&#40;F,Map&#91;X,Y&#93;,SizeOf&#40;TTile&#41;;
      end;
      CloseFile&#40;F&#41;;
    end;
    
    procedure LoadMap&#40;Filename&#58; String&#41;;
    var
      F&#58; File;
      X,Y&#58; Integer;
    begin
      AssignFile&#40;F,Filename&#41;
      Reset&#40;F,1&#41;;
    //Set map to correct size
      BlockRead&#40;F,L,SizeOf&#40;Integer&#41;&#41;;
      SetLength&#40;Map,L&#41;;
    //Itterate through each tile and load it from a file
      For X&#58;=Low&#40;Map&#41; To High&#40;Map&#41; Do begin
        BlockRead&#40;F,Map&#91;X,Y&#93;,SizeOf&#40;TTile&#41;;
      end;
      CloseFile&#40;F&#41;;
    end;
    
    begin
      SetLength&#40;Map,1024&#41;;
      SaveMap&#40;'Map.Dat'&#41;;//or -   LoadMap&#40;'Map.Dat'&#41;    - whatever.
    end;
    These are pretty basic map load/save methods but they should work, bearing in mind I'm writing this from memory. A couple of things to remember is that you can NOT write a string to file using BlockWrite(F,MyString,SizeOf(MyString))
    because in reality a String is just a pointer and this will simply write the pointer location to file which will change every time the program is run. For string you must itterate through each character and use BlockWrite to write each character in sequence. Don't forget to SetLength the string at the start though. In fact, writing a string to file is a lot like writing a map to file. Anyway, this is the best way I've found of doing things. Hope all this makes sense and if anyone wants to correct me on any of this then be my guest.



    EDIT Had two loops in there beforehand, was using a 2 dimensional static array but realised you wanted it dynamic. Fixed the code now. Well spotted JSoftware.
    Isometric game development blog http://isoenginedev.blogspot.com/

  5. #5

    Saving data

    just to add to master Crisp_n_Dry's example then it can be done much shorter![pascal]
    Type
    TTile = Record
    ImageIndex: Integer;
    Position: TPoint;
    Blocked: Boolean;

    var
    Map: Array Of TTile;

    procedure SaveMap(Filename: String);
    var
    F: File;
    X,Y: Integer;
    L: Integer;
    begin
    AssignFile(F,Filename)
    Rewrite(F,1);
    //Write the map size to file
    L:=Length(Map);
    BlockWrite(F,L,SizeOf(Integer));
    BlockWrite(F,Map[0],l*SizeOf(TTile);
    CloseFile(F);
    end;

    procedure LoadMap(Filename: String);
    var
    F: File;
    X,Y: Integer;
    begin
    AssignFile(F,Filename)
    Reset(F,1);
    BlockRead(F,L,SizeOf(Integer));
    SetLength(Map,L);
    BlockRead(F,Map[0],l*SizeOf(TTile);
    CloseFile(F);
    end;

    begin
    SetLength(Map,1024);
    SaveMap('Map.Dat');//or - LoadMap('Map.Dat')
    end; [/pascal]

    as for the string saving and loading just define a new type like this:

    [pascal]
    type binarystring = string[1000]; //or what size you want
    [/pascal]

    Well the example i made was only valid if you are using a one dimensional array. if you use multidimensional then you have to go with crisp_n_dry's example though it can still be shortened down to one for loop

    Regards
    Jeppe
    Peregrinus, expectavi pedes meos in cymbalis
    Nullus norvegicorum sole urinat

  6. #6

    Saving data

    Thanks guys
    I have a 2005 CRF 250 so &lt;^&gt;(&gt;&lt&lt;^&gt;
    <br />http://www.gtrpg.com/

  7. #7

    Saving data

    You could also use streams instead of BlockRead/BlockWrite as they are abstract, compatible with all stream types (file, memory, resource ...), they are also CrossPlatform and supported by FPC.
    You can also subclass them and make custom streams for various data.
    The future must be... Fast and OpenSource so...
    <br />Think Open and Lightning Fast!

  8. #8

    Saving data

    I prefer to use TFilestreams, when working with files. Gives you more control of the data, and works really well with dynamic arrays, and TMemoryStreams

  9. #9

    Saving data

    how does the tstringlist work? is it a component? I'm new to delphi & dont have enough knowledge on strings.

  10. #10

    Saving data

    the tstringlist class is.. a class. it's a non vcl/lcl class, which means, no it's not a component: you have to initialize it as a variable. ex:

    [pascal]procedure tform.form1oncreate(sender: TOBject);
    var stringlist: tstringlist;
    begin
    stringlist := tstringlist.create;
    stringlist.add('Jeppe');
    stringlist.add('is');
    stringlist.add('pretty cool');
    stringlist.savetofile('testfile.txt');
    stringlist.free;
    end;[/pascal]

    this would output:

    Jeppe
    is
    pretty cool

    to testfile.txt in the same dir as the application is in. this data can be loaden in using the stringlist.loadfromfile(filename here); procedure you just have to remember to create the stringlist first. and nearly more important destroy it afterwards(we're afterall speaking gamedev on this site )

    Happy coding

    Sincerely,
    Jeppe

    EDIT: D'oh.. just saw the other thread...
    Peregrinus, expectavi pedes meos in cymbalis
    Nullus norvegicorum sole urinat

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •