Data Storage Unity tutorial

Create a collection of information and get it working in your Unity Project in about 40min in five steps:

  1. Create the collection
  2. Store the initial information
  3. Check the information
  4. Retrieve the information
  5. Modify the information

Create the collection

Let’s start by creating the target collection.


1. Visit your Gamedonia Dashboard and log in.

2. Go to the Data Storage > Collections section.

3. Click on the Create Button.

4. Insert userstats as the Collection name and select the permissions R (read), W (write) under OWN.

5. Click on Save.


Collection creation successful!

Now you have a collection that will keep user information only available to be read & written by the owner. Way to go!

 

Store the initial information

Let’s add some information to the collection from a scene of your game.


1. Open your Unity game.

2. Locate the method OnFacebookLogin we created on the previous tutorial.

3. Create a dictionary with the information to store in the collection:

// Store all the information you want inside a dictionary
Dictionary<string,object> myStats = new Dictionary<string,object>();
myStats ["maxpoints"] = 0;
myStats ["lastlevel"] = 0;
myStats ["productpurchased"] = 0;
myStats ["username"] = "SuperNickName";

4. Send the information to Gamedonia with that code:

// Make the request to store the entity inside the desired collection
GamedoniaData.Create("userstats", myStats, delegate (bool cr_success, IDictionary data){
	if (cr_success){
		//TODO Your success processing
	}
	else{
		//TODO Your success processing
	}
});

5. The whole OnFacebookLogin should look like this:

void OnFacebookLogin (bool success) {
	if (success) {
		Debug.Log("Login worked!!");
		// Store all the information you want inside a dictionary
		Dictionary<string,object> myStats = new Dictionary<string,object>();
		myStats ["maxpoints"] = 0;
		myStats ["lastlevel"] = 0;
		myStats ["productpurchased"] = 0;
		myStats ["username"] = "SuperNickName";
		// Make the request to store the entity inside the desired collection
		GamedoniaData.Create("userstats", myStats, delegate (bool cr_success, IDictionary data){
			if (cr_success){
				//TODO Your success processing 
			}
			else{
				//TODO Your success processing
			}
		});
	}else {
		Debug.Log("Login failed");
	}
}

Stored initial information!

Now that you've stored the initial information you can move on to the next step. Keep going!

 

Check the information

Let's move back to the Dashboard Object Browser to see the information you stored there is correct.


1. Visit the Object Browser in the Dashboard.

2. Choose the userstats collection.

3. Click the Filter button.

4. You will see one entity/register in the list.

5. Click on the arrow to unfold the entity and see the associated information.


Checked the info is there!

What you created appears on the Object Browser. Let's move on to see what you can do with it.

 

Retrieve the information

To test how to get information from the server, let’s make a simple button that launches the request.


1. Create a button in the OnGUI() method that calls the search API when clicked.

2. Search all the information in the collection userstats.

if (GUI.Button (new Rect (80,205, 220, 50), "GetStats")) {
	GamedoniaData.Search("userstats", "{}", delegate (bool success, IList list){
		if (success){
			//TODO Your success processing
			if (list != null) {
				Dictionary<string, object> myStats = (Dictionary<string, object>) list[0];
			}
		}
		else {
			//TODO Your fail processing
		}
	});
}

3. Once the information has been collected we can show it as a log trace. Add this line after Dictionary<string>...”

		Debug.Log("username is=" + myStats["username"]);

Information correctly retrieved!

Good job! There's a couple of things you must know before going forward.

WARNING: Now you know how to search for an entity, you should really improve the code by adding a search before creating entities. Why? If you were to use this tutorial as it is, every “Facebook Login” would create a new userstats entity. You could end up with 10 entities for the same user. If you add the search, if the user already has an entity, it won’t create a new one.

 

We are retrieving all the information on userstats. Because the collection says that the entities are only readable by the owner (the one who stored them), I will only receive entities I can read. Because you only have one entity inside the collection you will get only 1 entity.

 

 

Modify info from the dashboard

Now that you are able to store and retrieve information, let’s make an interesting test!


1. Once again, go to the Object Browser and filter the userstats collection.

2. But now, check the entity and click on Update.

3. Modify the “username” value to “SuperNickname Changed By Admin”.

4. Click on the Floppy disk icon to save the changes.

5. Launch your game and click on the GetStats button you made in the previous step. The log trace should now print the new “username”.


Learnt to modify info!

Congrats! You are ready to save any information that's relevant for your game.

Having learnt to modify information in the database will come in handy when you get to the Server Code tutorial. You'll be able to launch events that modify the users' info, achievements, scores, and so on.

 

What's next?

In the next tutorial you'll be able to add server logic to your game using Server Code. Learn how server hooks and custom scripts can help you with your business logic, and more.

To access the Server Code tutorial right now, just click here.

This is the complete list of tutorials to help you build with Gamedonia Backend:

  1. Facebook login
  2. Data storage
  3. Server Code
  4. Push notifications
  5. In-app purchases
  6. Downloadable content