- Home /
yield return WWW; not returning
Been trying to implement a highscore system, and I was just testing/starting out with the standard highscore found in the wiki.
http://www.unifycommunity.com/wiki/index.php?title=Server_Side_Highscores
Been having issues though, it wouldn't add scores to the database. Using identical php scripts from the wiki except for own values and rewrote the javascript to c#.
IEnumerable PostScore(string username, int score) { var hash = Md5Sum(username + score + _secretKey); var highscoreUrl = _addScoreUrl + "name=" + WWW.EscapeURL(username) + "&score=" + score + "&hash=" + hash;
var hsPost = new WWW(highscoreUrl);
yield return hsPost; // Wait until the download is done
if(hsPost.error != null)
{
print("There was an error posting the high score: " + hsPost.error);
}
}
Never worked. I checked to make sure md5 matched, i printed off the url and manually used it and it all worked fine, but when that script runs it never added a score to the database.
I finally decided to test it without the yield
void PostScore(string username, int score) { var hash = Md5Sum(username + score + _secretKey); var highscoreUrl = _addScoreUrl + "name=" + WWW.EscapeURL(username) + "&score=" + score + "&hash=" + hash;
var hsPost = new WWW(highscoreUrl);
}
Works fine, adds the score to the database but I'd obviously like the added functionality and options of using the yield so could anyone explain to me why it's not working?
Answer by Mike 3 · Jan 28, 2011 at 12:41 PM
Make sure you're calling the function with StartCoroutine(PostScore(username, score));
If you leave that off, you'll end up with it just not doing anything in that function
If that's not it, i would suggest a slightly different yield:
while (!hsPost.isDone && hsPost.error == null)
{
yield return null;
}
That should pause until the download is complete or you get an error
thank you, thank you, thank you! I knew I forgot something simple (StartCoroutine).
Yeah, in C# you have to explicit start a coroutine and that's why i prefer C#. It's clearer what happens. Strict typing also just helps to avoid logical errors. In JS everything seems to be defined a bit blurry.
Your answer
Follow this Question
Related Questions
unity c# RPC 0 Answers
opening a webpage? 1 Answer
using Application.OpenURL for opening new tab? 2 Answers
how to test your multiplayer game 2 Answers
The name 'Joystick' does not denote a valid type ('not found') 2 Answers