return only after StartCoroutine finished unity

116 views Asked by At

I have some code that requests some data from a webserver. I do this in a function that takes some parameters and should return the returned data. See below for the code:

public string[] GetDBValues(int type, string[] data){
    string base_url = "http://localhost/artez/onderzoeks_opdracht/interface_test/get_elements.php";
    string typePrefix = "?type=";
    string dataPrefix = "data[]=";
    string uriString = base_url + typePrefix + type;
    foreach (string dataElement in data){
        uriString += "&" + dataPrefix + dataElement;
    }
    string[] db_objects = null;
    StartCoroutine(UrlData(uriString, (result) =>{
        string clean_data = Regex.Replace(result, @"[""\[\]]", "");
        db_objects = clean_data.Split(',');

    }));
    return db_objects;
}

but when i run this code the returned data is empty because the data empty array is returned before it is filled with the data from the webserver. How do i make it so the data is only returned after the web-request has finished?

I tried setting the return in the StartCoroutine but then the function wont compile.

If anything is unclear let me know so i can clarify!

0

There are 0 answers