Simple Database

Quickstart

Here we will take a quick look at how Simple Database works. With only the three steps you will be good to go:
using Skytanet.SimpleDatabase;
//opening / creating
SaveFile saveFile = new Savefile("My new save file");
//saving & loading
saveFile.Set("key", "value");
string savedValue = saveFile.Get<string>("key");
//closing
saveFile.Close();

Simple Database is much more powerful when combined with the free asset by Wanyzee Studio: Json.NET Converters - Simple compatible solution. If you downloaded the Json Converters assets and are having problems with dictionaries just delete the following script: WanzyeeStudio > Scripts > Runtime > Concrete > Json > DictionaryConverter

Opening save files

By default this asset is configured to work on the Application.persistentDataPath directory (unity docs). To open or create a new save file in that directory just create a new SaveFile instance passing the name as parameter.
SaveFile saveFile = new Savefile("slot1");
If there is a save file with that name already existing in that directory it will just get opened, if there is no file with that name then a new one will be created for you automatically. You cannot open two save files with the same name at the same time.

If you want your players to manage multiple save files you can get a list of their save file names by using the static method SaveFile.GetSaveFileList():
string[] saveFileList = SaveFile.GetSaveFileList();

Saving & loading

To save and load values use the set and get methods, for example:
SaveFile saveFile = new Savefile("slot1");
Vector3 vector = new Vector3(1, 2, 3);

saveFile.Set("key", vector);

When retrieving a key you can first check if a key is in use and alternatively you can pass a default value in case the key doesn't exist, just be sure to use the adequate data type when getting a value. If a default value is not specified and the key doesn't exist then default(T) will be returned.
//using a default value in case it doesn't exist
Vector3 firstValue = saveFile.Get<Vector3>("key", new Vector3(0,0,0));

//checking if it exists first
if (saveFile.HasKey("key")) {
    Vector3 secondValue = saveFile.Get<Vector3>("key")    
} else {
    //other stuff
}

This asset comes with the following built-in and tested supported types:
  • Vector2
  • Vector3
  • Vector4
  • Quaternion
  • Color
  • List
  • Dictionary (when the key is a string)
  • Vector2Int
  • Vector3Int
  • Bounds
  • Matrix4x4
  • Rect
  • RectOffset

Closing save files

Once you are done it is important to close the save files, otherwise the file streams will be kept open and you will be unable to open it again until it gets closed. Once closed you won't be able to access the save file until you open it back. If you wish to reuse the save file object you can re-open it by using the reOpen() function, although creating a new Savefile would be a valid alternative too:
//closing once done
saveFile.Close();
//reopening to reuse the "saveFile" object
saveFile.ReOpen();
Finally, if you want to permanently delete a save file you can use the static method DeleteSaveFile
DeleteSaveFile("slot1");
And that's all needed to know to start using this asset!

Sitemap

home
contact

Social




Copyright © 2016-2019 Eric Mourin