#Calling Coroutine

1 messages · Page 1 of 1 (latest)

viscid coral
#

Right now, I have 3 scripts.
The first script is the "backend" that downloads a Json file from a URL, using UnityWebRequest and hence IEnumerator.
The second script is the "frontend" that simulates what a client may do to access the data.
Then the third script is the "API" that receives commands from the frontend and access the backend to return the required data.

Right now, the frontend is a MonoBehaviour, while the API and the backend are just a class.
However, since the API is not a MonoBehaviour, it cannot call StartCoroutine and thus cannot initialize the backend.
Right now, I have to call StartCoroutine in the front end, which kinda defeats the purpose of having the middle API.

Is there any workaround for this? Or can I do UnityWebRequest without a Coroutine?

rapid cobalt
#

Not sure if this violates your architecture, but you could pass the mono as a param when you call your API, and run the coroutine that way, you may have to make sure the mono script calls some coroutine cleanup on exit, but for example, would this work for what your after?

public class SomeAPI
{
public void DoEvent(MonoBehaviour starter)
{
if(starter != null) {
starter.StopAllCoroutines();
starter.StartCoroutine(WebRequestRoutine());}
}

IEnumerator WebRequestRoutine()
{ ... }
}

Then you could call your SomeAPI.DoEvent(this); from the frontend Mono script, and maybe subscribe to a callback for the API to fire when the coroutine ends, if the frontend needs to know that - this way, the coroutine is still in your non-mono API, but called with a mono, since it sounds like you would need to go through a mono anyway for the frontend to access your API

viscid coral
#

Aye yo this works!
Passing the frontend MonoBehaviour as a param for the API to StartCoroutine on.
This is wesmart