FragmentsAsset

EntityAssetceramic.FragmentsAsset (Class)

An asset that loads and manages fragment data from .fragment files.

Fragments in Ceramic are reusable groups of visuals and entities that can be instantiated from data files. They support:

  • Multiple visual elements and entities with properties
  • Timeline-based animations with keyframes
  • Components that can be attached to fragment items
  • Hot-reloading for development

Fragment files can be in two formats:

  1. Legacy format: Direct JSON representation of FragmentData objects
  2. Version 1 format: Structured format with schema support that gets converted at load time

Example usage:

// Load a fragments asset
var fragmentsAsset = new FragmentsAsset('myFragments.fragment');
fragmentsAsset.onComplete(this, success -> {
    if (success) {
        // Access fragment data
        var menuFragment = fragmentsAsset.fragments.get('mainMenu');
        // Create a Fragment instance from the data
        var fragment = new Fragment();
        fragment.fragmentData = menuFragment;
    }
});
fragmentsAsset.load();
See: Fragment The runtime representation of fragment data, FragmentData The data structure for fragments, FragmentItem Individual items within a fragment

Instance Members

A map of fragment IDs to their corresponding FragmentData objects. This property is populated after successfully loading the fragments file. Each fragment can be accessed by its ID and used to create Fragment instances.

The property is observable, so you can react to changes when fragments are loaded:

fragmentsAsset.onFragmentsChange(this, (fragments, prevFragments) -> {
    if (fragments != null) {
        // Fragments loaded successfully
        for (id in fragments.keys()) {
            trace('Loaded fragment: ' + id);
        }
    }
});

invalidateFragments(): Void

load(): Void

Loads the fragments file from the specified path.

The loading process:

  1. Loads the JSON file as text
  2. Parses the JSON data
  3. Detects the format version (legacy or version 1)
  4. Converts version 1 format to runtime format if needed

Supports hot-reload on platforms that allow it - the file will be automatically reloaded when it changes on disk.


destroy(): Void

Destroys the fragments asset and clears the loaded fragment data from memory.


new(name: String, ?variant: String, ?options: Null<AssetOptions>): Void
Name Type Default
name String
variant String (optional)
options Null<AssetOptions> (optional)

Private Members

unobservedFragments: haxe.DynamicAccess<FragmentData>

fromRawFragments(rawFragments: Dynamic): haxe.DynamicAccess<FragmentData>

Converts version 1 fragment format to the runtime format.

This method handles the transformation of the newer fragment file format that includes additional metadata and schema information. It processes:

  • Fragment metadata (id, dimensions, color, transparency)
  • Visual and entity items with their properties
  • Component configurations
  • Schema references for entity types
Name Type Description
rawFragments Dynamic The raw parsed JSON data with version field
Returns Description
haxe.DynamicAccess<FragmentData> Map of fragment IDs to FragmentData objects

emitFragmentsChange(current: haxe.DynamicAccess<FragmentData>, previous: haxe.DynamicAccess<FragmentData>): Void

Event when fragments field changes.

Name Type
current haxe.DynamicAccess<FragmentData>
previous haxe.DynamicAccess<FragmentData>

assetFilesDidChange(newFiles: ReadOnlyMap<String, Float>, previousFiles: ReadOnlyMap<String, Float>): Void

Called when asset files change on disk (hot-reload support). Automatically reloads the fragments file if it has been modified.

Name Type Description
newFiles ReadOnlyMap<String, Float> Map of current files and their modification times
previousFiles ReadOnlyMap<String, Float> Map of previous files and their modification times

Metadata

Name Parameters
:build tracker.macros.EventsMacro.build()
:autoBuild tracker.macros.EventsMacro.build()
:build ceramic.macros.EntityMacro.buildForCompletion()
:autoBuild ceramic.macros.EntityMacro.buildForCompletion()
:build tracker.macros.ObservableMacro.build()
:autoBuild tracker.macros.ObservableMacro.build()