How to Make PvP Games in Unity Using Heroic's Shared Storage API

There are many mobile games on the app stores and desktop games on distribution platforms such as Steam -- with more launched every day. The very best of these games keep users entertained for weeks and months at a time. The hallmark of these games is how well they enable users to compete with friends and others around the world.

This tutorial will walk you through how you can use Heroic Labs’ Shared Storage APITrack this API with Unity to create games in which users can compete against each other. The game we’re using for inspiration is Boom Beach -- a mobile strategy game in which players build bases, attack other players’ bases, and defend their bases from attack. We’ll call our sample game Battle Town.

Our game will have a Medieval setting where players can build up armies consisting of 5 types of units: farmer, archer, knight, captain, and healer. Normally, we’d have many more unit types with different skills, damage output, etc. -- but let’s keep the code simple.

We’ll use C# and the Heroic Labs SDK to work with the Shared Storage API. To get started with Heroic Labs you can create an account for free in the Dashboard. When you’ve logged in, you then create a game in the service and grab an API key from the "Game Detail" view. This API key is used to authenticate the SDK with the API.

Now, we need to set up the game project in Unity. I won’t be able to cover all of the setup involved with Unity or how to build the UI or graphics for our battle game, but there are lots of tutorials you can use to cover other parts of the game development work. We’ll work heavily in Unity scripts -- which are attached to game objects -- and write our C# code.

The first thing we need to do is to initialize the SDK with our API key. This must be done as early as possible in the game -- ideally at game startup. (I’ll use an example key, but you will need to use a valid one from the Dashboard.):

Client.ApiKey = "215bff112839448cb4ccf716605c94b2";

You can test that the API key is valid and works with a quick "ping" request to the API:

Client.Ping ((status, reason) => {
  Debug.LogFormat ("Request failed with '{0}' and '{1}'.", status, reason);

The code above will only display a log message if you’re unable to connect to the API. If this happens, use the "reason" in the output to discover why and adjust your code as needed.

With the SDK configured and Unity set up, let’s return to our game design. Every player will need to be logged in to Battle Town to play. There are a number of ways we can solve this problem; some solutions involve Facebook or other social network integration -- but for this tutorial, we’ll keep it simple and use a unique device ID (UDID). For other options, have a look here.

UDIDs can be unreliable identification sources within mobile games because they can change after a device update. We’ll place a copy of the UDID in device storage so that it can be referenced by the game in the future if the user needs to re-authenticate.

Even with this solution, it’s possible that we won’t be able to recover a user’s account if the device storage is cleared and a device update occurs; consider one of the other account creation options in the Heroic Labs API:

string uid = PlayerPrefs.GetString("UID");
if (string.IsNullOrEmpty (uid)) {
  uid = SystemInfo.deviceUniqueIdentifier;
  PlayerPrefs.SetString ("UID", uid);

Client.LoginAnonymous (uid, (SessionClient sessionClient) => {
  // we have a SessionClient to make requests with
  // cache this for later use in the SceneGraph
}, (status, reason) => {
  Debug.LogErrorFormat ("Could not login user with ID: '{0}'.", uid);

The authentication of the user returns a "SessionClient" in the SDK which handles all API operations that require an active user. We need to have a player logged in so that we can share their army with other players for battles. At this point, let’s go back to our Battle Town game and outline how the gameplay mechanics will work.

Chris Molozian A software engineer and tinkerer. Co-Founder of Heroic Labs.