Getting a value outside of Coroutine
Hello, I'm actually developping a game on Unity, and I'm stuck on a problem for few days now. I think I'm doing something totally wrong but after searching again and again I found no way. I want to load my characters which are in a MySQL database. I'm using Coroutines to get the values from my PHP scripts, but it looks like I'm getting the coroutine AFTER my game loads characters, but i'm calling the coroutine before it. I'm getting the values, but I can't get them outside of my coroutine in my LoadCharacter. If i'm outputing the values IN my coroutine, I will see the value (CharacterName) that I want, but if I output the CharacterName in my LoadCharacter, it wont.
I'm sorry if I didn't explain well, but I hope that someone will help me just a little bit ! Thank you.
public CharacterSaveData[] LoadAllCharacters()
{
StartCoroutine(GetCharacters());
string[] charactersList = ZombieHunters.gameManager.CharactersList.Split ("," [0]);
List<CharacterSaveData> chars = new List<CharacterSaveData> ();
if (charactersList.Length > 0)
{
for (int i = 0; i < charactersList.Length; i++)
{
if (charactersList[i] != "")
{
CharacterSaveData ch = LoadCharacter(charactersList[i]);
if (ch.PlayerName != "")
{
chars.Add(ch);
}
}
}
CharacterSaveData[] allCharacters = chars.ToArray ();
return allCharacters;
}
return null;
}
public CharacterSaveData LoadCharacter(string charID)
{
CharacterSaveData character = new CharacterSaveData ();
StartCoroutine(LoadChar(charID));
character.PlayerName = ZombieHunters.gameManager.CharacterName;
character.CharacterIndex = ZombieHunters.gameManager.CharacterIndex;
Debug.Log("Character Name : " + ZombieHunters.gameManager.CharacterName + ". Character Index : " + ZombieHunters.gameManager.CharacterIndex);
character.CharacterKey = charID;
return character;
}
IEnumerator GetCharacters()
{
string uri = "http://localhost/keep-alive/api/getchars.php"; //
string accountID = ZombieHunters.gameManager.AccountID;
WWWForm form = new WWWForm();
form.AddField("accountID", accountID);
WWW www = new WWW (uri,form);
yield return www;
ZombieHunters.gameManager.CharactersList = www.text;
}
IEnumerator LoadChar(string _characterID)
{
string uri = "http://localhost/keep-alive/api/loadchar.php";
int characterID = Int32.Parse(_characterID);
WWWForm form = new WWWForm();
form.AddField("characterID", characterID);
WWW www = new WWW (uri,form);
yield return www;
string[] values = www.text.Split(new char[] {'\n'});
ZombieHunters.gameManager.CharacterName = values[0];
ZombieHunters.gameManager.CharacterIndex = Int32.Parse(values[1]);
Debug.Log("Load Char Character Name : " + ZombieHunters.gameManager.CharacterName + ". Character Index : " + ZombieHunters.gameManager.CharacterIndex);
}
Answer by HugoLeblanc · Oct 05, 2017 at 03:26 PM
I fixed this by using the new C# feature async-await which is new to Unity 2017. http://www.stevevermeulen.com/index.php/2017/09/23/using-async-await-in-unity3d-2017/ Here is a good web page that explains it.
Answer by Cuttlas-U · Oct 04, 2017 at 04:38 AM
hi; thats a big script :D and u did very good coding;
Im not pro at scripting but the thing i thought maybe i can help y with and worth testing is that when ever u run a corutine u dont wait for it to finish ; u continue your function but there is a small delay betwerrn runing corutines and wait for it to recive data;
so check if u wait for it things will be changed or not ;
Well thank you, I kinda knew it and did wait for it to finish yesterday, but I guess that I did wrong because it made my app wait forever. I simply created a bool and put it true when my coroutine started, false when it ended. Do you have any idea how I could wait for it by a clean way ? Anyway now I know 100% what is the problem and I thank you for that.
Your answer
![](https://koobas.hobune.stream/wayback/20220612140216im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Coroutine Error when Capturing 360 Video 0 Answers
Why Unity docs do not use New with WaitForSeconds and yield? 1 Answer
What is Image.Start() ?? and How can i reduce this function's GC.Collect 0 Answers
Heavy use of Unity API & UI Thread 0 Answers
If a variable used by a coroutine is changed, will the coroutine know while it is running? 1 Answer