Hello guys. I've recently started to use PlayFab in hopes that I may be able to store player's game data so the next time they come back to the game, they can start where they left last time. As this is also the first time I used a backend service, the whole thing is a bit confusing to me.
Here's what I'm trying to achieve:
1. Connect to Kongregate API then login to PlayFab based on user info
2. Automatically start checking if the user is a newly created account or an existing one. If it's a newly created account, make a new player on Game Manager and add the initial data for them. If it's an existing user, start loading their saved data and use them in the game.
I did the first step with an early build and it worked. PlayFab created my Kongregate account on the Players tab with the correct informations. So I went ahead and tried the save/load step and came across a problem when I test my build out. This error come up in a text I set up in Unity:
"Error logging in player with custom ID: Unknown Error."
To give a bit more informations out, my early build consist of the two scripts below. The difference is, my PlayFabManager doesn't have both the SaveUserData() and LoadUserData() method and the condition check that I added inside the Login() method. My current build basically has the same exact scripts that I provided below. I also tried deleting the user using the Try It button in the docs but it doesn't solve anything.
Here's the PlayFabManager code:
using UnityEngine; using UnityEngine.UI; using System.Collections.Generic; using PlayFab; using PlayFab.ClientModels; public class PlayFabManager : MonoBehaviour { public static PlayFabManager instance; public string playFabID; public string titleID; public List<string> items; public Text playfabConnection; public Text playfabStatus; public Text userDataString; void Awake() { instance = this; } public void Login(string kongregateID, string authTicket) { LoginWithKongregateRequest request = new LoginWithKongregateRequest() { TitleId = titleID, // UserID KongregateId = kongregateID, // GameAuthToken AuthTicket = authTicket, CreateAccount = true, }; PlayFabClientAPI.LoginWithKongregate(request, (result) => { // Get unique PlayFab ID playFabID = result.PlayFabId; // Connected playfabConnection.text = "Got PlayFabID: " + playFabID; if (result.NewlyCreated) { // If new account playfabStatus.text = "(new account)"; } else { // If existing account playfabStatus.text = "(existing account)"; } }, (error) => { // Error message playfabStatus.text = string.Format("Error logging in player with custom ID:\n{0}", error.ErrorMessage); }); } public void SaveUserData() { UpdateUserDataRequest request = new UpdateUserDataRequest() { Data = new Dictionary<string, string> { // Save score to Score key { "Score", GameManager.instance.score.ToString() } } }; PlayFabClientAPI.UpdateUserData(request, (result) => { Debug.Log("Successfully updated user data"); }, (error) => { // Error message Debug.Log(error.ErrorDetails); }); } public void LoadUserData() { GetUserDataRequest request = new GetUserDataRequest() { PlayFabId = playFabID, // Get all data Keys = null, }; PlayFabClientAPI.GetUserData(request, (result) => { // Got data Debug.Log("Got user data"); if (result == null || result.Data.Count == 0) { // No data available Debug.Log("No user data available"); } else { // Clear list first items.Clear(); foreach (var item in result.Data) { // Retrieve user item and add to list string userItem = string.Format(item.Key + ":" + item.Value.Value); items.Add(userItem); } // Add the list of item together as one string to be used later userDataString.text = string.Join("|", items.ToArray()); } }, (error) => { // Error message Debug.Log(error.ErrorMessage); }); } }
Here's the KongregateAPI code:
using UnityEngine; using UnityEngine.UI; public class KongregateAPI : MonoBehaviour { public static KongregateAPI instance; public int userID; public string username; public string gameAuthToken; public Text connectText; void Awake() { instance = this; Application.ExternalEval( @"if(typeof(kongregateUnitySupport) != 'undefined'){ kongregateUnitySupport.initAPI('KongregateAPI', 'OnKongregateAPILoaded'); };"); } public void OnKongregateAPILoaded(string userInfoString) { OnKongregateUserInfo(userInfoString); // Login when Kongregate is connected PlayFabManager.instance.Login(userID.ToString(), gameAuthToken); } public void OnKongregateUserInfo(string userInfoString) { var info = userInfoString.Split('|'); userID = System.Convert.ToInt32(info[0]); username = info[1]; gameAuthToken = info[2]; // Display the user informations in a text connectText.text = string.Format("UserID: {0}\nUsername: {1}\nToken: {2}", userID, username, gameAuthToken); } public static void SubmitStat(string statName, int statValue) { Application.ExternalCall("kongregate.stats.submit", statName, statValue); } }