To use Databox simply make sure to have a reference to a Databox object. It is important to load your data before accessing it. You can register a method to the OnDatabaseLoaded event, which will be called when your database has been loaded.
using Databox;
public class Example : MonoBehaviour
{
// The reference to your databox object
public DataboxObject data;
void OnEnable()
{
data.OnDatabaseLoaded += DataReady;
}
void OnDisable()
{
data.OnDatabaseLoaded -= DataReady;
}
void Start()
{
data.LoadDatabase();
}
void DataReady()
{
// Access data
}
}
using Databox;
public class Example : MonoBehaviour
{
// The reference to your databox object
public DataboxObject data;
// return data of type float
public void GetData()
{
FloatType health = data.GetData<FloatType>("TableName", "EntryName", "ValueName");
// modify value
health.Value = 10f;
Debug.Log(health.Value.ToString());
}
}
To add data by script simply call AddData.
using Databox;
public class Example : MonoBehaviour
{
// The reference to your databox object
public DataboxObject data;
public void AddData()
{
// create a new float value
FloatType health = new FloatType();
// add value
health.Value = 100f;
// Add health to our database
data.AddData("TableName", "EntryName", "ValueName", health);
}
}
Each data value has a "on value changed" event which gets called if a value has been changed.
using Databox;
public class Example : MonoBehaviour
{
public DataboxObject data;
FloatType health;
public void Start()
{
// get data
health = data.GetData<FloatType>("TableName", "EntryName", "ValueName");
// register event
health.OnValueChanged += OnHealthChanged;
}
void OnHealthChanged(DataboxType _data)
{
// instead of using a global variable you can also convert the data back
// var _health = _data as FloatType;
Debug.Log("Health has been changed " + health.Value.ToString());
}
}