Home > Blockchain >  Wait until TestName != "NoName"
Wait until TestName != "NoName"

Time:11-03

I am getting the player names from the server and it takes a while, I have added a function that should wait for the TestName variable to be overwritten by the player name

StartTask() is invoked via another function but does not wait until TestName != "NoName"

private string TestName = "NoName";

void OnLeaderboardGet(GetLeaderboardResult result)
{
    foreach (var item in result.Leaderboard)
    {
        TestName = "NoName";
        GetPlayerProfile(item.PlayFabId); //This function gets the player's nickname, but with a delay of a few seconds

        StartCoroutine(StartTask());

        Leaders  = (TestName   "\n");
    }
    Debug.Log(Leaders);
}

IEnumerator StartTask()
{
    Debug.Log("Start");

    yield return new WaitUntil(CheckName);

    Debug.Log("Finish");
}

bool CheckName(){
    if (TestName == "NoName")
    {
        return false;
    }
    else 
    {
        return true;
    }
}

CodePudding user response:

You didn't post your server request code but it also should be async operation and it should be able work like this way.

Your new yield statement could also be another async operation. that should fix your problem in case it is GetPlayerProfile

//you can ignore that with lambda like  = result => StartCoroutine(OnLeaderboardGetCoroutine(result));
void OnLeaderboardGet(GetLeaderboardResult result)
{
   StartCoroutine(OnLeaderboardGetCoroutine(result));
}


IEnumerator OnLeaderboardGetCoroutine(GetLeaderboardResult result)
{
    foreach (var item in result.Leaderboard)
    {
        TestName = "NoName";
        yield return GetPlayerProfile(item.PlayFabId); //This function gets the player's nickname, but with a delay of a few seconds

        //StartCoroutine(StartTask());

        Leaders  = (TestName   "\n");
    }
    Debug.Log(Leaders);
}
  • Related