Databases

This week I’ve been making the databases for my game. The data storage objects that make my game work. A lot of the features of these classes are similar and I was wondering what was the best way to design these. I’ll explain how I do it at the moment and talk about why.

My current solution involves inheriting a base database class. This class handles all of the common behaviour, such as loading and saving the data from the server, but the data objects are filled out in the child class. I find myself having to write similar functions for searching and ordering the data objects. I feel like there may be a better way to handle the data objects.

Here is the base class I use:


using UnityEngine;
using System.Xml;
using AdvancedUtility;

public class Database : Object {

    public bool gotData;
    protected delegate void OnGotDataCB();
    protected OnGotDataCB OnGotData; 

    protected string scriptLocation;
    protected int loaderOptions;
    private AdvancedLoader loader;

    public Database() {
        gotData = false;
    }

    public void LoadData() {
        loader = new AdvancedLoader(loaderOptions);

        loader.SetSuccessCB(WebReturnedSomething);
        loader.SetErrorCB(WebError);

        loader.Load(scriptLocation);
    }

    protected void SaveLocalToCache(XmlNode preLoaded) {
        loader.SavePrelodedData(scriptLocation, preLoaded);
    }

    protected void SaveLocalToCache(string preLoaded) {
        loader.SavePrelodedData(scriptLocation, preLoaded);
    }

    protected void SaveLocalToCache(byte[] preLoaded) {
        loader.SavePrelodedData(scriptLocation, preLoaded);
    }

    private void WebReturnedSomething(XmlNode data) {
        ProcessData(data);
        gotData = true;

        if (OnGotData != null)
            OnGotData();
    }

    protected virtual void ProcessData(XmlNode data) {

    }

    protected virtual void WebError(NetError error) {

    }
}

The Object class I inherit handles object destruction and memory handling. The following variables are either expected by my database manager or there to communicate the child class to the loader object. The LoadData method sends constructs the loader to fetch the data from where the loaderOptions direct it. I also have the functionality to override the cache with any local changes that I have. The ProcessData and WebError let the child know the outcome of the loader call.

If you’ve any suggestions let me know.

Published by

Dom

Game Developer.

Leave a Reply