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: Integer;
Position: TPoint;
Blocked: Boolean;
end;
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));
//Itterate through each tile and save it to a file
For X:=Low(Map) To High(Map) Do begin
BlockWrite(F,Map[X,Y],SizeOf(TTile);
end;
CloseFile(F);
end;
procedure LoadMap(Filename: String);
var
F: File;
X,Y: Integer;
begin
AssignFile(F,Filename)
Reset(F,1);
//Set map to correct size
BlockRead(F,L,SizeOf(Integer));
SetLength(Map,L);
//Itterate through each tile and load it from a file
For X:=Low(Map) To High(Map) Do begin
BlockRead(F,Map[X,Y],SizeOf(TTile);
end;
CloseFile(F);
end;
begin
SetLength(Map,1024);
SaveMap('Map.Dat');//or - LoadMap('Map.Dat') - 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.
Bookmarks