Custom Location¶
By default, Game Creator saves games using the Player Prefs built-in system. However, although this solution is cross-platform and will work for most users, some might prefer to sync their saves with an online database or use a different system than Unity's PlayerPrefs.
Here we will explore how easy it is to extend the save location.
TDataStorage class¶
To create a custom save location, one must create a class that inherits from the abstract class TDataStorage
, which contains all the necessary methods to store game information.
Note
Notice that in the following example(s) aren't any error handling mechanism for sake of simplicity. A production-ready product should also check and inform of the necessary errors that may occur.
Let's create our storage location class called StorageMyDB.cs
:
Convention
When creating a new storage database, we precede the name of the class with the Storage
word. So if your class is called MyDB
the convention would be to call it StorageMyDB
.
[Title("My Online Database")]
[Category("My Online Database")]
[Image(typeof(IconDiskSolid), ColorTheme.Type.TextLight)]
[Description("Stores the data in a custom database location")]
[Serializable]
public class MyOnlineDatabase: TDataStorage
{
public async override Task DeleteAll()
{
// ...
}
public async override Task DeleteKey(string key)
{
// ...
}
public async override Task<bool> HasKey(string key)
{
// ...
}
public async override Task<object> Get(string key, Type type)
{
// ...
}
public async override Task Set(string key, object value)
{
// ...
}
public async override Task Commit()
{
// ...
}
}
The Title
and Description
attributes allow to define the name and give a brief description of what this storage does, so it can be easily identified when choosing it from the Setting's dropdown menu.
The Image
attribute determines the visual icon from the dropdown menu. We recommend using the disk icon and only change the color.
The Category
attribute determines the location in the dropdown menu.
The TDatabaseStorage
class has a collection of abstract methods that need to be overridden and implemented in order to use the custom storage.
DeleteAll
: Is used to erase all information stored.DeleteKey
: Is used when deleting a single entry in the database.HasKey
: Returnstrue
orfalse
depending on whether the database has a field with that key name.Get
: Returns the contents of the database field with the specified key name.Set
: Sets the new value on the database with the specified key name.Commit
: Only used when setting multiple values on a database is very slow. The commit is always called after a batch of data is send to be stored.
Using async/await
It is important to note though that all methods have the async
prefix and either return a Task
object or a Task
associated with an object.
This is because there's a certain amount of time elapsed between the http request and the answer from the server. Being able to await requests let's you tailor how to safely chain commands and make sure each request is successfully fulfilled.