Good day!
Is I understand right, that DesignedComponent is function of the TCastleView means that I can load disigned components only if Iuse child class of TCastleView?
For example this is class of inventory display.
For now I use too much code to group it all rightly. So, if I create this class in special unit and want to construct in CGE Editor, I have only one approach:
TInventoryDisplay = class(TCastleView)
When I did it and run project I get message:
DesignedComponent can only be used if the design was loaded, which means that TCastleView has started and DesignUrl is not empty.
There are few parts of code:
constructor TInventoryDisplay.Create(AOwner: TComponent);
begin
inherited;
DesignUrl := 'castle-data:/gameviewmain.castle-user-interface';
end;
procedure TInventoryDisplay.Start;
begin
inherited;
RotationButton := DesignedComponent('Button') as TCastleButton;
ControlArea.InsertFront(RotationButton);
RotationButton.OnClick := @RotateInventory;
end;
I must admit I do not fully understand what exactly you are trying to achieve (in other words, how exactly you envision usage of TInventoryDisplay) . Guessing from the function names, this is a single inventory item, right?
One additional question is how to get access to the objects “inside” of design from code?
I can’t just create Design and use the button, I must manually create Button and set they personal URL from Design?..
Sorry, if I explained a little confused. For Example, I need to use ControllButton from here:
var
MyDesign: TCastleDesign;
MyButtonFromDesign: TCastleButton;
begin
MyDesign := TCastleDesign.Create(...);
MyDesign.Url := 'castle-data:/button_design.castle-user-interface';
MyButtonFromDesign := MyDesign.DesignedComponent('MyButton') as TCastleButton;
// remember to add it to some parent, e.g. from view, to make it visible
SomeParent.InsertFront(MyDesign);
2.2. Or you can deserialize the design files using UserInterfaceLoad. You then set at owner of the loaded things, and you can use FindRequiredComponent on t, like this:
var
OwnerOfLoadedComponents: TComponent;
MyDesignRoot: TCastleUserInterface;
MyButtonFromDesign: TCastleButton;
begin
OwnerOfLoadedComponents := TComponent.Create(...);
MyDesignRoot := UserInterfaceLoad('castle-data:/button_design.castle-user-interface', OwnerOfLoadedComponents);
MyButtonFromDesign := OwnerOfLoadedComponents.FindRequiredComponent('MyButton') as TCastleButton;
// remember to add it to some parent, e.g. from view, to make it visible
SomeParent.InsertFront(MyDesignRoot);
I would usually recommend using TCastleDesign over UserInterfaceLoad, the API is a bit more straightforward, though deep down they do the same thing.