Just looking for a second pair of eyes on my “database” UPDATE. I’m currently using a local database for my game for more complex entities, to see if there’s a better way or if I may be missing something.
- I load / create all my entities (anything that exists as a “thing” in my game) from their system:
EntitySystem<T> - I flag a entity as dirty if any of the property data changes
- In The Entity System I iterate over the collection of entities and check isDirty
(what I’m looking for suggestions with the below code) - I spin up a bunch of tasks to call my Update function on the entitys with the flag
Tasks are almost perfect in this case, I can run them and forget about them. All of the data is just fired off and saved and I don’t need to worry about it. There’s also no real instance where I’m worried about storing data immediately, if there’s any important things I need to store I would call the update manually.
I
//EntitySystem<T>
public void UpdateData()
{
tasks.Clear();
for (int i = 0; i < Datas.Items.Count; i++)
{
if (Datas.Items[i].isDirty)
{
var task = Task.Run(() =>
{
databaseSystem.UpdateData<T>(Datas.Items[i].Id, Datas.Items[i]);
if (Datas.Items[i].isDirty)
{
Datas.Items[i].isDirty = false;
}
});
tasks.Add(task);
}
}
Task.WhenAll(tasks).Wait();
}
//DatabaseSystem
public void UpdateData<T>(long id, T data) where T : class
{
if (data == null)
{
Debug.LogError("UpdateData failed: data is null.");
return;
}
try
{
bool success = auto.Update(GetTableName<T>(), id, data);
if (!success)
Debug.LogError($"UpdateData failed for ID {id}.");
}
catch (Exception ex)
{
Debug.LogError($"UpdateData exception for ID {id}: {ex.Message}");
}
}
//GameSystem (that constantly polls the systems)
bool storeData = true;
private void Update()
{
if(storeData){
foo.UpdateData();
bar.UpdateData();
}
}