C# Coroutine for WWW

Is there an example somewhere of how to write a coroutine in C# to wait on a WWW response? I’ve been using Invoke to do it, because I just can’t get the coroutine to work. What would the coroutine equivalent of this be?:

		void GetResponse()
	{
		if(!post.isDone)		
			Invoke("GetResponse", 0.3f);
			
		else if(post.error != null)
			Debug.Log(post.error);

		else 
			Debug.Log("Submission complete");		
	}

(post is a WWW object)

Something like this?

// Whenever you need to do a WWW request
StartCoroutine( HandleWWW( myWWW ) );

public IEnumerator HandleWWW( WWW theWWW )
{
    // pre-www actions //
    yield theWWW;
    // post-www actions //
}

Is that C#? The function won’t compile for me.

Ah sorry. Untested code and its been a while since I toyed with the WWW class. Regardless, the yield statement should be “yield return theWWW” - still untested though :wink:

That’s better…thanks!

I have some weird learning defect about coroutines…it’s very annoying.

Wild and crazy thought here, but would using a delegate work better (or at least equivalently)?

I don’t see where WWW allows you to feed it a delegate? Perhaps I’m misunderstanding your idea?

I meant as an alternative approach, maybe you could use a delegates instead of a coroutine, ie, fire an event when the WWW request completes. It might be a simpler way that would yield (ha!) the same effect.

The OP seemed to be struggling with coroutines, and perhaps it’s not the only way to go about it. Then again, I’m a Unity Noob and maybe coroutines are the “Unity Way” for this type of thing. :slight_smile:

I ended up using both:

using UnityEngine;
using System.Collections;

public class ScoreTable : MonoBehaviour 
{
	enum State {Idle, GettingText, SubmittingScore};
	State state;
	
	public delegate void CallBack(bool result);
	CallBack callBack;
	
	public string error;
	
	//------------------------------------------------------------
	public void Post(string pName, int score, string url)
	{ Post(pName, score, url, null); }

	//------------------------------------------------------------
	public void Post(string pName, int score, string url, CallBack cb)
	{
		callBack = cb;
	  string hash = QMd5.Sum(pName + score + iPhoneSettings.uniqueIdentifier + "float i");
	
		url += "?name=" + WWW.EscapeURL(pName)
			+ "&score=" + score
			+ "&UDID=" + iPhoneSettings.uniqueIdentifier
			+ "&hash=" + hash;

		StartCoroutine( HandleWWW( new WWW(url) ) );
	}

	//------------------------------------------------------------
	IEnumerator HandleWWW( WWW r )
	{
		state = State.SubmittingScore;  

		yield return r;

		state = State.Idle;	
			
		if(callBack != null)
		{
			callBack( r.error == null );
			callBack = null;
		}
		
		error = r.error;
	}
			
}

Very nice - one point: Why rely on a global variable to hold your callback reference rather than just passing along as a parameter to your coroutine?

Sounds good…the way it is now is an artifact of the Invoke method I was using before.

Thanks for the tip!