Fetch metadata in game from multiple sources

In this How-to, we will fetch all metadata matching a specific key on multiple items.

Prerequisites

Fetching Metadata from multiple sources

When fetching a several metadata, you need to provide 3 things:

  • A ulid: (can be retrieved through the web console or in the SDK's depending on the feature) of the item to fetch metadata for

    • For example: 01J96M0BX1GGTPP2QDRCBV3FW2

  • A key: of the desired metadata

    • For example: icon

  • A source type

When fetching metadata by tags, a key will supersede the tag, so if you want to search by tags it’s best to avoid using keys and vice versa. When fetching metadata by tags you will get all metadata on that object with the same corresponding tag.

// Create an array of sources and keys that we want to fetch
LootLockerMetadataSourceAndKeys[] sourcesAndKeys = new LootLockerMetadataSourceAndKeys[]{
    new LootLockerMetadataSourceAndKeys
    {
        source = LootLockerMetadataSources.currency,
        id = "01J7S0CED5K0THCB1ENN9XKQWZ",
        keys = new string[] { "icon" }
    },
    new LootLockerMetadataSourceAndKeys
    {
        source = LootLockerMetadataSources.catalog_item,
        id = "01J9RE51T32WNXSQADNJ5RGQCX",
        keys = new string[] { "icon", "featured" }
    }
};

// Base64 can be set to content_type "application/x-redacted", use this to avoid accidentally fetching large data files
bool ignoreFiles = true;

LootLockerSDKManager.GetMultisourceMetadata(sourcesAndKeys, (response) =>
{

    if (response.success)
    {
        // Handle the received metadata differently depending on the use cases
        foreach (var entry in response.Metadata)
        {
            // Currency
            if (entry.source == LootLockerMetadataSources.currency)
            {
                // Get the first entry as a base64 (file), this is an image so we convert it to a sprite to be displayed
                LootLockerMetadataBase64Value base64String = null;
                entry.entries[0].TryGetValueAsBase64(out base64String);
                byte[] imageBytes = Convert.FromBase64String(base64String.content);
                Texture2D tex = new Texture2D(291, 302);
                tex.LoadImage(imageBytes);
                tex.filterMode = FilterMode.Point;
                Sprite sprite = Sprite.Create(tex, new Rect(0.0f, 0.0f, tex.width, tex.height), new Vector2(0.5f, 0.5f), 100.0f);
                // Add your own code here to display the sprite
            }

            // Catalog Item
            if (entry.source == LootLockerMetadataSources.catalog_item)
            {
                // Multiple keys requested: loop through to find the correct one
                foreach (var catalogItemMetadata in entry.entries)
                {
                    // Handle the image
                    if (catalogItemMetadata.type == LootLockerMetadataTypes.Base64)
                    {
                        // Look above in how LootLockerMetadataSources.currency is handled to see how to handle an image
                    }

                    // Handle a bool
                    if (catalogItemMetadata.type == LootLockerMetadataTypes.Bool)
                    {
                        bool featured = catalogItemMetadata.TryGetValueAsBool(out featured);
                        // Add your own code here to make us of the returned bool value
                    }
                }
            }
        }
    }
    else
    {
        // If it failed, output the error to the console
        Debug.Log(response.errorData.message);
    }
}, ignoreFiles);

Check out our SDK for more ways of handling the received metadata entries.

Conclusion

In this How-to we’ve fetched multiple metadata entries that were created in the web console by using tags. Metadata also provides the possibility to fetch a single metadata in game or fetch metadata in game with tags.

Last updated