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.

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