Examples » Engine » Assets » Loading Reflectable Assets

Loading reflectable assets from JSON.

We'll use the following type as an example:

struct Strings
{
    CUBOS_REFLECT;
    std::vector<std::string> strings;
};

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

Then, we must register a bridge for this type. We provide JSONBridge for easily loading and saving reflectable assets as JSON.

    cubos.startupSystem("setup bridge to load .strings files").tagged(assetsBridgeTag).call([](Assets& assets) {
        assets.registerBridge(".strings", std::make_unique<JSONBridge<Strings>>());
    });

With the bridge registered, we can just load it from its handle:

    static const Asset<Strings> SampleAsset = AnyAsset("6f42ae5a-59d1-5df3-8720-83b8df6dd536");

    cubos.startupSystem("access .strings asset").tagged(assetsTag).call([](Assets& assets) {
        auto read = assets.read(SampleAsset);
        for (const auto& str : read->strings)
        {
            Stream::stdOut.printf("String: {}\n", str);
        }
    });

These sytems are configured the usual way, as explained in Introduction and Custom Bridges.