Author: Jonas Bilinkevicius
I am writing an adventure game and need to store information in a save game. The
game requires data from 3 different records and one variable
record1 = hotspot scene information(50 recs),
record2 = conversation information (60 recs),
record3 = hypertext information(50 recs)
variable = integer - # of scene currently on.
My problem is that I need to seek for a particular record of particular type in the
file (I do not want to have to keep huge arrays of records in memory). I know how
to do this with a file containing records of only one record type but have no clue
how to combine all three records and one integer into a single random access file.
Answer:
I generally use a file with a header, then just keep the header in memory and use
it to seek to the records I need.
1 type2 TSaveHeader = record3 scene: Integer;
4 hotspots: LongInt;
5 talk: LongInt;
6 hype: LongInt;
7 end;
8 9 var10 SaveHeader: TSaveHeader;
11 12 procedure OpenSaveFile(fname: string);
13 var14 f: file;
15 i: Integer;
16 begin17 AssignFile(f, fname);
18 Reset(f, 1);
19 BlockRead(f, SaveHeader, Sizeof(TSaveHeader));
20 { get one set of records }21 Seek(f, SaveHeader.hotspots);
22 for i := 1 to 50 do23 BlockRead(f, somevar, sizeof_hotspotrec);
24 { and so on }25 CloseFile(f);
26 end;
27 28 { assuming the file is open }29 30 procedure GetHotspotRec(index: LongInt; var hotspotrec: THotspot);
31 var32 offset: LongInt;
33 begin34 offset := SaveHeader.hotspots + index * Sizeof(THotSpot);
35 Seek(f, offset);
36 BlockRead(f, hotspotrec, Sizeof(THotspot));
37 end;