Tutorial presenting how to create a player profile system powered by Syncano.
If you are looking for an easy and fast way to set up a player profile system in your game, then this blog post will guide you step by step through the process.
The example below explains in detail how such a system works and how it can be adjusted so it suits your needs.
Player profiles in other games
Player profiles vary from game to game. For instance, Hearthstone keeps data for a player’s region, card list, friend list, money, quests, arena card list and much more. What is very important, is that players have access to all this information on different devices like PC, MAC, phones/tablets with Android and iOS. By having cross-platform player profiles, users experience the game without platform limits and it is one of the things that made Hearthstone a successful product. What is even more interesting, is that Hearthstone was made with Unity!
In other words, every game that is designed to be cross-platform must implement some way of sharing data between all devices. Let’s take a look at another example - Words with Friends from Zynga. Many functionalities like a friend list, game score, and winning streaks are available on every platform independently. Such features make games even more popular and easier to spread among other players.
Today, we game developers are accustomed to big game engines like Unity3D or Unreal and a cross-platform approach, in many cases, is a must in order to survive in a tough gaming market. But what about a platform independent system of sharing data? Well, there isn’t too much choice in variants. Unity indeed offers some solutions, but keep in mind it mostly covers matchmaking and synchronising object position, etc. When it comes to storing and sharing data between platforms, game devs must rely on themselves and it usually takes some time to design and implement a satisfying system. If you are curious about how this can be achieved effortlessly, I’m glad to introduce you to Syncano. Let’s get started!
Before you start
Before you start, you need to create a Syncano account. It is completely free, so you can join here and receive access to all of the cool features Syncano offers!
After setting up an account, go to the Dashboard and create a new Instance.
Instance name as you will need to provide it when connecting to Syncano through the Unity Library or when operating with Scripts.
The last thing you need to do is to download a Unity Library from the Releases Page. You can find more information about the library on GitHub. Don’t forget to import it into your Unity project. That’s all for setting up the environment. Let’s turn player profiles into reality!
Getting Started: Syncano Users
What you need first is a structure that holds a player (in Syncano, we’ll refer to players as users) and all his information in one place. Usually, data in Syncano is stored in Data Objects.
You can store data on Syncano using Data Objects. Data Objects are similar to JSON objects that contain key-value pairs defined by you during Data Class creation. All of them also have default attributes defined by Syncano.
Fortunately, Syncano has already done some of the work for us and implemented a handy system for managing users. When you browse through the Instances, you may notice there is an additional Data Class called
user_profile. This is a default Class that stores the Data Objects of your users. According to Syncano docs:
So, whenever a new user is added, a special "profile" Data Object is being created. The userprofile Data Class and its Data Objects profiles are protected, which means that no one can delete the userprofile Data Class, create new user profiles manually, delete profiles, or change ownership of the profile (the user is always the owner of their profile).
So whenever a user is created, its data will be stored in the
user_profile Class. In the example, we store things like a user’s avatar, region, score, etc. What you need to do is to extend the user Class and add some extra fields for keeping additional information.
- Go to the
Data Classespanel from the left sidebar
- Locate three dots on the right side of the
user_profileClass, edit it so the schema matches the screenshot below:
![Player Profile Schema](https://s15.postimg.org/izsa783qz/user_schema.jpg "Player Profile Schema")
Note: Be sure to click Add after creating each field!
Getting Started: About API KeysWhenever you use Syncano, you will be asked for two things - Instance Name and API Key. With these two variables, Syncano recognizes and authenticates anybody who is making an API call to the backend. Basically there are two types of API Keys. The first one is an `Account Key`, which gives you the possibility of acting as a Syncano administrator. With this key you can add, delete, modify Data Objects, Channels, and even Instances! Remember, you should never expose your Account Key and you should use it only when working in a safe environment. From Syncano docs:
The second key type is a default API Key with three different permissions. If you go to the API Keys section located at the left bottom corner of the Dashboard and press the `ADD` button, a panel with the API Key creator will pop up.
By using an Account Key you act as a Syncano Administrator. You can access almost every endpoint in the Syncano API (with user endpoint being the only exception). What this means is that it can be used not only to create users, Data Classes, Data Objects, etc., but also to change your billing or account information.
ignore_acl- API Key with this flag set to true will ignore permissions set for Instance resources. You can read more about ignore_acl keys in the Permissions chapter.
allow_user_create- API Key with this flag set to true can be used to create users (it is not possible with a standard API Key). You can read more about user creation in the User management chapter.
allow_anonymous_read- When this flag is set to true, an API Key can read Data Classes and Data Objects with other_permissions set to read or higher. You can read more about this key in the Permissions chapter.
Working ExampleYou may not believe it, but the hardest part was already done! Now, let’s take a step-by-step look at how to save, update, and retrieve a user profile from the Unity application.
- Download the Unity demo project from here.
- Open Unity and import the downloaded project.
- Open the UI.cs file located in the root directory.
- In the UI.cs file, find the Start method. There, you need to set your API Key and Instance name.
- Open the MainScene file located in the Assets/Scenes folder.
Once these steps are done, you may press the play button and run the current scene. From this point, you have two options - log in and create a new user account. Since you don’t have an account yet, go to Create Account and create one.
What actually happened is we stored three variables - nickname, password, and avatar - and sent them to Syncano via the Unity library like this:
user = new User<TestProfile>(TextUsername.text, TextPassword.text); /* FULL CODE CAN BE FOUND IN CreateNewProfile METHOD */ user.Profile = new TestProfile(randomVictories, randomDefeats, country, avatar, randomTrophies1, randomTrophies2, randomTrophies2); yield return user.Register(OnUserRegisterSuccess, OnUserRegisterFail);
If you go now to Data Classes in your Instance, you will see a new object has appeared in the user_profile Data Class.
The magic behind the scenes is pretty simple. If you open the TestProfile.cs file, you will see that all properties correspond to the ones in the user Class you created earlier. All you need to remember is this: when creating new user, you must also take care of its profile. The profile keeps and gives access to all custom data. So, if you want to store additional fields in Syncano, you also must update your custom Profile Class so the value can be read and modified.
With an account, you can sign in and check the details of your profile. The idea of retrieving user data is straightforward. When a user signs up, a call to Syncano is made. In case of success, your user object is returned with all its details. User login can be done with only a few lines of code.
user = new User<TestProfile>(TextUsernameLogin.text, TextUsernamePassword.text); return user.Login(OnUserSigninSuccess, OnUserSigninFail);
When it comes to modifying user data there is also an easy way to do it. Once you are logged in, you can modify every property of your user. For instance, in this example, if you want to change the existing profile picture you can click on it, select a new one and then press the Update button. The mechanics for updating a user profile is very simple:
user.Profile.Avatar = new SyncanoFile(AvatarImageDetails.sprite.texture.EncodeToPNG()); yield return user.UpdateCustomUser(OnUserUpdateSuccess, OnUserUpdateFail);
Working with Syncano User Profiles is simple. If you wish to keep more data about your users, all you need to do is to edit the default
user_profile Class and add extra fields. The second thing to do is to create a special API Key with
create user permissions. After that, using the Unity library is about creating a custom Profile Class with all fields corresponding to your Class in Syncano, and that’s it. The three main methods you will be using are creating, signing in, and updating your User Profile.
As you can see, managing a cross-platform Player Profile system can be easy. Syncano provides a great way of sharing data between all devices, including the ones made with Unity. In the case of users, you can extend the existing user_profile Class and add as many fields as you wish! Let us know what you think about Syncano User Profiles by tweeting us @Syncano.
Source code for this blog post can be found here.
Also feel free to ask any questions you have in the comments under this post, by joining our Slack community channel, or by writing to our support team at [email protected] Good luck!