- Home /
Return result of the IEnumerator.
Hi all, I did a little update on the code
Been a long time since I have asked anything but this one has bugged me for a while.
I will keep it simple, is this possible:
void Start(){
WWW data = myDataClass.StartCoroutine(myDataClass.getData(data_id));
}
Basically I want the Coroutine / IEnumerator in another class so I can better manage the code. I have looked at some of the solutions on the forum but none resemble the simplicity of the above.
this works fine but cannot catch a response as far as I can see:
myDataClass.StartCoroutine(myDataClass.getData(data_id));
I did solve this in the past using a notification manager, and I will probably look to use delegation but not sure as yet if it’s the only or best way to go about it.
If I could just do the WWW data = myDataClass.StartCoroutine(myDataClass.getData(data_id)); then it seems like the best way.
Answer by fafase · Jun 11, 2015 at 07:52 AM
WWW already has a similar approach I would guess.
WWW www = new WWW(url);
yield return www:
there you have your www object with the data.
EDIT:
WWW www = null;
myDataClass.StartCoroutine(myDataClass.getData(data_id, result => target = result));
public class MyDataClass:MonoBehaviour
{
public IEnumerator getData( string data_id, Action<WWW>action)
{
WWW www = new WWW(data_id);
yield return www;
action(www);
}
}
hmmm I must be missing something here, will update the question code.
This is the same as your code example, except the WWW is no more on the receiving end but as a parameter.
That look like it!! .. Will test it out later and mark as answered.
The method is stripped from any important check, like if the www has succeeded and what should happen in case of failure. Think about it.
Answer by Kiwasi · Jun 11, 2015 at 07:58 AM
Typically I would wrap the data and coroutine in a class together. The class can contain a bool isDone and another field for the result. The WWW class in Unity is a good example of this.
I did an update on the question code, it was not clear on what I'm asking.
Your answer
Follow this Question
Related Questions
Can I use StartCoroutine inside of the same coroutine with a new variable? 0 Answers
yield return M() vs yield return StartCoroutine 2 Answers
Coroutine error with result 1 Answer
Moving Coroutines To One Method 1 Answer
Item duration doesn't stack 3 Answers