Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Firebase Asynch programming

Discussion in 'Multiplayer' started by Jeunas, Dec 16, 2017.

  1. Jeunas

    Jeunas

    Joined:
    Sep 5, 2017
    Posts:
    14
    Hello everyone

    I didn't know just where to put this topic, so as it is kind of a utility to creating a multiplayer game I decided to post it here.

    I am having trouble understanding this code i have here:


    Code (CSharp):
    1. public double GetBankBalance()
    2.     {
    3.         double bankBalance = 0;
    4.  
    5.         Router.PlayerMoney("ymO6Hyl8WjbuPGaWV7FksbVABGb2").GetValueAsync().ContinueWith(task =>
    6.         {
    7.         if (task.IsFaulted)
    8.             {
    9.                 Debug.Log("Error in GetBankBalance()");
    10.             }
    11.         else if (task.IsCompleted)
    12.             {
    13.                 DataSnapshot snapshot = task.Result;
    14.                 bankBalance = Convert.ToDouble(snapshot.Child("bankBalance").Value);
    15.                 //UpdateBankBalanceText(this.bankBalance);
    16.             }
    17.        
    18.  
    19.         });
    20.  
    21.         return bankBalance;
    22.     }
    So I am trying to get a value from my database so I can return it as a value to use when needed. The problem is that the "return bankBalance;" is executed before the task is done. I get the correct value, but too late. I can't seem to figure out how to wait for the task to be finished. I can't use Task.Wait(t) because for some reason it keeps throwing that the Wait method isn't in the definition of "Task" (yes I added a using statement so I can use "Task").

    Could anyone explain how I can make this work? Tried a lot and searched on the internet but I can't seem to figure it out.

    Thanks in advance
     
    Last edited: Dec 16, 2017
  2. hasanbayat

    hasanbayat

    Joined:
    Oct 18, 2016
    Posts:
    629
    Hi.
    Please use Code tags.
    You should use a Async solution, so you can use Callbacks in this case.
    Thanks.
     
  3. Jeunas

    Jeunas

    Joined:
    Sep 5, 2017
    Posts:
    14
    Thanks for the response.
    Care to explain how I could implement this best in this method?
     
  4. hasanbayat

    hasanbayat

    Joined:
    Oct 18, 2016
    Posts:
    629
    Create a delegate and accept the callback:
    Code (CSharp):
    1. public delegate BankBalanceCallback (double balance);
    2.  
    3. public double GetBankBalance(BankBalanceCallback cb)
    4. {
    5.     double bankBalance = 0;
    6.     Router.PlayerMoney("ymO6Hyl8WjbuPGaWV7FksbVABGb2").GetValueAsync().ContinueWith(task =>
    7.     {
    8.         if (task.IsFaulted)
    9.         {
    10.             Debug.Log("Error in GetBankBalance()");
    11.         }
    12.         else if (task.IsCompleted)
    13.         {
    14.             DataSnapshot snapshot = task.Result;
    15.             bankBalance = Convert.ToDouble(snapshot.Child("bankBalance").Value);
    16.             if (cb != null) {
    17.                 cb(bankBalance);
    18.             }
    19.             //UpdateBankBalanceText(this.bankBalance);
    20.         }
    21.     });
    22.     return bankBalance;
    23. }
    It is the solution, or you can have a look at the resources below:
    Thanks.
     
  5. Jeunas

    Jeunas

    Joined:
    Sep 5, 2017
    Posts:
    14
    Thanks for your help. But I still can't figure out how to properly implement that into my code so I'll just try to add it to the task from within the task.

    Thanks again for the assistance!
     
  6. MGGDev

    MGGDev

    Joined:
    Nov 6, 2018
    Posts:
    27
    Same question here too.
     
  7. Hemachandran

    Hemachandran

    Joined:
    May 1, 2016
    Posts:
    1
    The callback is not working.
     
  8. artoonie

    artoonie

    Joined:
    Feb 9, 2017
    Posts:
    22
    For future googlers who may stumble here like I did:

    You will need the return value in the delegate:
    Code (CSharp):
    1. public delegate void BankBalanceCallback (double balance);