Facebook - How to get a list of friends

1

Index

Stats

9,666 visits, 18,503 views

Tools

Translations

This tutorial hasn't been translated.

License

This tutorial is licensed under CC BY 4.0. Please refer to the license text if you wish to reuse, share or remix the content contained within this tutorial.

Published on 23 Jan, 2013. Last updated 19 Feb, 2019

Contents of Runtime.js

Runtime.js contains all the user-defined Javascript code you wish your new Facebook plugin to perform. If you know Javascript, then take a good look through runtime.js, since it contains all the important functions for the plugin.

I shall be starting the tutorial proper from here to build on the baseline Facebook plugin.

How to request for "Friends Information"

Open up runtime.js and search for "pluginProto.acts = new Acts();". Copy the section of code below and paste it before the line you found.

We shall use RequestUserFriendsInfo to retrieve a list of friends belonging to the player. I used a call to FB.api on line 6 to GET the entire list of friends belonging to the player. A callback function immediately follows as indicated by "function(response)" on the same line. This function will handle all replies from Facebook. If there are no errors, line 12 will check to see how many friends the player has by getting the length of the response (response.data.length). All friends are then transferred and stored in two internal Javascript arrays - fbFriendsName on line 15 and fbFriendsID on line 16.

If the player has (say) 500 friends, it would take a long time to get the entire reply from Facebook. To handle this, we set up a trigger Condition (OnFriendsInfoReceived) to tell Construct 2 that the entire list of friends has been retrieved on line 18.

    Acts.prototype.RequestUserFriendsInfo = function () {
       var responseLength = 0;
       var tempCount = 0;
       if (this.runtime.isDomFree)
          return;
       FB.api('/me/friends', 'GET', {}, function(response) {
          if (!response.data)
    	  {
    	     console.error("Friends request failed: " + response);
    	     return;
    	  }
    	  responseLength = response.data.length;
    	  for (tempCount = 0 ; tempCount < responseLength; tempCount++)
    	  {
    	     fbFriendsName.push(response.data[tempCount].name);
    	     fbFriendsID.push(response.data[tempCount].id);
          }
          fbRuntime.trigger(cr.plugins_.FacebookGW.prototype.cnds.OnFriendsInfoReceived, fbInst);
          if (!response || response.error) {
             console.error(response);
          } else {
             log(response);
          }
       });
    };

Because of lines 15 and 16, we must declare these two new variables in your code. Search for the following line "function onFBLogin()" in runtime.js and insert the following declarations in the code before the function.

    var fbFriendsName = [];
    var fbFriendsID = [];

Because of line 18, we must search for the following line "pluginProto.cnds = new Cnds();" in runtime.js and insert the following OnFriendsInfoReceived function before the line you found.

    Cnds.prototype.OnFriendsInfoReceived = function ()
    {
       return true;
    };

The arrays are useless if you have no way of retrieving the data stored in them. Search for the following line "pluginProto.exps = new Exps();" and insert the following three expressions. You can use FriendName and FriendID expressions to retrieve both User ID and Name for a specific friend in the array. To determine the size of the array, use the expression NumberofFriends.

    Exps.prototype.FriendName = function (ret, friend_index)
    {
       var friendname = "NO FRIEND IN THAT INDEX";
       if (friend_index < fbFriendsName.length) {
          friendname = fbFriendsName[friend_index];
       }
       ret.set_string(friendname);
    };
    Exps.prototype.FriendID = function (ret, friend_index) {
       var friendid = "NO FRIEND IN THAT INDEX";
       if (friend_index < fbFriendsID.length) {
          friendid = fbFriendsID[friend_index];
       }
       // Float because these numbers are insanely huge now!
       ret.set_float(friendid);
    };
    Exps.prototype.NumberOfFriends = function (ret) {
       ret.set_int(fbFriendsName.length);
    };
	

Ok, now save your modified runtime.js and open up edittime.js. Because we created a new Action called RequestUserFriendsInfo, we must define it here. Go to the Actions section and add in a new entry like the one shown below (if you have a different plugin, change the number 12 to the next largest number in the list, the rest of the code remains the same).

    AddAction(12, 0, "Request friends info", "Friends info (no permissions required)", "Request friends info", "Request Request friends info, triggering 'on friends info available' when the info is received.", "RequestUserFriendsInfo");

Also, we created a new trigger called OnFriendsInfoReceived which must be defined. Go to the Conditions section and add in a new entry like the one shown below.

    AddCondition(9, cf_trigger, "On Friends Info available", "On Friends Info available", "On Friends Info available", "Triggered after the 'RequestUserFriendsInfo' action.", "OnFriendsInfoReceived");

Finally, we had three expressions to retrieve data stored in the arrays. Define them in the Expressions section with the entries below.

    AddNumberParam("Friend Slot", "The slot in the friends info array where this friend is.");
    AddExpression(14, ef_return_string, "Get Friend Name", "Facebook", "FriendName", "Get the Friend's Name in slot {0}.");
    AddNumberParam("Friend Slot", "The slot in the friends info array where this friend is.");
    AddExpression(15, ef_return_number, "Get Friend ID", "Facebook", "FriendID", "Get the Friend's ID in slot {0}.");
    AddExpression(16, ef_return_number, "Get Number of Friends", "Facebook", "NumberOfFriends", "Get the Number of friends retrieved.");


  • 0 Comments

Want to leave a comment? Login or Register an account!