question

Hussien avatar image
Hussien asked

Json problem with Playfab and unity 2019.2

Hello I can't save my statistics player on the cloud because I have this error in the console

Assets/Scripts/PlayFabController.cs(266,19): error CS0103: The name 'JsonWrapper' does not exist in the current context

and this my code below

using System.Collections.Generic; using UnityEngine; using PlayFab; using PlayFab.ClientModels; using PlayFab.Json; public class PlayFabController : MonoBehaviour { // Static instance for this Class. public static PlayFabController PFC; private string userEmail = "s@gmail.com"; private string userPassword = "123456789"; private string myUserName = "jussien"; public GameObject loginPanel; public GameObject addLoginPanel; public GameObject recoverButton; // create singleton static. private void OnEnable() { if (PlayFabController.PFC == null) { PlayFabController.PFC = this; } else { if (PlayFabController.PFC != this) { Destroy(this.gameObject); } } DontDestroyOnLoad(this.gameObject); } public void Start() { //Note: Setting title Id here can be skipped if you have set the value in Editor Extensions already. if (string.IsNullOrEmpty(PlayFabSettings.TitleId)) { PlayFabSettings.TitleId = "7C8FB"; // Please change this value to your own titleId from PlayFab Game Manager } // to delete user date. //PlayerPrefs.DeleteAll(); //var request = new LoginWithCustomIDRequest { CustomId = "GettingStartedGuide", CreateAccount = true }; //PlayFabClientAPI.LoginWithCustomID(request, OnLoginSuccess, OnLoginFailure); if (PlayerPrefs.HasKey("EMAIL")) { userEmail = PlayerPrefs.GetString("EMAIL"); userPassword = PlayerPrefs.GetString("PASSWORD"); var request = new LoginWithEmailAddressRequest { Email = userEmail, Password = userPassword }; PlayFabClientAPI.LoginWithEmailAddress(request, OnLoginSuccess, OnLoginFailure); } // create account with Android ID without usieng email. else { #if UNITY_ANDROID var requestAndroid = new LoginWithAndroidDeviceIDRequest { AndroidDeviceId = ReturnMobileID(), CreateAccount = true }; PlayFabClientAPI.LoginWithAndroidDeviceID(requestAndroid, OnLoginMobieSuccess, OnLoginMobileFailure); #endif #if UNITY_IOS var requestIOS = new LoginWithIOSDeviceIDRequest { DeviceId = ReturnMobileID(), CreateAccount = true }; PlayFabClientAPI.LoginWithIOSDeviceID(requestIOS, OnLoginMobieSuccess, OnLoginMobileFailure); #endif } } #region Login // todo: login function private void OnLoginSuccess(LoginResult result) { Debug.Log("Congratulations, you made your first successful API call!"); // to save date of player when login again. PlayerPrefs.SetString("EMAIL", userEmail); PlayerPrefs.SetString("PASSWORD", userPassword); loginPanel.SetActive(false); recoverButton.SetActive(false); GetStats(); } // todo: failure func. private void OnLoginFailure(PlayFabError error) { var registerRequest = new RegisterPlayFabUserRequest { Email = userEmail, Password = userPassword, Username = myUserName }; PlayFabClientAPI.RegisterPlayFabUser(registerRequest, OnRegisterSuccess, OnRegisterFailure); } // todo: Registration func public void OnRegisterSuccess(RegisterPlayFabUserResult result) { Debug.Log("Congratulations. you made your first successful API"); // to save date of player when login again. PlayerPrefs.SetString("EMAIL", userEmail); PlayerPrefs.SetString("PASSWORD", userPassword); loginPanel.SetActive(false); GetStats(); } // todo: Resister Failure func. public void OnRegisterFailure(PlayFabError error) { Debug.LogError(error.GenerateErrorReport()); } // todo: login function private void OnLoginMobieSuccess(LoginResult result) { Debug.Log("Congratulations, you made your first successful API call!"); loginPanel.SetActive(false); GetStats(); } // todo: failure Android func. private void OnLoginMobileFailure(PlayFabError error) { Debug.Log(error.GenerateErrorReport()); } // todo: username func public void GetUserEmail(string emailIn) { userPassword = emailIn; } // todo: password func public void GetUserPassword(string passwordIn) { userPassword = passwordIn; } // todo: password func public void GetUserName(string userNameIn) { myUserName = userNameIn; } public void OnClickLogin() { var request = new LoginWithEmailAddressRequest { Email = userEmail, Password = userPassword }; PlayFabClientAPI.LoginWithEmailAddress(request, OnLoginSuccess, OnLoginFailure); } public static string ReturnMobileID() { string deviceID = SystemInfo.deviceUniqueIdentifier; return deviceID; } public void OpenAddLogin() { addLoginPanel.SetActive(true); } public void OnClickAddLogin() { var addLoginRequest = new AddUsernamePasswordRequest { Email = userEmail, Password = userPassword, Username = myUserName }; PlayFabClientAPI.AddUsernamePassword(addLoginRequest, OnAddLoginSuccess, OnRegisterFailure); } private void OnAddLoginSuccess(AddUsernamePasswordResult result) { Debug.Log("Congratulations, you made your first successful API call!"); PlayerPrefs.SetString("EMAIL", userEmail); PlayerPrefs.SetString("PASSWORD", userPassword); addLoginPanel.SetActive(false); } #endregion Login #region PlayerStats // Create Statistics Players. public int playerLevel; public int gameLevel; public int playerHealth; public int playerDamage; public int playerHighScore; // todo: function which Button Call it to set the values inside it. public void SetStats() { PlayFabClientAPI.UpdatePlayerStatistics(new UpdatePlayerStatisticsRequest { // request.Statistics is a list, so multiple StatisticUpdate objects can be defined if required. Statistics = new List<StatisticUpdate> { new StatisticUpdate { StatisticName = "PlayerLevel", Value = playerLevel }, new StatisticUpdate { StatisticName = "GameLevel", Value = gameLevel }, new StatisticUpdate { StatisticName = "PlayerHealth", Value = playerHealth }, new StatisticUpdate { StatisticName = "PlayerDamage", Value = playerDamage }, new StatisticUpdate { StatisticName = "PlayerHighScore", Value = playerHighScore }, } }, result => { Debug.Log("User statistics updated"); }, error => { Debug.LogError(error.GenerateErrorReport()); }); } void GetStats() { PlayFabClientAPI.GetPlayerStatistics( new GetPlayerStatisticsRequest(), OnGetStats, error => Debug.LogError(error.GenerateErrorReport()) ); } // todo: func which pass the values to SetStats(); function above void OnGetStats(GetPlayerStatisticsResult result) { Debug.Log("Received the following Statistics:"); foreach (var eachStat in result.Statistics) { Debug.Log("Statistic (" + eachStat.StatisticName + "): " + eachStat.Value); switch (eachStat.StatisticName) { case "PlayerLevel": playerLevel = eachStat.Value; break; case "GameLevel": gameLevel = eachStat.Value; break; case "PlayerHealth": playerHealth = eachStat.Value; break; case "PlayerDamage": playerDamage = eachStat.Value; break; case "PlayerHighScore": playerHighScore = eachStat.Value; break; } } } // Build the request object and access the API public void StartCloudUpdatePlayerStats() { PlayFabClientAPI.ExecuteCloudScript(new ExecuteCloudScriptRequest() { FunctionName = "UpdatePlayerStats", // Arbitrary function name (must exist in your uploaded cloud.js file) FunctionParameter = new { Level = playerLevel, highScore = playerHighScore, apple = 0 }, // The parameter provided to your function GeneratePlayStreamEvent = true, // Optional - Shows this event in PlayStream }, OnCloudUpdateStats, OnErrorShared); } // OnCloudHelloWorld defined in the next code block private static void OnCloudUpdateStats(ExecuteCloudScriptResult result) { // Cloud Script returns arbitrary results, so you have to evaluate them one step and one parameter at a time Debug.Log(JsonWrapper.SerializeObject(result.FunctionResult)); JsonObject jsonResult = (JsonObject)result.FunctionResult; object messageValue; jsonResult.TryGetValue("messageValue", out messageValue); // note how "messageValue" directly corresponds to the JSON values set in Cloud Script Debug.Log((string)messageValue); } private static void OnErrorShared(PlayFabError error) { Debug.Log(error.GenerateErrorReport()); } #endregion PlayerStats }

unity3dLeaderboards and Statistics
1 comment
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Hussien avatar image Hussien commented ·

@Sarah Zhang. thanks fo replay I tried your solution and its work fine but my first question o can use PlayFabSimpleJson.SerializeObject instead sonWrapper.Serialize.?

and where I can put my code here in post with right way?

0 Likes 0 ·
Sarah Zhang avatar image
Sarah Zhang answered

The code you provide is not very easy to identify the line’s number, so I just searched the part of “JsonWrapper” and checked it. Actually, JsonWrapper isn’t included in the namespace PlayFab.Json. It’s included in the PlayFab.PfEditor.Json. And this namespace is in the Assembly-CSharp-Editor project, it’s designed to help PlayFab SDK work. You can also find the following comment in the PlayFab.Json. “Most users shouldn't access this JsonWrapper.Serialize, and JsonWrapper.Deserialize will always use it automatically (Unless you deliberately mess with them). Any Serialization of an object in the PlayFab namespace should just use JsonWrapper.”

So, you can use PlayFabSimpleJson.SerializeObject instead of JsonWrapper.SerializeObject. PlayFabSimpleJson is public for users.

3 comments
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Hussien avatar image Hussien commented ·

@Sarah Zhang thanks fo replay I tried your solution and its work fine but my first question o can use PlayFabSimpleJson.SerializeObject instead sonWrapper.Serialize.?

and where I can put my code here in post with right way?

0 Likes 0 ·
Sarah Zhang avatar image Sarah Zhang Hussien commented ·

You can try to attach it as a text file or use [insert code]. Please pay attention to hide your private information.

0 Likes 0 ·
picture1.png (12.8 KiB)
David Coombes avatar image David Coombes commented ·

Can you update the documentation to use the correct calls please. I copied the code here for testing and it had compiler errors, requiring me to search for the fix. I believe I need to include...

Using PlayFab.Json

...in my Unity code, and replace the JsonWrapper references.

0 Likes 0 ·
Benjamin avatar image
Benjamin answered

oh is to correct: PlayFabSimpleJson.SerializeObject

10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.