![]() |
Re: Speichern verschiedener Objekte in einer Datei
Okay, ich poste mal die gesamte Unit, die für das Speichern der Objekte verantwortlich ist. Der restliche Code befindet sich in anderen Units, dieser ist hier aber nicht relevant.
Ist ganz schön lang, ich wäre aber sehr dankbar, wenn ihr mal drüber schauen könntet, da ich wirklich ein wenig auf dem Schlauch stehe. Es geht darum, eine Objektlist ("BrickPrototypes"), die mit Objekten vom Typen "TBrickPrototype" gefüllt ist zu speichern und zu laden. Um den Vorgang des Speicherns und Ladens vom restlichen Programm strickt zu trennen, speichere ich die Klasse "TBrickPrototype" nicht direkt, sondern gehe den Umweg über die Klasse "TBrickCollItem", die von TPersistent abgeleitet ist. Außer den beiden Arrays habe ich alle restlichen Parameter weggelassen, da diese problemlos funktionieren. Die Klasse "TFileControler" ist die Klasse, die meine Objektlist "BrickPrototypes" als Collection speichert und aus der Datei wiederherstellt. Mein Problem findet ihr zwei Posts weiter oben. Aber ich wiederhole meine Frage nochmal: Hab ich da irgendwas falsch verstanden und kann das folglich so gar nicht funktionieren, oder ist da nur irgendwo ein kleiner Fehler drin? Wäre auch für sonstige Verbesserungsvorschläge dankbar.
Delphi-Quellcode:
type
//Objektlist mit Objekten dieses Typs soll gespeichert werden!! TBrickPrototype = class(TObject) {aus anderer Unit} private public textures: array of TTargaGraphic; animationVelocities: array of Real; end; //Subcollections: TRealCollItem = class(TCollectionItem) private fRealValue: Real; public procedure Assign(Source: TPersistent); override; published property RealValue: Real read fRealValue write fRealValue; end; TTargaCollItem = class(TCollectionItem) private fTarga: TTargaGraphic; public procedure Assign(Source: TPersistent); override; published property Targa: TTargaGraphic read fTarga write fTarga; end; //Maincollection: TBrickCollItem = class(TCollectionItem) private fTargaCollItems: TJSCollection; fRealCollItems: TJSCollection; public constructor Create(Collection: TCollection); override; destructor Destroy; override; procedure Assign(Source: TPersistent); override; published property TargaCollItems: TJSCollection read fTargaCollItems write fTargaCollItems; property RealCollItems: TJSCollection read fRealCollItems write fRealCollItems; end; //Administration: TFileControler = class(TOBject) private public procedure LoadBricks; //Bricks from File -> global TObjectList procedure SaveBricks; end; implementation uses UBrickEditor_Main; { TRealCollItem } procedure TRealCollItem.Assign(Source: TPersistent); begin inherited; if Source is TRealCollItem then fRealValue := TRealCollItem(Source).RealValue else inherited Assign(Source); end; { TTargaCollItem } procedure TTargaCollItem.Assign(Source: TPersistent); begin inherited; if Source is TRealCollItem then fTarga := TTargaCollItem(Source).Targa else inherited Assign(Source); end; { TBrickCollItem } constructor TBrickCollItem.Create(Collection: TCollection); begin inherited Create(Collection); fTargaCollItems := TJsCollection.Create(TTargaCollItem); fRealCollItems := TJsCollection.Create(TRealCollItem); end; destructor TBrickCollItem.Destroy; begin fTargaCollItems.free; fRealCollItems.free; inherited Destroy; end; procedure TBrickCollItem.Assign(Source: TPersistent); begin inherited; if Source is TBrickCollItem then begin fTargaCollItems.Assign(TBrickCollItem(Source).fTargaCollItems); fRealCollItems.Assign(TBrickCollItem(Source).fRealCollItems); end else inherited Assign(Source); end; { TFileControler } procedure TFileControler.LoadBricks; var Coll: TmxJSCollection; tempBrickPrototype: TBrickPrototype; i, j: integer; begin Coll := TmxJSCollection.Create(TBrickCollItem); Coll.LoadFromFile(filename); BrickPrototypes.Clear; for i:=0 to Coll.Count-1 do begin for j:=0 to TBrickCollItem(Coll.Items[i]).TargaCollItems.Count-1 do begin setlength(tempBrickPrototype.textures, length(tempBrickPrototype.textures)+1); tempBrickPrototype.textures[high(tempBrickPrototype.textures)] := TTargaCollItem(TBrickCollItem(Coll.Items[i]).TargaCollItems).Targa; end; {} for j:=0 to TBrickCollItem(Coll.Items[i]).RealCollItems.Count-1 do begin setlength(tempBrickPrototype.animationVelocities, length(tempBrickPrototype.animationVelocities)+1); tempBrickPrototype.animationVelocities[high(tempBrickPrototype.animationVelocities)] := TRealCollItem(TBrickCollItem(Coll.Items[i]).RealCollItems).RealValue; end; BrickPrototypes.Add(tempBrickPrototype); end; {for i...} Coll.free; end; procedure TFileControler.SaveBricks; var Coll: TmxJSCollection; i,j : integer; begin coll := TmxJSCollection.Create(TBrickCollItem); for i:=0 to BrickPrototypes.Count-1 do begin with Coll.add as TBrickCollItem do begin for j:=0 to high(TBrickPrototype(BrickPrototypes[i]).textures) do with TargaCollItems.add as TTargaCollItem do Targa := TBrickPrototype(BrickPrototypes[i]).textures[j]; for j:=0 to high(TBrickPrototype(BrickPrototypes[i]).animationVelocities) do with RealCollItems.add as TRealCollItem do RealValue := TBrickPrototype(BrickPrototypes[i]).animationVelocities[j]; end; {with Coll.add...} end; {for i...} Coll.SaveToFile(filename); Coll.free; end; end. |
Alle Zeitangaben in WEZ +1. Es ist jetzt 17:04 Uhr. |
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024-2025 by Thomas Breitkreuz