DynamicData
A flexible component for attaching arbitrary data to entities.
DynamicData provides a way to associate any type of data with an entity without modifying the entity's class. This is useful for storing metadata, configuration, or temporary state that doesn't warrant a dedicated component.
Features
- Lazy Initialization: Data object created only when accessed
- Type Flexible: Can store any Dynamic data structure
- Component Pattern: Can be attached to any entity
- Memory Efficient: No allocation until data is needed
Usage Examples
// Attach custom data to an entity
var sprite = new Quad();
var data = new DynamicData({
health: 100,
speed: 5.0,
name: "Player"
});
sprite.component("dynamicData", data);
// Access data later
var data = sprite.component("dynamicData", DynamicData);
trace(data.data.health); // 100
// Lazy initialization
var emptyData = new DynamicData();
emptyData.data.score = 0; // Creates {} automatically
Common Use Cases
- Game object properties (health, score, inventory)
- UI element configuration
- Temporary animation state
- Debug information
- Plugin-specific data
Instance Members
hasData: Bool
Whether this component currently has data assigned. Returns true if data has been set or accessed (triggering lazy init).
data: Dynamic
The dynamic data stored in this component.
On first access, automatically initializes to an empty object {} if no data was previously set. This allows for convenient property assignment without null checks.
var dynData = new DynamicData();
dynData.data.score = 100; // Auto-creates {}
dynData.data.name = "Player";
// Or set entire object
dynData.data = {
x: 100,
y: 200,
items: ["sword", "shield"]
};
entity: Entity
initializerName: String
Creates a new DynamicData component.
Name | Type | Default | Description |
---|---|---|---|
data |
Dynamic | (optional) | Optional initial data to store. If not provided, data will be lazily initialized on first access. * haxe // With initial data var data1 = new DynamicData({level: 1, xp: 0}); * // Without initial data (lazy init) var data2 = new DynamicData(); |
Private Members
bindAsComponent(): Void
Called when this component is bound to an entity. Currently empty as DynamicData doesn't require special binding logic.
Name | Type |
---|---|
entity |
Entity |
getEntity(): Entity
Returns |
---|
Entity |
Metadata
Name | Parameters |
---|---|
:build |
ceramic.macros.ComponentMacro.build() |
:autoBuild |
ceramic.macros.ComponentMacro.build() |
:build |
ceramic.macros.EntityMacro.buildForCompletion() |
:autoBuild |
ceramic.macros.EntityMacro.buildForCompletion() |
:build |
tracker.macros.EventsMacro.build() |
:autoBuild |
tracker.macros.EventsMacro.build() |