Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Unity and Facebook SDK managing Score System

Discussion in 'Scripting' started by Gremory, May 14, 2014.

  1. Gremory

    Gremory

    Joined:
    May 23, 2013
    Posts:
    2
    Hello!

    I've been reading allot about this and there is very few information on this subject.

    I am having trouble managing unity and facebook sdk with the score system.

    So, I might be making some mistakes and I hope someone can help me through them.

    So, when I open my game, I can either already be connected, or need to press the conenct button.

    And this is the code I have so far.

    The problem here is that I get an error when trying to POST a score = to 0.

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4. using System.Collections.Generic;
    5. using Facebook.MiniJSON;
    6.  
    7. public class FacebookInit : MonoBehaviour {
    8.  
    9.     // Use this for initialization
    10.     void Start () {
    11.         FB.Init(OnInitComplete);
    12.     }
    13.  
    14.     private void OnInitComplete()
    15.     {
    16.         if (FB.IsLoggedIn)
    17.         {
    18.             FB.API("/"+FB.UserId+"/scores", Facebook.HttpMethod.GET, retrieveScore);
    19.             gameObject.SetActive(false);
    20.         }
    21.         else {
    22.             gameObject.GetComponent<UILabel>().text = "Connect With Facebook!";
    23.         }
    24.     }
    25.  
    26.     void retrieveScore(FBResult result)
    27.     {
    28.         Dictionary<string, object> playerScore = Json.Deserialize(result.Text) as Dictionary<string, object>;
    29.  
    30.         object score;
    31.         if (playerScore.TryGetValue("score", out score))
    32.         {
    33.             Debug.Log(score);
    34.         }
    35.         else
    36.         {
    37.             // Player has no score registered, so lets make it 0? Is this right?
    38.             var wwwForm = new WWWForm();
    39.             wwwForm.AddField("score", "0");
    40.             FB.API("/" + FB.UserId + "/scores", Facebook.HttpMethod.POST, Callback, wwwForm);
    41.         }
    42.     }
    43.  
    44.     void Callback(FBResult result)
    45.     {
    46.         Debug.Log(result.Error);
    47.     }
    48.  
    49.     public void CallFBLogin()
    50.     {
    51.         FB.Login("email,publish_actions", LoginCallback);
    52.     }
    53.  
    54.     void LoginCallback(FBResult result)
    55.     {
    56.         if (result.Error != null){
    57.  
    58.             Debug.Log("Error Response:\n" + result.Error);
    59.         }
    60.         else if (!FB.IsLoggedIn)
    61.         {
    62.             Debug.Log("Login cancelled by Player");
    63.         }
    64.         else
    65.         {
    66.             Debug.Log("Login was successful!");
    67.             FB.API("/" + FB.UserId + "/scores", Facebook.HttpMethod.GET, retrieveScore);
    68.             gameObject.SetActive(false);
    69.  
    70.         }
    71.     }
    72. }
    73.  
     
  2. trololo

    trololo

    Joined:
    Dec 13, 2012
    Posts:
    17
    Did you solve that?