Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
1
Question by DanFrias · Feb 12, 2013 at 02:59 AM · yieldcoroutines

Waiting for a coroutine to complete before getting a return value

Here is what I am trying to do:

I have a program that requires a player be logged into Facebook in order to perform certain functions. I have the Facebook login process which works by taking delegate as a parameter and then calling that delegate upon completion of the login with a bool value representing success or failure.

Here is where it gets a bit tricky. I want to fully encapsulate the login command within a class so that the user of the class does not have to worry about the login specifically when attempting to query data. For example, I have another function which retrieves the friends list of the user who is currently logged in. If the GetFriends function is called I would first like to check if a user is logged in and, if so, simply return the friends list which would have already been retrieved. However, if not logged in, this function will throw up the login prefab and wait for the user to log in. Once the user has done so it will then retrieve the friends list and return it back as the return value to the GetFriends function that initiated this process.

I have thought about having the user of the class check the login state first but this tends to get a bit messy with having to themselves create delegates and wait for the callback to ensure login occurred before being able to query friends. I would much prefer my class takes care of this.

I have attempted to do this with coroutines but have had difficulties returning the friends list back up to the original calling function. I have also had trouble getting a function whose type is not IEnumerator to wait for a coroutine to finish (in this case the coroutine is the login process). Can anyone suggest a good way to do what I'm asking? I really need the login to be handled by my class and not place this responsibility upon the user of my class.

To try to sum it up, I need this function:

   public List<FacebookFriend> GetFriends() 

to be able to login, if needed, and then return the friends list back.

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

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by aldonaletto · Feb 12, 2013 at 04:44 AM

I would try something like this:

 using UnityEngine;
 using System.Collections;
 
 public class Facebook: MonoBehaviour {
 
     private List<FacebookFriend> friendList = null;
     private bool busy = false;
 
     public static List<FacebookFriend> GetFriends(){
         if (friendList == null){ // friend list not ready:
             // if CoroutGetFriends not running yet, start it:
             if (!busy) StartCoroutine(CoroutGetFriends());
         }
         return friendList; // return null if not ready
     }
 
     IEnumerator CoroutGetFriends(){
         busy = true; // CoroutGetFriends is running now
         if (!logged){ // not logged yet?
             // chain to Login coroutine:
             yield return StartCoroutine(Login());
         }
         // user logged in by now: get friends
         ...
         busy = false; // CoroutGetFriends ended
     }

     IEnumerator Login(){
         ...
     }
 }

In the calling script, call GetFriends to get the list: if it's ready, GetFriends just returns it; if not, it returns null and starts the login process automatically - do nothing while GetFriends return null:

 void Update(){
     List<FacebookFriend> friends = Facebook.GetFriends();
     if (friends != null){
         // the list is ready: do whatever you want to do with it
     }
 }
  
Comment
Add comment · Show 1 · 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 DanFrias · Mar 13, 2013 at 10:53 PM 0
Share

Thanks for your help. I ended up doing something similar to this.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

10 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Coroutines and states 1 Answer

Are coroutines freezing my game? 0 Answers

How do I properly use Coroutines, yield and animators to sequence commands ? 2 Answers

yield, Coroutines, InvokeRepeating... 1 Answer

Inconsistent StopCoroutine behavior with CustomYieldInstruction 1 Answer


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