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:
public double GetBankBalance()
{
double bankBalance = 0;
Router.PlayerMoney("ymO6Hyl8WjbuPGaWV7FksbVABGb2").GetValueAsync().ContinueWith(task =>
{
if (task.IsFaulted)
{
Debug.Log("Error in GetBankBalance()");
}
else if (task.IsCompleted)
{
DataSnapshot snapshot = task.Result;
bankBalance = Convert.ToDouble(snapshot.Child("bankBalance").Value);
//UpdateBankBalanceText(this.bankBalance);
}
});
return bankBalance;
}
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