,Call api and wait answer to return (async?)
I'm stuck since this morning on this problem, I've watched a lot of videos, tutorials etc. I'm not used to ask for the solution directly, but now I'm stuck.
I have a firebase real time database, and I receive the players in JSON, I can access a player precisely with his example file: https://minisoldiers-fdd66.firebaseio.com/Soldiers/username.json
I would have to be able to check whether or not this gambler already exists. For that I make a request to the URL, and I simply look if there is a name already present or the field is empty.
The problem is that the result of the query is in the .Then() and so the result comes after. I have to use async with coroutine, but I can't do it at all, it's the first time I do this, I would like to be given the solution this time to point me on this track there
Here is the code:
This method is in the DatabaseQueries class, and I have to do 1 method that will return a bool indicating if the username exists or not. I have to use async with coroutine here, and then call the method in the SoldierCreation class below
public class DatabaseQueries : MonoBehaviour
{
public static bool CheckIfUsernameExists(string username)
{
Soldier soldier = new Soldier();
RestClient.Get<Soldier>("https://minisoldiers-fdd66.firebaseio.com/Soldiers/" + username + ".json")
.Then(response =>
{
soldier = response;
return (soldier.name != null) ? true : false;
});
return true;
}
}
This method is in the SoldierCreation class, as soon as I press the button it should check if the username already exists or not. It would be necessary to complete this method
public void PostToDatabase()
{
if (DatabaseQueries.CheckIfUsernameExists(soldierName))
{
Soldier soldier = new Soldier(soldierName, selectedSkin);
Give(soldier, GetAllIdFromList(allWeaponsList));
RestClient.Put("https://minisoldiers-fdd66.firebaseio.com/Soldiers/" + soldier.name + ".json", soldier)
.Then(res => SceneManager.LoadScene("HouseScene"));
}
else
{
soldierNameWarning.text = "Ce soldat existe déjà !";
}
}
,I'm stuck since this morning on this problem, I've watched a lot of videos, tutorials etc. I'm not used to ask for the solution directly, but now I'm stuck.
I have a firebase real time database, and I receive the players in JSON, I can access a player precisely with his example file: https://minisoldiers-fdd66.firebaseio.com/Soldiers/username.json
I would have to be able to check whether or not this gambler already exists. For that I make a request to the URL, and I simply look if there is a name already present or the field is empty.
The problem is that the result of the query is in the .Then() and so the result comes after. I have to use async with coroutine, but I can't do it at all, it's the first time I do this, I would like to be given the solution this time to point me on this track there
Here is the code:
This method is in the DatabaseQueries class, and I have to do 1 method that will return a bool indicating if the username exists or not. I have to use async with coroutine here, and then call the method in the SoldierCreation class below
public class DatabaseQueries : MonoBehaviour
{
public static bool CheckIfUsernameExists(string username)
{
Soldier soldier = new Soldier();
RestClient.Get<Soldier>("https://minisoldiers-fdd66.firebaseio.com/Soldiers/" + username + ".json")
.Then(response =>
{
soldier = response;
return (soldier.name != null) ? true : false;
});
return true;
}
}
This method is in the SoldierCreation class, as soon as I press the button it should check if the username already exists or not. It would be necessary to complete this method
public void PostToDatabase()
{
if (DatabaseQueries.CheckIfUsernameExists(soldierName))
{
Soldier soldier = new Soldier(soldierName, selectedSkin);
Give(soldier, GetAllIdFromList(allWeaponsList));
RestClient.Put("https://minisoldiers-fdd66.firebaseio.com/Soldiers/" + soldier.name + ".json", soldier)
.Then(res => SceneManager.LoadScene("HouseScene"));
}
else
{
soldierNameWarning.text = "Ce soldat existe déjà !";
}
}
Answer by xxmariofer · Dec 02, 2020 at 10:35 AM
I dont know about that RestClient library, and you are missing a lot of information, Why "I have to use async with coroutine, but I can't do it at all"? why cant you use it? if you want to use Coroutines why dont simply use the JSONUtility and WebRequest classes from unity? if you want to use that library, why dont simpoly cll the PostToDatabase from inside the then like this:
public class DatabaseQueries : MonoBehaviour
{
public static bool CheckIfUsernameExists(string username)
{
Soldier soldier = new Soldier();
RestClient.Get<Soldier>("https://minisoldiers-fdd66.firebaseio.com/Soldiers/" + username + ".json")
.Then(response =>
{
soldier = response;
return (soldier.name != null) ? PostToDatabase() : HandleError();
});
return true;
}
}
public void PostToDatabase()
{
Soldier soldier = new Soldier(soldierName, selectedSkin);
Give(soldier, GetAllIdFromList(allWeaponsList));
RestClient.Put("https://minisoldiers-fdd66.firebaseio.com/Soldiers/" + soldier.name + ".json", soldier)
.Then(res => SceneManager.LoadScene("HouseScene"));
}
public void StartDataQuery()
{
DatabaseQueries.CheckIfUsernameExists(soldierName)
}
public void HandleError()
{
soldierNameWarning.text = "Ce soldat existe déjà !";
}