WWW takes time to load the content from the remote location (the url).
Note the documentation and example:
See how it yields the WWW in a Coroutine? That’s waiting for the response to come back from the server.
In your code you immediately access the text, this is nanoseconds after creating the WWW object. There is no way data could be transferred over the internet in that time. Especially when you consider that even GOOD pings can be in the milliseconds, let alone bandwidth.
[Tooltip("The level number.")]
public int level;
[Tooltip("The amount of moves used.")]
public int moves;
[Tooltip("The amount of seconds given before a timeout.")]
public int secondsToWait;
[Tooltip("The retrieved validation code.")]
public string authCode;
[Tooltip("The status from the website (or error).")]
public string status;
void Start ()
{
StartCoroutine(WaitForCode(secondsToWait));
}
IEnumerator WaitForCode(int secondsToWait)
{
string code;
string result;
code = FTBAuth.FTBAuth.GetAuthCode("http://com.areonline.co.uk/wp-content/plugins/StatSet/Valid8.txt");
result = FTBAuth.FTBAuth.PostStats(level, moves, authCode);
yield return new WaitForSeconds(secondsToWait);
authCode = code;
status = result;
}
but it is still doing sweet FA, even when the secondsToWait variable is set to 20, which should be enough time for anyone on dial up to get a text file.
because GetAuthCode STILL returns BEFORE the WWW is complete.
Just because you call GetAuthCode during a coroutine doesn’t mean the WWW is waited on. You wait on some WaitForSeconds AFTER accessing the text property of WWW in GetAuthCode.
NO.
You must yield the WWW, then access its text field.
Also, why the heck is that encapsulated in the GetAuthCode function? Like, for what purpose is that?
Just create a WWW, yield it, then get the text property. That will be your ‘code’.
IEnumerator WaitForCode(int secondsToWait)
{
var www = new WWW("http://com.areonline.co.uk/wp-content/plugins/StatSet/Valid8.txt");
yield return www; //THIS - THIS IS CRITICAL! This makes certain that the WWW gets its result before accessing text
var code = www.text;
var result = FTBAuth.FTBAuth.PostStats(level, moves, authCode);
authCode = code;
status = result;
}
I think the issue isn’t so much that the method is static but that the method is void. You’re trying to get something immediately that cannot be got immediately. I can see two approaches right off the bat - Pass an Action to the method which sets result (should work - don’t think it being a string would mess with the closure); Or raise some events that the things who need auth subscribe to.
I was thinking the same thing possibly… but if he wasn’t understanding “the www needs to be yielded”, I wasn’t even going to broach the subject of that sort of abstraction.