DatabaseAsset
Asset for loading CSV database files.
DatabaseAsset loads CSV (Comma-Separated Values) files and parses them into an array of dynamic objects, where each row becomes an object with properties based on the CSV headers.
Features:
- Automatic CSV parsing with header detection
- Support for both comma and semicolon separators
- Quoted string handling with escape sequences
- Hot-reload support for database files
- Each row accessible as a dynamic object
CSV Format:
- First row must contain column headers (field names)
- Supports comma (,) or semicolon (;) as separators
- String values can be quoted with double quotes
- Use "" to escape quotes within quoted strings
// Load a CSV database
var dbAsset = new DatabaseAsset('enemies');
dbAsset.path = 'data/enemies.csv';
dbAsset.onComplete(this, success -> {
if (success) {
// Access the parsed data
for (row in dbAsset.database) {
trace('Enemy: ${row.get("name")} HP: ${row.get("hp")}');
}
// Find specific entries
var boss = dbAsset.database.find(row -> row.get("type") == "boss");
}
});
assets.addAsset(dbAsset);
assets.load();
Example CSV content:
name,hp,damage,type
Goblin,10,5,normal
Orc,20,10,normal
"Dragon King",100,50,boss
Instance Members
database: Array<haxe.DynamicAccess<String>>The parsed database as an array of dynamic objects. Each object represents a row, with properties matching the CSV headers. Will be null until the asset is successfully loaded.
Access values using the get() method:
var name = row.get("name");
var hp = Std.parseInt(row.get("hp"));
invalidateDatabase(): Voidload(): VoidLoads the CSV database file from the specified path.
The file is loaded as text and then parsed using the CSV parser. The parsing automatically detects whether comma or semicolon is used as the separator based on the first line.
On success, the database property will contain the parsed data. On failure (file not found or parse error), the asset status becomes BROKEN.
destroy(): VoidDestroys the database asset and clears the loaded 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
unobservedDatabase: Array<haxe.DynamicAccess<String>>emitDatabaseChange(current: Array<haxe.DynamicAccess<String>>, previous: Array<haxe.DynamicAccess<String>>): VoidEvent when database field changes.
| Name | Type |
|---|---|
current |
Array<haxe.DynamicAccess<String>> |
previous |
Array<haxe.DynamicAccess<String>> |
assetFilesDidChange(newFiles: ReadOnlyMap<String, Float>, previousFiles: ReadOnlyMap<String, Float>): VoidCalled when asset files change on disk (hot-reload support). Automatically reloads the database if its CSV file 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() |