PDA

View Full Version : Saving my classes into Stream



ysundawa
21-04-2004, 11:32 AM
I am creating a class of TMonsters uses for my RPG game,
and then make a component that having a property called Monsters which is a TCollection of TMonsters ...

I am tryoig to save the whole collection into stream or file but never successfull, here's my code :

TMonsterCollection = class(TComponent)
private
FMonsters : TBattleMonsters; //it is a collection of TMonsters
public
Constructor create(Aowner : TComponent); Override;
procedure SaveToStream(Stream: TFileStream);
procedure SaveToFile(AFileName: String);
procedure LoadFromStream(Stream: TFileStream);
procedure LoadFromFile(FileName: String);
published
property Monsters : TBattleMonsters read FMonsters write FMonsters;
end;


procedure TMOnsterCollection.SaveToStream(Stream: TFileStream);
begin
Stream.WriteComponentRes('ABCDE', Self);
end;

procedure TMOnsterCollection.SaveToFile(AFileName: String);
var
Stream : TFileSTream;
begin
Stream := TFileStream.Create(AFileName, fmCreate);
try
SaveToStream(Stream);
finally
Stream.Free;
end;
end;

Any vision please .... :cry:

cairnswm
21-04-2004, 12:39 PM
The way I do it is as follows

TMonsterRec = Record
Name, Type : Stirng;
End;

TMonster = Class
Data : TMonster;
End;

Load monsters

While not File.eof do
Begin
Monster := TMonster.Create;
Read(File,Monster.TMonster);
End;