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
0
Question by XoetziX · Jan 18 at 06:30 AM · databasewait

How to wait for a database result?

Hello everyone,

I guess I have a rather standard issue, but I do not figure out how to solve it.

The szenario is a racing game where at the moment the finish line is reached the following things shall happen:

  1. Load previous highscores from the database

  2. Do some magical calculation (check if new top highscore, etc.)

  3. Save the new highscore list to the database

The problem I am facing right now is that 2 and 3 is done before I got the result from 1, hence I do not have the stored highscores yet before the rest is done.

So at the end I "simply" looking for a way to wait until loading the database is finished.

I tried different approaches with Coroutines and "yield return new WaitUntil" but non of it worked.


Here are the relevant parts of my code:

Class: HighscoreController

 public void AddHighScore(string playerName, float time)
     {
         currentPlayerHighscore = new PlayerHighscore(playerName, time);
         //LoadHighscores();
         highscoresDB = FirebaseManagerGame.instance.LoadHighscoresOfLevel(levelInfo.LevelName);
         Debug.LogWarning("HERE I HAVE TO WAIT UNTIL DATABASE RESULT IS THERE");
 
         //reset values
         newTopHighscore = false; inTop10 = false; notInTop10 = false;
 
        [...] some magical calculation [...]
         highscoresDB.Sort(SortByTime);
     
         SaveHighscoresDB();
         ShowHighScores();
     }


Class: FirebaseManagerGame

 public List<PlayerHighscore> LoadHighscoresOfLevel(string levelName)
     {
         Debug.Log("FireBaseGame - LoadHighscoresOfLevel START");
         List<PlayerHighscore> tmpList = new List<PlayerHighscore>();

         StartCoroutine(GetHighscoresOfLevel(levelName, returnValue =>
         {
             tmpList = returnValue; 
         }));
         Debug.Log("LoadHighscoresOfLevel List.count: " + tmpList.Count);
         return tmpList;
     }
 
 
 
     private IEnumerator GetHighscoresOfLevel(string levelName, System.Action<List<PlayerHighscore>> callback)
     {
         Debug.Log("FireBaseGame - getHighscoresOfLevel START");    
         List<PlayerHighscore> tmpHighscores = new List<PlayerHighscore>();            
         Task<DataSnapshot> DBTask = DBreference.Child("levelHighscores").Child(levelName).GetValueAsync();
         yield return new WaitUntil(predicate: () => DBTask.IsCompleted);
         if (DBTask.Exception != null)
         {
             Debug.LogWarning(message: $"Failed to register task with {DBTask.Exception}");
         }
         else if (DBTask.Result.Value == null) //No data exists yet
         {
             Debug.Log("getHighscoresOfLevel - NO DATA EXISTS");
         }
         else //Data has been retrieved
         {
             DataSnapshot snapshot = DBTask.Result;
             tmpHighscores = JsonConvert.DeserializeObject<List<PlayerHighscore>>(snapshot.GetRawJsonValue());
             Debug.Log("getHighscoresOfLevel - FOUND DATA - count: " + tmpHighscores.Count);
             callback(tmpHighscores);
         }
     }

I hope you can help me to solve this issue.

Thanks in advance!

oetzi

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
0
Best Answer

Answer by XoetziX · Jan 20 at 04:07 PM

Here is the solution. The magic trick are 2 words: "yield return" which cause the method to wait until the database results returned. To be able to use these magic words I had to change the AddHighScore method into an IEnumerator.

 public IEnumerator AddHighScore(string playerName, float time)
 {
     currentPlayerHighscore = new PlayerHighscore(playerName, time);
 
     //IEnumerator cannot return any internal values / variables. Hence, the following way can be used:
     //pass a place holder (= returnValue) to the IEnumerator. Within the IEnumerator this placeholder can be set to any value. Outside the Enumerator / here it can be further processed. 
     yield return StartCoroutine(FirebaseManagerGame.instance.GetHighscoresOfLevel(levelInfo.LevelName, returnValue =>
     {
         highscoresDB = returnValue;
     }));
 [...]

Comment
Add comment · 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

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

138 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Update mySQL at game runtime ??? 2 Answers

Unity3d and Database and MultiPlayer Games 1 Answer

trouble finding the route to connect iPhone Unity App to a CPanel database 1 Answer

Fatal Error: Database or Disk is Full. 0 Answers

write text contents on text file which is in server 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