How-to: Sample SDK Javascript

Xtralife wants to make sure that everyone can use our tools easily.

Today, we’re introducing this example project to show you how to use the SDK for Javascript. We hope it will guide you to make your game social and start a player community.

You’ll learn how to use the following features of Xtralife:

  • Login (anonymous or with Facebook),
  • Virtual File System to handle player data persistence
  • Leaderboard

Get the SDK and sample project

First of all, clone the Xtralife Javascript SDK:
git clone https://github.com/xtralifecloud/javascript-sdk.git

If you have node.js installed, you can run the sample easily

cd javascript-sdk  
npm install  
npm start  

Now open (it won’t work yet) http://localhost:8080/

Dive into the code

Open an account at https://account.clanofthecloud.com, and create your first game (call it sample for instance), to get an API key and API secret. You’ll need them in the next step.

Now copy/paste the Key and Secret at the very beginning of ./sample/js/sample.jsinto the corresponding variables. This tells your app how to authenticate your game to the backend.

You’ll notice a few things:

  • ./sample/js/bundle.min.js contains the javascript SDK with all its dependencies, minified. This file is a copy of ./lib/bundle.min.js (you’ll need your own copy in your own game). You can use ./lib/bundle.js if you need more debugging information.
  • the bundle.min.js script is included on every web page of the sample (except index.html which does use the backend). The script exposes a global window.Clan object.
  • the sample includes a Login with Facebook feature which only works if you have a web server running on the “localhost” domain
  • LocalStorage is used to store authentication information (see ./sample/js/storagemanager.js) the ./sample/js/sample-*.js files show how to use each feature

This sample is minimalist in terms of toolchain and structure, and doesn’t reflect any good practices of game development for the sake of simplicity.

Start your own game

From the sample, you’ve learnt a few things that’ll be required to start your own game :

  • you need a copy of ./lib/bundle[.min].js
  • we add no dependencies except this file
  • the SDK is exposed as window.global
  • player authentication/identification must be persisted (in LocalStorage for example)
  • our JS SDK is as powerful as our C# SDK, and often simpler
  • the SDK is open source. It’s written in Coffeescript but it should read very easily. It’s almost a one-to-one mapping to our REST APIs.

Now get back to your code!

Waiting for your feedback on Twitter : @XtraLifeCloud