Asset

Implements: tracker.Observable

Base class for all asset types in Ceramic.

Assets represent loadable resources like images, fonts, sounds, etc. This class provides common functionality including:

  • Path resolution based on density and variants
  • Reference counting for memory management
  • Hot reload support
  • Asset lifecycle management

Asset subclasses should override the load() method to implement specific loading logic for their asset type.

See: Assets

Instance Members

observedDirty: Bool

Default is false, automatically set to true when any of this instance's observable variables has changed.


kind: String

Asset kind identifier (e.g., 'image', 'font', 'sound'). Used to categorize assets and determine loading behavior.


name: String

Asset name without extension or variant. Setting this triggers path recomputation.


variant: String

Optional variant suffix for the asset. Useful for loading different versions of the same asset (e.g., 'large', 'small').


fullName: String

Full asset identifier including variant if provided. Format: 'name' or 'name:variant'


path: String

Resolved file path for this asset. Automatically computed based on name, variant, density, and available files.


allPaths: Array<String>

All available file paths for this asset across different densities. Useful for preloading multiple resolutions.


density: Float

Asset target density matching the best available file. Automatically set based on screen density and available asset files. Default is 1.0.


owner: Assets

The Assets instance that owns this asset. When the owner is destroyed, all its assets are destroyed too.


runtimeAssets: RuntimeAssets

Optional runtime assets configuration for dynamic asset loading. Used to compute paths when loading assets from custom directories.


options: AssetOptions

Asset-specific loading options. Content depends on asset type and backend implementation. Common options include premultiplyAlpha for images, streaming for sounds, etc.


assets: Assets

Sub-assets owned by this asset. Some assets (like bitmap fonts) create this to manage their dependencies. Automatically destroyed when the parent asset is destroyed.


refCount: Int

Reference count for memory management.

  • Call retain() to increase (claim ownership)
  • Call release() to decrease (release ownership)
  • Asset can be safely destroyed when refCount reaches 0
var texture = assets.texture('hero');
texture.retain(); // refCount = 1
// ... use texture ...
texture.release(); // refCount = 0, can be cleaned up

status: Anonymous

Current loading status of the asset. Observable property that triggers updates when status changes.

  • NONE: Not loaded
  • LOADING: Currently loading
  • READY: Successfully loaded
  • BROKEN: Failed to load

invalidateStatus(): Void

load(): Void

Load the asset. Subclasses must override this method to implement actual loading logic. Should set status to LOADING during load, then READY or BROKEN when complete. Must call emitComplete() when finished.


destroy(): Void

Destroy this asset and clean up resources.

  • Removes from owner Assets instance
  • Destroys any sub-assets
  • Should not be called directly if refCount > 0

computePath(?extensions: Array<String>, ?dir: Bool, ?runtimeAssets: RuntimeAssets): Void

Compute the best file path for this asset based on available files and screen density. Automatically called during initialization and when properties change.

Name Type Default Description
extensions Array<String> (optional) File extensions to look for (auto-detected if not provided)
dir Bool (optional) Whether to look for directories instead of files
runtimeAssets RuntimeAssets (optional) Runtime assets configuration to use

retain(): Void

Increase the reference count by 1. Call this when you start using an asset to prevent it from being destroyed. Must be balanced with a corresponding release() call.

See: release

release(): Void

Decrease the reference count by 1. Call this when you're done using an asset. When refCount reaches 0, the asset can be safely destroyed.

Warning: Calling release() when refCount is already 0 will log a warning.

See: retain

new(kind: String, name: String, ?variant: String, ?options: Null<AssetOptions>): Void

Create a new asset.

Name Type Default Description
kind String The asset type identifier
name String The asset name (without extension)
variant String (optional) Optional variant suffix
options Null<AssetOptions> (optional) Loading options specific to the asset type

Private Members

unobservedStatus: Anonymous

handleTexturesDensityChange: Bool

hotReload: Bool

customExtensions: Array<String>

emitObservedDirty(instance: Asset, fromSerializedField: Bool): Void

Event when any observable value as changed on this instance.

Name Type
instance Asset
fromSerializedField Bool

emitComplete(success: Bool): Void

Emitted when the asset finishes loading.

Name Type Description
success Bool True if the asset loaded successfully, false if it failed

emitStatusChange(current: Anonymous, previous: Anonymous): Void

Event when status field changes.

Name Type
current Anonymous
previous Anonymous

texturesDensityDidChange(newDensity: Float, prevDensity: Float): Void

Called when screen texture density changes. Subclasses can override to handle density changes (e.g., reload at new resolution).

Name Type Description
newDensity Float The new texture density
prevDensity Float The previous texture density

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

Called when watched asset files change on disk. Subclasses can override to implement hot reload behavior.

Name Type Description
newFiles ReadOnlyMap<String, Float> Map of file paths to modification times after change
previousFiles ReadOnlyMap<String, Float> Map of file paths to modification times before change

toString(): String

String representation of the asset for debugging.

Returns Description
String String in format "AssetType(name:variant path)" or "AssetType(name:variant)"

willEmitComplete(success: Bool): Void
Name Type
success Bool

Metadata

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