so im having an error Assets/FacebookSDK/SDK/Scripts/LoginManager.cs(25,144): error CS0136: A local variable named obj' cannot be declared in this scope because it would give a different meaning to obj’, which is already used in a `parent or current’ scope to denote something else
i really dont know how to fix this error because some namespace are change because of the updates, that’s why i can’t follow the scripts in the video properly…
Hi cyryllgalon,
I’m part of the customer support team at www.gamesparks.com . A Facebook SDK update caused some changes to the login process. Specifically when integrating Facebook into GameSparks. Our tutorials are in the process of being updated but I can pass you an updated version of the class you are using to handle your GameSparks and Facebook Login.
using UnityEngine;
using System.Collections;
using GameSparks.Core;
using System.Collections.Generic;
using System;
using GameSparks.Api.Responses;
using Facebook.Unity;
public class GameSparksManager : MonoBehaviour {
//singleton for the gamesparks manager so it can be called from anywhere
private static GameSparksManager instance = null;
//getter property for private backing field instance
public static GameSparksManager Instance() { return instance; }
// Use this for initialization
void Awake ()
{
//this will create a singleton for our gamesparks manager object
if(instance = null)
{
instance = this;
DontDestroyOnLoad(this.gameObject);
}
else
{
DontDestroyOnLoad(this.gameObject);
}
GS.GameSparksAvailable += GSAvailable;
GameSparks.Api.Messages.AchievementEarnedMessage.Listener += AchievementEarnedListener;
}
void GSAvailable (bool _isAvalable)
{
//this method will be called only when the GS service is available or unavailable
if(_isAvalable)
{
// Application.LoadLevel(1);
Debug.Log(">>>>>>>>>GS Conected<<<<<<<<");
}
else
{
Debug.Log(">>>>>>>>>GS Disconnected<<<<<<<<");
}
}
//Achievement message listener
private void AchievementEarnedListener (GameSparks.Api.Messages.AchievementEarnedMessage _message)
{
Debug.LogWarning("Message Recieved" + _message.AchievementName);
}
#region FaceBook Authentication
/// <summary>
/// Below we will login with facebook.
/// When FB is ready we will call the method that allows GS to connect to GameSparks
/// </summary>
public void ConnectWithFacebook()
{
if(!FB.IsInitialized)
{
Debug.Log("Initializing Facebook");
FB.Init(FacebookLogin);
}
else
{
FacebookLogin();
}
}
/// <summary>
/// When Facebook is ready , this will connect the pleyer to Facebook
/// After the Player is authenticated it will call the GS connect
/// </summary>
void FacebookLogin()
{
if(!FB.IsLoggedIn)
{
Debug.Log("Logging into Facebook");
FB.LogInWithReadPermissions(
new List<string>() { "public_profile", "email", "user_friends" },
GameSparksFBConnect
);
}
}
void GameSparksFBConnect(ILoginResult result)
{
if(FB.IsLoggedIn)
{
Debug.Log("Logging into gamesparks with facebook details");
GSFacebookLogin(AfterFBLogin);
}
else
{
Debug.Log("Something wrong with FB");
}
}
//this is the callback that happens when gamesparks has been connected with FB
private void AfterFBLogin(GameSparks.Api.Responses.AuthenticationResponse _resp)
{
Debug.Log(_resp.DisplayName );
}
//delegate for asynchronous callbacks
public delegate void FacebookLoginCallback(AuthenticationResponse _resp);
//This method will connect GS with FB
public void GSFacebookLogin(FacebookLoginCallback _fbLoginCallback )
{
Debug.Log("");
new GameSparks.Api.Requests.FacebookConnectRequest()
.SetAccessToken(AccessToken.CurrentAccessToken.TokenString)
.Send((response) => {
if(!response.HasErrors)
{
Debug.Log("Logged into gamesparks with facebook");
_fbLoginCallback(response);
}
else
{
Debug.Log("Error Logging into facebook");
}
});
}
#endregion
/// <summary>
/// If a player is registered this will log them in with GameSparks.
/// </summary>
public void LoginPlayer(string _userNameInput, string _passwordInput)
{
new GameSparks.Api.Requests.AuthenticationRequest()
.SetUserName(_userNameInput)
.SetPassword(_passwordInput)
.Send((response) => {
if (!response.HasErrors)
{
Debug.Log("Player Authenticated...");
}
else
{
Debug.Log("Error Authenticating Player\n" + response.Errors.JSON.ToString());
}
});
}
/// <summary>
/// this will register a new player and assign their email to their account.
/// </summary>
public void RegisterNewPlayer(string _userNameInput, string _emailInput, string _passwordInput)
{
new GameSparks.Api.Requests.RegistrationRequest()
.SetDisplayName(_userNameInput)
.SetUserName(_userNameInput)
.SetPassword(_passwordInput)
.SetScriptData(new GSRequestData().AddString("email", _emailInput))
.Send((response) =>
{
if (!response.HasErrors)
{
Debug.Log("Player registered");
}
else
{
Debug.LogWarning("Failed to register player...\n" + response.Errors.JSON.ToString());
}
});
}
}
This should resolve you issue. If you ahve any further issues. Please don’t hesitate to make use of our active community forums located here: https://support.gamesparks.net/discussions/forums/1000077208
Or our ticket system where our dedicated support team will be able to assist you directly: https://support.gamesparks.net/helpdesk/tickets
Hope I have been of help. Looking forward to hearing from you.
Best Regards, Patrick-GameSparks.
This updated script works well for Facebook login after the SDK changes. Will you be updating your website documentation soon? It took me some time to stumble upon this.
Am I right in saying that this doesn’t work for users coming back wanting to sign in again with their already authorised facebook account?
I find that when I assign a button to ConnectWithFacebook(), it only works the very first time. I’ve added the following section under FacebookLogin() and it seems to have fixed the problem but could someone double check it as I want to make sure the authentication is working correctly for returning users:
if (FB.IsLoggedIn) {
GSFacebookLogin (AfterFBLogin);
}
FacebookLogin() now looks like this;
void FacebookLogin ()
{
if (!FB.IsLoggedIn) {
Debug.Log ("Logging into Facebook");
FB.LogInWithReadPermissions (
new List<string> () { "public_profile", "email", "user_friends" },
GameSparksFBConnect
);
}
if (FB.IsLoggedIn) {
GSFacebookLogin (AfterFBLogin);
}
}
You have to save the facebook access token on the initial log in, then on return just feed it into the GameSparks FacebookConnectionRequest. See my code below for an example.
Theres probably a better way to do it but this has been working for me.
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Facebook;
using Facebook.Unity;
using GameSparks.Api;
using GameSparks.Api.Requests;
using GameSparks.Api.Responses;
public class Auth : MonoBehaviour {
//General player information variables
public string fbAuthToken = "";
void Awake(){
//load authtoken from playerprefs and see if it's been filled in
Load ();
if (fbAuthToken != "") {
GameSparksFBLogBackIn ();
}
}
public void CallFBInit(){
//initialize facebook
FB.Init(this.OnInitComplete, this.OnHideUnity);
}
private void OnInitComplete(){
Debug.Log ("FB.Init completed: Is user logged in? " + FB.IsLoggedIn);
CallFBLogin ();
}
private void CallFBLogin(){
FB.LogInWithReadPermissions (new List<string>() { "public_profile", "email"}, GameSparksFBLogin);
}
private void GameSparksFBLogin(ILoginResult result){
if (FB.IsLoggedIn) {
fbAuthToken = AccessToken.CurrentAccessToken.TokenString;
Save ();
new FacebookConnectRequest ()
.SetAccessToken (AccessToken.CurrentAccessToken.TokenString)
.SetSwitchIfPossible (true)
.SetSyncDisplayName(true)
.Send ((response) => {
if (response.HasErrors) {
Debug.Log ("Something failed when connecting with Facebook - "+result.Error);
} else {
Debug.Log ("Gamesparks Facebook login successful");
}
});
}
}
private void GameSparksFBLogBackIn(){
new FacebookConnectRequest ()
.SetAccessToken (fbAuthToken)
.Send ((response) => {
if (response.HasErrors) {
Debug.Log ("Something failed when connecting with Facebook on log back in");
} else {
Debug.Log ("Gamesparks Facebook login successful on log back in");
}
});
}
private void OnHideUnity(bool isGameShown){
}
void Save()
{
PlayerPrefs.SetString("fbAuthToken", fbAuthToken);
}
void Load()
{
fbAuthToken = PlayerPrefs.GetString("fbAuthToken");
}
}
I’m not using GameSparks (using other similar services), but with Facebook, don’t you run into issues with expired access tokens? From the looks of it, your LogBackIn call never logs you back into Facebook? Unless I misread, which means you wouldn’t have any of the FB social features if you were using those.
@Brathnann I only posted the non-project specific areas of my Auth class, when the LogBackIn method fails I have a UI popup asking if the user would like to sign back in.
As far as I understand it the access token is refreshed every time it is used, if it is not used, it expires in 60 days, or when nullified by dis-allowing the app access to the users facebook account.
Yep, I have read the docs, already have a game live on FB, was just curious how you were handling possible expired tokens. We just init Facebook when a person loads up the game, which gives us the token. Of course, with webGL on Facebook, we can’t use playerprefs, so even our mobile version just follows the same pattern.
Doesn’t calling the init function just pull up the authorization screen again? When I tried doing it this way, I got a facebook popup saying “This app is already authorized” with the option of “ok”. I didn’t want the user to have to see that every time the app is launched. Are you doing it in the background somehow?
On mobile, this only occurs if the user does a fresh install. In which case, if they don’t have the Facebook app, they are prompted to log in, after which it will say they already authorized. (This will only happen once). I think Facebook might be storing something in the cache, as I think you can clear the cache and it will prompt you again if I remember correctly.
If they have the Facebook app and are logged into it, it seems to get the info from there, so they don’t have to log in. But if a player logs out of the Facebook app, I think it prompts again. I know we did a lot of test on this before.
On Facebook itself, it just logs them straight in. But they will get prompts if they removed the app from their authorized apps, which removes any requested permissions.
My understanding of FB.init was without it you couldn’t do any of the other functions and Facebook wouldn’t record that a person was playing your game. So we just always run it at the start before doing the loginWithReadPermissions.
I am curious if you were getting that notice every time you loaded up the game what the reason for that was, as I get different results.