Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This question was closed Feb 02, 2017 at 01:15 AM by sten360 for the following reason:

Solved with other methods

avatar image
0
Question by sten360 · Nov 23, 2016 at 02:32 PM · androidfunctiongoogle playgoogle play games

C# function continues without waiting for called Google Play method to complete

Okay maybe I'm just missing something blatantly obvious here and sorry if that's the case, but I'm stumped. I'm trying to adjust my Google Play Services log-in and cloud save system to have a little more flexibility. This is my code (I'll only provide the relevant sections):

 void Start () {
         ActivateSocialPlatform ();
         LoginSuccess = false;
         Login ();
 }
 
 private void Login(){
     DebugText.text += "Starting login process \n";
     GooglePlaySignIn ();
     if (LoginSuccess == true) {
         LoadData();
     } else {
         DebugText.text += "Something went wrong \n";
     }
 }
 
 public void GooglePlaySignIn(){
     DebugText.text += "Signing into Google Play \n";
     Social.localUser.Authenticate ((bool success) => {
         if (success) {
             DebugText.text += "Google Play authentification success \n";
             LoginSuccess = true;
         } else {
             DebugText.text += "Google Play authentification failed \n";
             LoginSuccess = false;
         }
     }); 
 } 

Basically, I call Login() to start the authentification process in GooglePlaySignIn(), which is supposed to set the variable LoginSuccess either true or false, and if it's true, I start loading savedata from the cloud. However I end up with a log like this:

 Activating Social Platform
 Starting login process
 Signing into Google Play
 Something went wrong
 Google Play authentification success

As I understand, for some reason the function Login() doesn't wait until GooglePlaySignIn() completes and continues into the false state, AFTER which the user is actually signed on. Why does this happen? I thought all functions are supposed to wait until their called functions complete before continuing with their own code? Everything works just fine if I place the authentification code inside Login() itself, instead of having it in a separate function. I want to be able to reuse this code though, instead of having to duplicate it every time I need the user to log in again.

Thanks in advance.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

  • Sort: 
avatar image
0

Answer by hexagonius · Nov 23, 2016 at 09:56 PM

Login is just a method that does not know how to wait. it executes it's block in one go. the only part that is waiting here is Authenticate(). internally that's an asynchronous call, going all the way through the internet and when returning executes the lambda expression you passed in as a parameter. If you want log-in to wait make it a coroutine and yield for a success. or put LoadData into the Lambda callback where it checks for success. OR create your own callback that you pass to GooglePlaySignIn which you call on success.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image sten360 · Nov 23, 2016 at 11:14 PM 0
Share

Thanks for the reply! I guess I've misunderstood then? I thought all functions wait by default before they continue executing their code. Could've sworn I've read this somewhere. Anyhow, creating a callback was actually my initial idea, but Unity kept giving me "cannot convert bool to void" errors. This is the code:

 public bool GooglePlaySignIn(){
     DebugText.text += "Signing into Google Play \n";
     Social.localUser.Authenticate ((bool success) => {
         if (success) {
             DebugText.text += "Google Play authentification success \n";
             return true;
         } else {
             DebugText.text += "Google Play authentification failed \n";
             return false;
         }
     }); 
 } 

How could I make something like this work? I only started coding in C# a few days ago so my apologies for asking stupid questions.

avatar image Bunny83 sten360 · Nov 25, 2016 at 10:51 AM 0
Share

You seem to have problems reading that code of yours properly ^^. You actually pass a lambda expression into the Authenticate method. A lambda expression is just like an anonymous method. It's the same as if you have written this:

 public bool GooglePlaySignIn()
 {
     DebugText.text += "Signing into Google Play \n";
     Social.localUser.Authenticate(OnRequestCompleted);
 } 
 
 void OnRequestCompleted (bool success)
 {
     if (success) {
         DebugText.text += "Google Play authentification success \n";
     } else {
         DebugText.text += "Google Play authentification failed \n";
     }
 }

You actually pass a "reference" to the "OnRequestCompleted" method to the Authenticate method. The Authenticate method internally starts a new thread which runs independently from the main thread. This thread will perform a web request or something similar which might take some time to complete. Once it completed it will execute the method you passed to Authenticate. In the meantime the main thread has already continued. You might want to use a coroutine which polls the state of a boolean until the callback has been invoked.

avatar image sten360 Bunny83 · Nov 25, 2016 at 04:08 PM 0
Share

I'm sorry, I keep trying and trying with coroutines but always run into issues. I just don't fully understand how to work around this lambda expression. Could you please point me in the right direction with actual code? Ignore the main post for now, I've decided to forego the Login() function. I'll try to give a clearer explanation to what I'm trying to do.

Basically, whenever I need to have communication with the Google Play servers, e.g through functions like SaveGame(), LoadGame(), Load$$anonymous$$erboard() etc, I need to first check whether the user is still logged in/authenticated. To do this, I wanted to create an AuthCheck() function that returns a boolean so the functions know whether to continue or display an error. I want AuthCheck() to first check whether the user is still authenticated, if yes return true, if no try to log in again, and if that should fail as well, return false. I realize I could just check authentication with a simple if(Social.localUser.authenticated){} in every function separately and my entire problem would be solved, but I don't want to copypaste the entire retry login process to all these separate functions. The code would be much cleaner if I could figure out how to get AuthCheck() to work. This way the initial login could also just be handled by AuthCheck() and I'd simply call LoadData() in the very beginning of the game. It all seems simple, except I just need for other functions to wait until the logging in is complete.

     void Start () {
         ActivateSocialPlatform ();
         LoadData();
     }
   
     public void LoadData(){
         if (AuthCheck ()) {
             CloudSaving = false;
             GetSaveFromCloud();
         } 
     }
 
     private bool AuthCheck(){
         if (Social.localUser.authenticated) {
             DebugText.text += "Authentication success \n";
             return true;
         } else {             
             // try to log in again
             DebugText.text += "Authentication failed \n";
             GooglePlaySignIn();    // I need this function to wait until GooglePlaySignIn is complete, AND receive an answer whether it worked or not (true/false). Then return this function to false. How?        
         }
     }
         
     private void GooglePlaySignIn(){
         Social.localUser.Authenticate ((bool success) => {
             if (success) {
                 DebugText.text += "Google Play authentification success \n";
                 // somehow make AuthCheck know login succeeded
             } else {
                 DebugText.text += "Google Play authentification failed \n";
             // somehow make AuthCheck know login failed
             }
         }); 
     }
  
 
 // other functions for context, I don't want any of these including LoadData to change though.
     public void LoadAchievements(){
         if (AuthCheck()){
             Social.ShowAchievementsUI();
         } 
     }
 
     public void SaveData(){
         if (AuthCheck ()) {
             CloudSaving = true;
             GetSaveFromCloud();
         } 
     }

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Social.localUser.Authenticate((bool success) returns no bool 1 Answer

Google Play ShowLeaderBoard(); won't show 0 Answers

How to save and load string using google services ? 0 Answers

Google Play - Android Setup - config window errors 1 Answer

Google play code I don't know where to use it 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges