Monday, July 21, 2014

Manage save games in Unity without pain using NDatabase




As developers, we all know that save games are an important aspect to most of our games, even more so on mobile where the game can be disrupted at any time.
But how could we solve this issue? How can we store our game data in a performant and easy way without the major limitations that Unitys PlayerPrefs put on our titles?

Today, I want to introduce you to NDatabase!
NDatabase is an easy to use data storage solution.
NDatabase is written purely in C# without native code (like SQLite), making it capable of going to all Unitys platforms that can handle file operations.
You can DOWNLOAD our ready to use Unity project containing all sources to a simple game using these classes for you to follow this blog with full sources to experiment right away.
The game is fully commented and contains additional game elements.

Prerequirements

Before we get started, we need to ensure that we have everything ready.
For simplicity we will use the precompiled library.
You can download the ‘Download for .net 3.5′ version from https://ndatabase.codeplex.com.
After you downloaded it, extract the files content and move it into your projects Assets/Plugins folder.
Thats already all you need to start saving and loading your game data.

Define what you want to store

For this introduction, we will store data for an endless runner:
  • the current player position
  • the current score
  • the remaining lives
  • the inventory (a list of names)
We will store this information in a simple, lightweight data object that we can easily pass around as required.
We will not attempt to save a whole MonoBehavior or GameObject.
MonoBehavior and GameObject are very heavy weight objects.
Attempting to save them severly limits the complexity of the data you can store performantly.

The classes

NDatabase makes it very easy to store data.
All it requires is classes with data to store and it will handle it invisibly.
This is for example the case for object, float, int, string, bool or arrays, lists and dictionaries of these.
In our case, we want to store an array of floats for the position, two ints for score and lives as well as a list of strings for the inventory.
If we put this all together, this results in the following class:
PlayerData.cs
public class PlayerData {
    public float[] PositionData;
    public int Score;
    public int Lives;
    public List Inventory = new List ();


    /// 
    /// Gets or sets the position array
    /// 
    /// The position.
    public Vector3 Position {
        get {
            if (PositionData == null) {
                return Vector3.zero;
            }
            Vector3 result;
            for (int i = 0; i < 3; i++) {
                result [i] = PositionData [i];
            }
            return result;
        }
        set{
            PositionData = new float[3]{ value.x, value.y, value.z };
        }
    }
}
To focus on the specific NDatabase operations in the remaining post, I also want to provide the sources of the Player class you can find in the download. The Player class manages the PlayerData including the loading and saving of the data.
Player.cs
public class Player : MonoBehaviour
{
    private string _savegamePath;
    private IOdb _dataStorage;
    public PlayerData Data { get; private set; }

    private void Awake ()
    {
        _savegamePath = Application.persistentDataPath + "/savegame";
        LoadFirstSavegame ();
    }

    private void LoadFirstSavegame ()
    {
        _dataStorage = OdbFactory.Open (_savegamePath);

        Data = _dataStorage.QueryAndExecute ().GetFirst ();
        if (Data == null) {
            Data = new PlayerData ();
        } else {
            transform.position = Data.Position;
        }
    }

    private void OnApplicationPause (bool pause)
    {
        if (pause) {
            SavePlayerData ();
        } else {
            if (_dataStorage == null || _dataStorage.IsClosed ()) {
                LoadFirstSavegame ();
            }
        }
    }

    private void OnApplicationQuit ()
    {
        if (_dataStorage != null) {
            SavePlayerData ();
            _dataStorage.Dispose ();
        }
    }

    private void SavePlayerData ()
    {
        Data.Position = transform.position;
        _dataStorage.Store (Data);
        _dataStorage.Commit ();
    }
}

Creating the data storage

NDatabase makes creating the data storage as simple as it can be. All you need is a single line of code:
IOdb _dataStorage = OdbFactory.Open (savegamePath);
If you keep this reference stored on your player or a global manager, you can access the data at any time.

Saving your game the easy way

To prepare a new object to be stored, all we need to do is call
_dataStorage.Store (theObject);
This though will not yet write the data to disk. We need to finalize the changes to have them written to disk calling
_dataStorage.Commit();
In our case, we would like to update the existing save game instead of creating more and more entries in the database.
NDatabase makes this very easy, you simply need to store a previously fetched object.
As we assigned the latest player data to Data in the call to LoadFirstSavegame, all we need to do is update above Store call to
_dataStorage.Store (Data);

Loading the game made simple

Loading your save game is straight forward.
To get all stored PlayerData object, the simplest way is to call
var result = _dataStorage.QueryAndExecute ();
For the start, we will focus on getting the first PlayerData object, assuming there will only be one at any time.
This is done through the call to GetFirst:
Data = result.GetFirst();
Using Data, you can now restore the players position, score, lives and inventory by reassigning them to the appropriate objects.

Lookout

With the knowledge we now have we can already handle complex save game data sets through the ease of NDatabase. But we are still limited to the amount of data we can store in realtime as writing a lot of data takes time.
In the next article of this series, we will present you with different strategies to challenge the current limits in realtime save game sizes.
We will continue our coverage on NDatabase in future blog posts, covering NDatabase more indepth, showing you how to optimize your queries beyond the simple call, use indices to accelerate accesses even further and leveraging the In Memory database capabilities of NDatabase to manage large, complex data sets during runtime.
If you currently have a challenge with your save game handling that you want us to cover, let us know through the comments!

Tuesday, May 21, 2013

Unity Mobile (Android, iOS) free now

In a bold move Unity Technologies today updated their store and made Unity mobile platforms free again. They already made it free during a limited time in winter 2011 but it seems like the basic licenses will remain permanentely free this time.
I'm sure this is a welcome move for many and I'm looking forward to see many great new games.
Its hard to say why this decision was made but I hope that it is an indicator for future addon platforms, namely Windows Phone8 and BlackBerry 10, where such a 'support of the platform' would be very welcome for the platforms as whole. If I had to guess though I would assume that especially UDK, Cry and Havocs new free engine had a major impact as does Corona.

Repost of http://www.gamedevuniverse.com/blog/2013/05/unity-mobile-android-ios-free.html

Tuesday, March 6, 2012

Get Unity iOS Basic & Android Basic for free!

I'm normally nobody who writes about special discounts and things like these but in this case its just to big to not write about it: As I just learned, Unity Technologies is offering the basic iOS and Android licenses for FREE until April 8th on top of the free Unity license on their store: https://store.unity3d.com/index.html

This means that you can now get Windows Game Development, OSX Game Development, Web Game Development, iOS Game Development and Android Game Development through the Unity 3D Game Engine for free! :)

If you don't own the iOS and Android addons yet or wanted to see how your Windows, OSX or Web game would run on iOS, you definitely should give this a go!

Repost of  http://www.gamedevuniverse.com/blog/2012/03/get-unity-ios-basic-android-basic-for-free.html