Hello. I have a script that logs in or prompts account register for a player. Is there a way to wait until the player is logged in or not so that when I make API calls from another script, it doesn't give me the "Must be logged in to call this method" error?
Answer by Paul Boutros · Oct 27, 2020 at 01:15 AM
Well I suppose your API login call looks like :
PlayFabClientAPI.LoginWithCustomID(request, OnLogin, OnLoginError);
right? So you could have a public readonly bool variable in your OnLogin for example:
OnLogin(){
currentlyLoggedIn = true;
}
Your other script could run a coroutine that would do something like:
While( !myExampleLoginScript.currentlyLoggedIn ) {
//don't go further until myExampleLoginScript.currentlyLoggedIn
//is set to true
yield return null;
}
// when it finally is set to true
// Do the intended task here
Good answer! I set a bool to true when the player is logged in and then access it from whatever script that needs to make api calls.
Answer by SethDu · Oct 27, 2020 at 05:15 AM
In some SDKs, like Unity SDK and C# SDK, APIs are capsuled as asynchronous tasks. Paul Boutros provides a feasible workaround, however, in the common scenario, we will add the following APIs in the callback result function. I will use Unity SDK and C# Action as an example:
PlayFabClientAPI.LoginWithCustomID( new PlayFab.ClientModels.LoginWithCustomIDRequest { TitleId = "xxxx", CreateAccount = true, CustomId = "xxxxxxx" }, onSuccess => { print(PlayFabSettings.staticPlayer.ClientSessionTicket); PlayFabClientAPI.GetLeaderboard( // additional API calls ); }, onFailure=> { print(onFailure.GenerateErrorReport()); }); }