Prefabs
The Gorilla3D Prefab System allows you to save and load scene elements as reusable templates. A prefab stores a component hierarchy with all property values and child elements, from which you can instantiate copies at runtime. Prefabs are stored as DFM (Delphi Form) data, either in files or embedded directly.
Table of Contents
- Overview
- Getting Started
- Creating Prefabs
- Loading Prefabs
- Saving Prefabs
- Prefab Properties
- Working with Multiple Prefabs
- Best Practices
Overview
The prefab system consists of two classes:
| Class | Description |
|---|---|
TGorillaPrefabSystem |
Component that manages a collection of prefabs |
TGorillaPrefab |
Individual prefab template (collection item) |
Getting Started
Design-Time
- Drop a
TGorillaPrefabSystemonto your form - Open the
Prefabscollection in the Object Inspector - Add a new prefab item
- Set
OwnerComponentto the root component of your template - Set
FileNameto the file path for the prefab
Runtime
var
PrefabSys: TGorillaPrefabSystem;
Prefab: TGorillaPrefab;
begin
PrefabSys := TGorillaPrefabSystem.Create(Self);
Prefab := PrefabSys.AddPrefab('Tree', GorillaViewport1, 'prefabs/tree.prefab');
end;
Creating Prefabs
Saving a Scene Element as Prefab
// Add a prefab and link it to a component hierarchy
var LPrefab := PrefabSystem1.AddPrefab('Enemy', GorillaViewport1, '');
// Set the root component whose hierarchy will be saved
LPrefab.OwnerComponent := EnemyModel;
// Save to file
LPrefab.SaveToFile('prefabs/enemy.prefab');
// Or save to stream
var LStream := TMemoryStream.Create;
try
LPrefab.SaveToStream(LStream);
// use stream...
finally
LStream.Free;
end;
Embedded DFM Content
Prefabs can store DFM content directly instead of referencing a file:
LPrefab.DFM.Text := SomeDFMString; // DFM text content
When DFM content is set, Load() uses the embedded data instead of reading from file.
Loading Prefabs
From File
var
LComponents: TList<TComponent>;
begin
LComponents := PrefabSystem1.FindPrefab('Enemy').LoadFromFile('prefabs/enemy.prefab');
try
// LComponents contains all instantiated components
for var Comp in LComponents do
// position, configure, etc.
finally
LComponents.Free;
end;
end;
From Stream
LComponents := LPrefab.LoadFromStream(MyStream);
// For compressed .prefabz files:
LComponents := LPrefab.LoadFromStream(MyStream, True); // AIsPrefabZ = True
Load All Prefabs at Once
var AllComponents: TObjectList<TComponent>;
begin
AllComponents := TObjectList<TComponent>.Create(False);
try
PrefabSystem1.LoadAllPrefabs(AllComponents);
// AllComponents contains everything loaded from all prefabs
finally
AllComponents.Free;
end;
end;
Saving Prefabs
// Save individual prefab
LPrefab.Save();
LPrefab.SaveToFile('prefabs/enemy_v2.prefab');
// Save all prefabs
PrefabSystem1.SaveAllPrefabs();
Prefab Properties
TGorillaPrefab Properties
| Property | Type | Description |
|---|---|---|
DisplayName |
String | Name identifier for the prefab |
FileName |
String | File path (not auto-loaded on set) |
OwnerComponent |
TComponent | Root node of the prefab hierarchy |
DFM |
TStrings | Embedded DFM content (overrides file loading) |
Stored |
Boolean | Whether loaded components are stored in the owner's DFM |
WriteFormMethods |
Boolean | Write event properties linked to form methods |
IgnoreUnresolvedEvents |
Boolean | Silently ignore missing event methods (default: False) |
TGorillaPrefab Events
| Event | Type | Description |
|---|---|---|
OnLoaded |
TOnPrefabLoadedEvent |
Fired after prefab components are loaded |
OnStored |
TNotifyEvent |
Fired after prefab is saved |
OnCreateComponent |
TOnCreateComponentEvent |
Custom component creation callback |
TGorillaPrefabSystem Methods
function AddPrefab(AName: String; AOwner: TComponent; const AFileName: String): TGorillaPrefab;
procedure RemovePrefab(AName: String);
function FindPrefab(AName: String): TGorillaPrefab;
function IndexOfPrefab(AName: String): Integer;
procedure LoadAllPrefabs(var AList: TObjectList<TComponent>);
procedure SaveAllPrefabs();
Working with Multiple Prefabs
// Create a scene by loading multiple prefabs
var
LEnemy, LTree, LHouse: TGorillaPrefab;
begin
LEnemy := PrefabSystem1.AddPrefab('Enemy', GorillaViewport1, 'prefabs/enemy.prefab');
LTree := PrefabSystem1.AddPrefab('Tree', GorillaViewport1, 'prefabs/tree.prefab');
LHouse := PrefabSystem1.AddPrefab('House', GorillaViewport1, 'prefabs/house.prefab');
// Instantiate multiple enemies at different positions
for var I := 0 to 9 do
begin
var LComps := LEnemy.Load();
try
// Position each instance
if (LComps.Count > 0) and (LComps[0] is TControl3D) then
TControl3D(LComps[0]).Position.X := I * 5;
finally
LComps.Free;
end;
end;
end;
Best Practices
- Set
IgnoreUnresolvedEvents := Truewhen loading prefabs in a different context than where they were created - Use
OwnerComponentto define the parent for instantiated components - Prefab files use Delphi's DFM binary format — use
SaveToFile/LoadFromFilefor persistence - Compressed prefabs (
.prefabz) use ZIP compression for smaller file sizes - Keep prefab hierarchies self-contained — avoid cross-references to components outside the prefab
- Use the
OnLoadedevent to post-process instantiated components (apply materials, set positions)