Hi there, I am testing out PlayFab and wanted to learn by making a simple C# app using Microsoft Forms in Visual Studio. I followed the instructions from the C# console application tutorial, with a few modifications to use Forms and a GUI instead of command line.
For this test, I made a simple app using NuGet to install NewtonSoft.JSON and PlayFab.AllSDKs.
The form is very simple: there is one button to submit the login command and a textbox to populate with log messages in the GUI. There is also a textbox which is used to enter a custom userID for testing the function "PlayFabClientAPI.LoginWithCustomIDAsync(request);"
My issue is that although everything seems to work in regards to sending the API call out, the application never receives the asynchronous reply with the confirmation receipt. I am new to asynch requests in C#, so I haven't yet been able to figure out why these responses aren't coming. To prevent my application from hanging forever in the wait loop, I added a timeout counter of 25 seconds. Even with all this time, the application doesn't seem to receive the async reply.
I have pasted the two relevant .cs files below. My project.cs and my form.cs. If you are familiar with the C# Getting Started tutorial where you build a console app that calls the API, most of this code will be familiar to you.
This is the Form's cs file which calls functions in the program and updates the GUI
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace CSharpGettingStarted { public partial class FormMainWindow : Form { public FormMainWindow() { InitializeComponent(); } private void FormMainWindow_Load(object sender, EventArgs e) { Console.WriteLine("Form Loaded Successfully!"); string[] startupMessages = { "Test App Loaded.", "Type in a new username or your old one.", "Then press the button to log in." }; printMultiGuiLog(startupMessages); } public void printGuiLog(string message) { if (message != null) textBoxGuiConsole.Text = textBoxGuiConsole.Text + message + Environment.NewLine; else textBoxGuiConsole.Text = "Error: no message to display."; } public void printMultiGuiLog(params string[] theMessages) { string newLog = string.Join(Environment.NewLine, theMessages); Console.Write(newLog); printGuiLog(newLog); } private void buttonLogIn_Click(object sender, EventArgs e) { string CustomUserID = textBoxUsernameField.Text; Program.OnLoginAttempt(CustomUserID); } } }
This is the Program.cs file with the functions that use the PlayFab SDK
using System; using System.Collections.Generic; using System.Linq; using System.Threading; using System.Threading.Tasks; using PlayFab; using PlayFab.Internal; using PlayFab.ClientModels; using CSharpGettingStarted; using System.Windows.Forms; public static class Program { private static bool _running = true; public static FormMainWindow myForm; // <-- this is the main GUI [STAThread] static void Main(string[] args) { PlayFabSettings.TitleId = "1234"; // NOTE: I have changed this value to my own titleId just not for pasting in the forums Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); myForm = new FormMainWindow(); Application.Run(myForm); } public static void OnLoginAttempt(string CustomUserID) { myForm.printGuiLog("Attempting to Log In as " + CustomUserID + "..."); _running = true; var request = new LoginWithCustomIDRequest { CustomId = CustomUserID, CreateAccount = true}; var loginTask = PlayFabClientAPI.LoginWithCustomIDAsync(request); int n = 1; // n is the timeout counter while (_running && n < 25000) //we're only waiting 25 seconds to get a reply. If none comes, don't let the program hang. { if (loginTask.IsCompleted) // You would probably want a more sophisticated way of tracking pending async API calls in a real game { OnLoginComplete(loginTask); } n++; //increment our timeout counter Thread.Sleep(1); } Console.Write("It looks like you timed out"); string[] timeOutMessages = { "API Request timed out after 25000 ms.", "Login attempted with UserID:" + CustomUserID + "." }; myForm.printMultiGuiLog(timeOutMessages); } private static void OnLoginComplete(Task<PlayFabResult<LoginResult>> taskResult) { var apiError = taskResult.Result.Error; var apiResult = taskResult.Result.Result; if (apiError != null) { myForm.printGuiLog(PlayFabUtil.GetErrorReport(apiError)); } else if (apiResult != null) { myForm.printGuiLog("You made a successfull API call!"); } else { Console.Write("ERROR: You called the OnLoginComplete without a result!"); } _running = false; // Because this is just an example, successful login triggers the end of the program } }
Thanks for reading.