Debian Codegraph @

tests/delphi_tests/testcastleserialization.pas

raw
1 unit TestCastleSerialization;
2
3 interface
4
5 uses SysUtils, Classes, CastleComponentSerialize, CastleClassUtils;
6
7 procedure TestRTTICastle;
8
9 implementation
10
11 uses CastleLog;
12
13 type
14 TFakeCastleScene = class(TCastleComponent)
15 private
16 FURL: String;
17 published
18 property URL: String read FURL write FURL;
19 end;
20
21 procedure TestRTTICastle;
22 var
23 Scene: TFakeCastleScene;
24 SceneOwner: TComponent;
25 begin
26 // InitializeLog;
27
28 RegisterSerializableComponent(TFakeCastleScene, 'FakeScene');
29
30 { Create TCastleTransform instance, with a TCastleScene child. }
31 Scene := TFakeCastleScene.Create(nil);
32 Scene.Name := 'MyScene'; // will enable to find it later with FindRequiredComponent
33 Scene.URL := 'castle-data:/teapot.x3dv';
34
35 { Save it to file. }
36 ComponentSave(Scene, 'aaa.castle-transform');
37
38 { You can destroy the instances now. }
39 FreeAndNil(Scene);
40
41 { Create a component that will own all loaded instances,
42 allowing to easily free them,
43 and to use FindRequiredComponent. }
44 SceneOwner := TComponent.Create(nil);
45
46 { Now you can load them from file, and check that they are equal. }
47 Scene := ComponentLoad('aaa.castle-transform', SceneOwner) as TFakeCastleScene;
48 WritelnLog('Found scene in loaded transform, with url %s', [
49 Scene.Url
50 ]);
51
52 { Free loaded stuff. }
53 FreeAndNil(SceneOwner);
54 end;
55
56 end.