Examples » Engine » Assets » Creating and Saving

Creating and saving assets.

This example demonstrates how a new asset can be created programatically and how it can be saved to the assets directory, which is useful while working on tools such as TESSERATOS

Before we go any further, if we want to save assets to the filesystem, we must allow assets to be modified. This is done through the following setting:

    cubos.startupSystem("configure Assets plugin").tagged(settingsTag).call([](Settings& settings) {
        // If we want to save assets, we must set this to false.
        settings.setBool("assets.io.readOnly", false);

We'll use the following asset type as an example, with a JSONBridge registered for it with the extension .int.

struct IntegerAsset
{
    CUBOS_REFLECT;
    int value;
};

CUBOS_REFLECT_IMPL(IntegerAsset)
{
    return Type::create("IntegerAsset").with(FieldsTrait{}.withField("value", &IntegerAsset::value));
}

First, we'll create an asset of this type:

    cubos.startupSystem("create and save asset").tagged(assetsTag).call([](Assets& assets) {
        // Create a new asset (with a random UUID).
        auto handle = assets.create(IntegerAsset{1337});

Then, we'll assign it a path and save it. It's important that the path ends with the correct extension, so that Assets knows which bridge to use when loading it.

        assets.writeMeta(handle)->set("path", "/assets/sample/sample.int");
        assets.save(handle);

With this, the files sample/sample.int and sample/sample.int.meta should have appeared on the assets/ directory. The .meta file contains the UUID of the asset, which is used by the engine to identify it.

Try running the sample yourself to see the files being created!