Accessing Configs
In order to drive the game behavior based on the Config, you need to access its values via code. This tutorial will show you how to access Configs and use them in your game.
Requirements
- You completed the Creating Configs tutorial.
com.homagames.configurationpackage is installed.
Setup
In this tutorial we'll create a simple EnemySpawner script. We'll use values from previously created Config to determine the health of the spawned enemies.
Create the EnemySpawner class
Create a new C# script called EnemySpawner.cs with the following code:
using UnityEngine;
public class EnemySpawner : MonoBehaviour
{
public void SpawnEnemy()
{
const float enemyBaseHealth = 100f;
Debug.Log($"Enemy spawned with health: {enemyBaseHealth}");
}
}
Retrieving the Config Handle
Config objects are accessed via an object called Config Handle. To obtain a IConfigHandle<T> call Config.GetHandle<T>() using the type of Config you want to access as the type argument.
Accessing Config Handles requires Config service to be initialized. Use the Config.IsInitialized property to check whether initialization has completed, or subscribe to the Config.Initialized event to be notified when it does.
private IConfigHandle<EnemyBalanceConfig> _configHandle;
private void Awake()
{
if (Config.IsInitialized)
FetchConfigHandle();
else
Config.Initialized += FetchConfigHandle;
}
private void FetchConfigHandle()
{
_configHandle = Config.GetHandle<EnemyBalanceConfig>();
}
Accessing the Config
Once you have the IConfigHandle<T>, you can access the Config through its Config property.
public void SpawnEnemy()
{
const float enemyBaseHealth = 100f;
var enemyHealth = enemyBaseHealth * _configHandle.Config.HealthScalingFactor;
Debug.Log($"Enemy spawned with health: {enemyHealth}");
}
Notice that the Config instance is not cached. Instead, it's accessed through the IConfigHandle<T>.Config every time it's needed. The next step explains why that's important.
Updating the Config
When the remote configuration is updated, the config object can become outdated. To ensure that the config object is up-to-date, you can check if it's outdated and update it if necessary.
public void OnLevelStarted()
{
if (!_configHandle.IsUpToDate)
_configHandle.UpdateConfig();
}
In this example, the OnLevelStarted method ensures that the Config is up-to-date at the start of the level.
Calling UpdateConfig() will instantly replace the config reference of the Config property to one that represents the latest configuration. This way, you can be sure that the values you're using are the most recent ones.
Tip
Pay attention to when you update your Configs. Updating the Config mid-level can lead to unwanted inconsistencies.
Summary
In this tutorial, you learned how to access Configs in your game and how to ensure that they are up-to-date.
Here's the complete EnemySpawner script:
using Homa.Sdk.Configuration;
using UnityEngine;
public class EnemySpawner : MonoBehaviour
{
private IConfigHandle<EnemyBalanceConfig> _configHandle;
private void Awake()
{
if (Config.IsInitialized)
FetchConfigHandle();
else
Config.Initialized += FetchConfigHandle;
}
private void FetchConfigHandle()
{
_configHandle = Config.GetHandle<EnemyBalanceConfig>();
}
public void SpawnEnemy()
{
const float enemyBaseHealth = 100f;
var enemyHealth = enemyBaseHealth * _configHandle.Config.HealthScalingFactor;
Debug.Log($"Enemy spawned with health: {enemyHealth}");
}
public void OnLevelStarted()
{
if (!_configHandle.IsUpToDate)
_configHandle.UpdateConfig();
}
}