Config Caching

Remote Config & Game Overrides utilize Config assignment hashes to cache player config. Economy services cache the Config assignment hash for many hours to avoid unnecessary network calls.

Config assignment hash

What does the Config assignment hash represent?

The ConfigAssignmentHash is a unique identifier for a snapshot of a player's data in Remote Config, Game Overrides and Economy project config.

For Economy specifically, the snapshot is only for the Economy project config (Item definitions, currency definitions, etc) and not for the player's balances or inventory.

Config caching with Cloud code and Economy SDK

Please read Game Overrides - Cloud code and Game Overrides - Economy for more information on how to use Config assignment hash caching with the Economy Cloud Code SDK and C# SDK.

Accessing the configuration assignment hash

At some point, you may want to retrieve the player's current configuration assignment hash to use it with other Unity services. You can get the hash from the Configuration namespace.

GetConfigAssignmentHash

Retrieves the players current configuration assignment hash as a string. Returns null if none present.

C#

EconomyService.Instance.Configuration.GetConfigAssignmentHash();

SyncConfigurationAsync

Fetches the latest config for the authenticated player and saves the Config Assignment Hash locally for future Economy API calls. It's a good practice to call this method before making other Economy calls to ensure the right Config Assignment Hash is used.

C#

EconomyService.Instance.Configuration.SyncConfigurationAsync();

Pitfalls

Economy changes not being reflected due to a cached Config Assignment Hash

When you make changes to the Economy project config or Game Overrides, both Cloud code executions and Economy SDK calls will continue using their latest cached Config Assignment Hash.

You can circumvent this issue by making sure that you fetch the latest Config Assignment Hash before making any API calls.

For Cloud Code, you might want to pass the latest Config Assignment Hash as a parameter to your script and to your API calls inside the script.

You can also fetch the latest Config Assignment Hash at the start of your Cloud code script and pass it to your API calls inside the script.

Cloud code JS Example

const { CurrenciesApi, ConfigurationApi } = require("@unity-services/economy-2.4");

module.exports = async ({ params, context, logger }) => {
  const currenciesApi = new CurrenciesApi({ accessToken: context.accessToken });
  const configApi = new ConfigurationApi({ accessToken: context.accessToken });
  const currencyId = "COIN";

  try {
    // Retrieve the player config to get the configAssignmentHash.
    // This is needed to ensure the currency is synced
    var config = await configApi.getPlayerConfiguration({
      playerId: context.playerId,
      projectId: context.projectId,
    });

    const result = await currenciesApi.incrementPlayerCurrencyBalance({
      currencyId,
      playerId: context.playerId,
      projectId: context.projectId,
      configAssignmentHash: config.data.metadata.configAssignmentHash,
      currencyModifyBalanceRequest: { amount: 10 },
    });

    return result.data;
  } catch (err) {
    logger.error("Failed to increment currency for player", { "error.message": err.message });
    return;
  }
};

Exceptions

You may run into an EconomyException that tells you the configuration assignment hash was not found.

This exception has a Reason of EconomyExceptionReason.ConfigAssignmentHashInvalid.

To resolve this, fetch your Economy configuration again using one of the Configuration methods, for example, GetCurrenciesAsync(). This causes the SDK to get a refreshed configuration assignment hash, and you are then able to make calls as normal.