x


Troubles With Getting a String from a WWW Object

Hi guys,

I'm sorry if this has been asked and answered before, but I just can't figure this out and my searching has turned up nothing I can get to work in my situation.

So my team and I are developing a game for Unity iOS and, as it's essentially a social game, we need to be able to send things back and forth from a server.

So, in the scenario we're currently having trouble with we have a log-in form that takes in a number. The number then has to be sent to the server, and the server then has to send back the list of cases (as in a medical case) associated with said number so it can then be parsed from a string stored on the server.

public IEnumerator returnCases(int uID)
{
    WWWForm userIDForm = new WWWForm();
    userIDForm.AddField("user_id", uID);

    WWW cases = new WWW(ip + "/ShowCases.php", userIDForm);

    yield return cases.text;
}

This code is called in another class's method upon pressing a log-in button.

Really what we want to do is get the data associated with uID and return a string that we can then parse and use in the game. But this doesn't seem to be possible because this is not returning a string. However, it is not possible to yield with a return type of string (or anything not of the iterator interface type).

Our current solution was to do a busy-wait loop, which apparently will not work on iOS so we really need some form of a solution that will suit our needs.

more ▼

asked Jun 21 '12 at 06:52 PM

fryedrycestyle gravatar image

fryedrycestyle
15 1 1 1

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

You need to yield cases so that the request is processed, then cases.text will contain the return value on the line after the yield.

edit

WWW is asynchronous so something like this:

public IEnumerator getCases(int uID, Action<string> complete)
{
    WWWForm userIDForm = new WWWForm();
    userIDForm.AddField("user_id", uID);

    WWW cases = new WWW(ip + "/ShowCases.php", userIDForm);
    yield return cases;    
    complete(cases.text);
}

The to call it:

StartCoroutine(getCases(id, (cases)=>{
    //Do something with the cases here
});
more ▼

answered Jun 21 '12 at 06:54 PM

whydoidoit gravatar image

whydoidoit
33.7k 14 23 105

And you need to start the routine with StartCoroutine()

Jun 21 '12 at 06:55 PM whydoidoit

Like this?

IEnumerator returnCases(WWW cases)
{
    yield return cases;
}

public string returnCasesAsString(int uID)
{
    WWWForm userIDForm = new WWWForm();
    userIDForm.AddField("user_id", uID);

    WWW cases = new WWW(ip + "/ShowCases.php", userIDForm);

    StartCoroutine(returnCases(cases));

    return cases.text;
}

Because I tried this and got the UnityException saying "WWW is not ready downloading yet".

Jun 21 '12 at 07:05 PM fryedrycestyle

No, you've got the wrong idea about coroutines - that isn't going to return you the string - you are going to have to do something when the string arrives later.

Jun 22 '12 at 12:17 AM whydoidoit

WWW is asynchronous so something like this:

public IEnumerator getCases(int uID, Action<string> complete)
{
    WWWForm userIDForm = new WWWForm();
    userIDForm.AddField("user_id", uID);

    WWW cases = new WWW(ip + "/ShowCases.php", userIDForm);
    yield return cases;    
    complete(cases.text);
}

The to call it:

   StartCoroutine(getCases(id, (cases)=>{
         //Do something with the cases here
      });
Jun 22 '12 at 12:20 AM whydoidoit

Yes callbacks are a good way to handle such cases.

@fryedrycestyle:
I guess you want actually a blocking read, however that's not possible and shouldn't be used since it would block the whole app / game. Unity uses only one thread for the whole scripting environment. Coroutines are a great way to have several things executed "simultaneously". A www request always takes some time and life(your game) goes one. When you yield a www object, Unity will suspend the coroutine until the download is finished.

StartCoroutine will just start the coroutine but returns immediately after the first yield. Unity's coroutine scheduler will take care of resuming it when necessary.

Jun 22 '12 at 12:43 AM Bunny83
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x548
x66

asked: Jun 21 '12 at 06:52 PM

Seen: 725 times

Last Updated: Jun 22 '12 at 03:46 PM