Public Statics not returning anything?

I have a public static string set up to get some basic content from a page:

public static string GetAuthCode(string url)
        {
            WWW www = new WWW(url);
            return www.text;
        }

But it doesn’t return anything when using it like this:

authCode = FTBAuth.FTBAuth.GetAuthCode("http://com.areonline.co.uk/wp-content/plugins/StatSet/Valid8.txt");

The first bit of code is in an accessible namespace, which can be seen in the other script, it just doesn’t work; no errors, just doesn’t do anything.

Any ideas as to what I am doing wrong?

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.

Speed of light and all…

2 Likes

OK, I now have this:

[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’.

How exactly do you use WaitForSeconds AND return a string inside an IEnumerator?
They both use the word return?

you don’t wait for seconds, you wait for the WWW

you don’t return a string, you return the WWW

the problem is you have this WWW inside a static function… and I don’t know why. Why did you put those 2 lines of code inside ‘GetAuthCode’?

Not sure why I shouldn’t be using those 2 lines, that’s how it works :stuck_out_tongue:
They were static so I could call them from another script.

not that you use those 2 lines, that you do them IN THE STATIC METHOD.

You need to yield the WWW. You can’t when it’s locked up in that static method.

Here… I’ll hold your hand the entire way…

    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;
    }

Do you get it now?

YOU NEED TO YIELD THE WWW!

1 Like

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.

void Auth(string url, Action<string> action)
{
    StartCoroutine(DoAuth(url, action));
}

IEnumerator DoAuth(string url, Action<string> action)
{
    WWW www = new WWW(url);
    yield return www;
    if (action != null)
        action(www.text);
}

// calling
string result;
Action<string> a = (wText) => { result = wText; }
Static.Auth("www.google.com", a);

~cutting short~

Or just do what @lordofduct did :slight_smile: I was more thinking of the static method as being some sort of rudimentary authentication API

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.

2 Likes

http://wiki.unity3d.com/index.php?title=Server_Side_Highscores