*

CC Tutorial2 – Making API calls

In this tutorial we’ll explain how to make brainCloud API calls from within Cloud Code scripts.

Example Background

For this example, we’ll imagine that the developer wants to write to two leaderboards upon completion of a game round.

She has decided to combine these two operations into a single cloud code script to reap the following benefits:

  • More responsive app – fewer client/server calls means a more responsive app
  • Cost savings – it costs 1 API count to call a script, but the first 3 api calls called from within a script are free (and the rest cost only 1/2 count each). The result is she can write to two leaderboards for a single api count combined, half the cost otherwise.

Note that for this example we’ll use the dynamic leaderboard calls so that we don’t have to set up the leaderboard metadata ahead-of-time.

 

Step 1 – Write the Script

We’ll enter the script following the same steps as the last tutorial.

First of all, create the script by going to Design | Cloud Code | Script, hitting the [+], setting Script Name to “Tut2_WriteToLeaderboards”, ensuring Client Callable is TRUE, and entering the following for the Test Parameters:

{
  "score": 100,
  "kills": 10
}

Then switch to the Editor tab and copy-and-paste the following for the script:

Script   
  1. // Retrieve the parameters
  2. var score = data.score;
  3. var kills = data.kills;
  4. var leaderboardsWritten = 0;
  5. var results = {};
  6.  
  7. // Grab the leaderboard service proxy from the bridge
  8. var leaderboardService = bridge.getLeaderboardServiceProxy();
  9.  
  10. // Prepare the leaderboard paramaters. These are only used when creating the
  11. // leaderboard (which happens the first time a score is posted to it)
  12. var leaderboardType = "HIGH_VALUE";
  13. var rotationType = "NEVER";
  14. var rotationReset = new Date("2016-01-01");  // Unused because rotationType is NEVER
  15. var retainedCount = 1; // Just keep the current leaderboard
  16.  
  17. // Post to the score leaderboard
  18. var scoreResult = leaderboardService.postScoreToDynamicLeaderboard(
  19.     "scoreLeaderboard",
  20.     score,
  21.     null, // Not appending any extra data
  22.     leaderboardType,
  23.     rotationType,
  24.     rotationReset,
  25.     retainedCount);
  26.    
  27. if (scoreResult.status == "200") {
  28.    
  29.     leaderboardsWritten++;
  30.    
  31.     // Post to the kill leaderboard
  32.     var killsResult = leaderboardService.postScoreToDynamicLeaderboard(
  33.         "killsLeaderboard",
  34.         kills,
  35.         null, // Not appending any extra data
  36.         leaderboardType,
  37.         rotationType,
  38.         rotationReset,
  39.         retainedCount);
  40.        
  41.     if (killsResult.status == "200") {
  42.         leaderboardsWritten++;
  43.     }
  44.    
  45. }
  46.  
  47. results["leaderboardsWritten"] = leaderboardsWritten;
  48. results;

The important things to note from this example:

  • Before you can make an API call, you must request the appropriate service proxy from the bridge.  Note that there are separate bridges for Client API and S2S API calls – consult the appropriate one for the available proxies.
  • You then invoke the API call upon the service proxy itself.

Step 2 – Run/Test the Script

Switch to the API Explorer and run the script. Remember to authenticate first!

You should see results that look similar to:

Results   
{
 "status": 200,
 "data": {
  "response": {
   "leaderboardsWritten": 2
  },
  "success": true
 }
}

For fun, check out the leaderboards from our Monitoring section. Go to Monitoring | Global Monitoring | Leaderboards By default you’ll see the “killsLeaderboard” – but you can switch to the “scoreLeaderboard” via the combo box.

 


Step 3 – Call the Script from your App

The following code would call the script from Unity:

// Call the script
results = BrainCloudWrapper.GetBC().BrainCloudScript.RunScript(
  "Tut2_WriteToLeaderboards",
  "{ \"score\": 100, \"kills\": 10 }",
  OnSuccess_UpdateLeaderboards,
  OnError_UpdateLeaderboards,
  null );