1

I am trying to write a compound component which is derived from TDummy. The component source is:

  TMyObjectType=(otCube,otSphere);
  TMyGameObject=class(TDummy)
  private
    FObj:TCustomMesh;
    FMyObjectType: TMyObjectType;
    procedure SetMyObjectType(const Value: TMyObjectType);
  public
    constructor Create(AOwner:TComponent);override;
    destructor Destroy;override;
    property MyObjectType:TMyObjectType read FMyObjectType write SetMyObjectType;
  end;

{ TMyGameObject }

constructor TMyGameObject.Create(AOwner: TComponent);
begin
  inherited;
  MyObjectType:=otCube;
end;

destructor TMyGameObject.Destroy;
begin
  FObj.Parent.RemoveObject(FObj);
  FreeAndNil(FObj);
  inherited;
end;

procedure TMyGameObject.SetMyObjectType(const Value: TMyObjectType);
begin
  FMyObjectType := Value;
  if(Assigned(FObj))then begin
    FObj.Parent.RemoveObject(FObj);
    FreeAndNil(FObj);
  end;
  case FMyObjectType of
    otCube: FObj:=TCube.Create(Self);
    otSphere: FObj:=TSphere.Create(Self);
  end;
  FObj.SetSubComponent(True);
  FObj.Parent:=Self;
end;

after I register the component and put one instance on a TViewport3D in the code of a Tbutton I try to change the MyObjectType to otSphere.

MyGameObject1.MyObjectType:=otSphere;

but it seems there is nothing happening. So I wrote a piece of code as fallow.

procedure MyParseObj(obj:TFmxObject;var s:string);
var
  i: Integer;
  a:string;
begin
  s:=s+obj.ClassName+'(';
  a:='';
  for i := 0 to obj.ChildrenCount-1 do begin
    s:=s+a;
    MyParseObj(obj.Children.Items[i],s);
    a:=',';
  end;
  s:=s+')'
end;

and call it in another button.

procedure TForm1.Button2Click(Sender: TObject);
var s:string;
begin
  s:='';
  MyParseObj(myGameObject1,s);
  ShowMessage(s);
end;

the result was strange. if I press the button2 result is: TMyGameObject(TCube(),TCube())

and when I press the button1 and after that press button2 result is: TMyGameObject(TCube(),TSphere())

why there is two TCustomMesh as child in my object? (TCube and TSphere are derived from TCustomMesh) how can I fix this?

and there is another test that I performed. if I create the object not in design time it work properly. problem happens if I put an instance of TMyGameObject in design time.

Kromster
  • 7,181
  • 7
  • 63
  • 111
Loghman
  • 1,500
  • 1
  • 14
  • 30

1 Answers1

2

When you save a form (from the IDE) all controls and all their children are saved. If your control creates it's own children then you need to set Stored = False to prevent them being streamed by the IDE.

Mike Sutton
  • 4,191
  • 4
  • 29
  • 42