Unit CastleComponentSerialize

Description

Reading and writing of hierachy of CGE components (TCastleUserInterface, TCastleTransform) to and from files. This is used by CGE editor to read/write components, and it can be used at runtime by games to instantiate components designed in CGE editor.

Uses

Overview

Classes, Interfaces, Objects and Records

Name Description
Class EInvalidComponentFile  
Class TRegisteredComponent Describes a component registered using RegisterSerializableComponent, enumerated using RegisteredComponents list.
Class EComponentNotFound  
Class TComponentHelper  
Class TSerializedComponent Load the serialized component once, instantiate it many times.

Functions and Procedures

procedure TransformSave(const T: TCastleTransform; const Url: String);
function TransformLoad(const Url: String; const Owner: TComponent): TCastleTransform;
procedure UserInterfaceSave(const C: TCastleUserInterface; const Url: String);
function UserInterfaceLoad(const Url: String; const Owner: TComponent): TCastleUserInterface;
procedure ComponentSave(const C: TComponent; const Url: String);
function ComponentLoad(const Url: String; const Owner: TComponent): TComponent;
function ComponentToString(const C: TComponent): String;
function StringToComponent(const Contents: String; const Owner: TComponent): TComponent;
procedure RegisterSerializableComponent(const ComponentClass: TComponentClass; const Caption: String);
procedure RegisterSerializableComponent(const C: TRegisteredComponent);
function RegisteredComponents: TRegisteredComponents;

Types

TRegisteredComponents = specialize TObjectList<TRegisteredComponent>;

Description

Functions and Procedures

procedure TransformSave(const T: TCastleTransform; const Url: String);

Save / load TCastleTransform (or descendant) to a .castle-transform file.

An example:

{ TransformSave and TransformLoad example. }
uses SysUtils, Classes,
  CastleLog, CastleVectors, CastleTransform, CastleScene, CastleComponentSerialize;
var
  Scene: TCastleScene;
  Transform: TCastleTransform;
  TransformOwner: TComponent;
begin
  InitializeLog;

  { Create TCastleTransform instance, with a TCastleScene child. }
  Scene := TCastleScene.Create(nil);
  Scene.Name := 'MyScene'; // will enable to find it later with FindRequiredComponent
  Scene.Load('castle-data:/teapot.x3dv');
  Transform := TCastleTransform.Create(nil);
  Transform.Translation := Vector3(1, 2, 3);
  Transform.Add(Scene);

  { Save it to file. }
  TransformSave(Transform, 'aaa.castle-transform');

  { You can destroy the instances now. }
  FreeAndNil(Scene);
  FreeAndNil(Transform);

  { Create a component that will own all loaded instances,
    allowing to easily free them,
    and to use FindRequiredComponent. }
  TransformOwner := TComponent.Create(nil);

  { Now you can load them from file, and check that they are equal. }
  Transform := TransformLoad('aaa.castle-transform', TransformOwner);
  WritelnLog('Loaded transform, with translation %s, with %d children', [
    Transform.ToString,
    Transform.Count
  ]);
  Scene := TransformOwner.FindRequiredComponent('MyScene') as TCastleScene;
  WritelnLog('Found scene in loaded transform, with url %s', [
    Scene.Url
  ]);

  { Free loaded stuff. }
  FreeAndNil(TransformOwner);
end.

function TransformLoad(const Url: String; const Owner: TComponent): TCastleTransform;
 
procedure UserInterfaceSave(const C: TCastleUserInterface; const Url: String);

Save / load TCastleUserInterface (or descendant) to a .castle-user-interface file.

function UserInterfaceLoad(const Url: String; const Owner: TComponent): TCastleUserInterface;
 
procedure ComponentSave(const C: TComponent; const Url: String);

Save / load TComponent (or descendant) to a .castle-user-interface or .castle-transform file.

Usually it is more comfortable to use stronger typed UserInterfaceSave, UserInterfaceLoad, TransformSave, TransformLoad.

function ComponentLoad(const Url: String; const Owner: TComponent): TComponent;
 
function ComponentToString(const C: TComponent): String;

Save / load TComponent (or descendant) to a string. The string contents have the same format as a .castle-user-interface or .castle-transform file.

function StringToComponent(const Contents: String; const Owner: TComponent): TComponent;
 
procedure RegisterSerializableComponent(const ComponentClass: TComponentClass; const Caption: String);

Register a component that can be serialized and edited using CGE editor.

In case of the overloaded version that gets TRegisteredComponent instance, the TRegisteredComponent instance becomes internally owned in this unit (do not free it yourself).

procedure RegisterSerializableComponent(const C: TRegisteredComponent);
 
function RegisteredComponents: TRegisteredComponents;

Read-only list of currently registered (using RegisterSerializableComponent) components.

Types

TRegisteredComponents = specialize TObjectList<TRegisteredComponent>;
 

Generated by PasDoc 0.15.0.