- Home /
Help returning a variable from this script
I am trying to make a reusable script which retrieves the text from a url. I cannot figure out a way to return the data variable in DataRetrieved to my other script where I am calling the getData function. I am aware they are using void instead of string right now, I reverted it to how it originally was since I couldn't figure it out.
The script I am calling:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System;
using UnityEngine.Networking;
public class GetDatabaseInfo : MonoBehaviour
{
public void getData(string url)
{
StartCoroutine(GetRequest(url,DataRetrieved));
}
private IEnumerator GetRequest(string url,System.Action<string> callback)
{
UnityWebRequest request = UnityWebRequest.Get(url);
yield return request.SendWebRequest();
if(request.isNetworkError || request.isHttpError)
{
Debug.LogError(request.error + " ...Cannot retrieve database information");
}
else
{
callback(request.downloadHandler.text);
}
}
private string DataRetrieved(string data)
{
Debug.Log(data); //return data variable here
}
}
The script I am calling it from:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System;
using UnityEngine.Networking;
public class ValidateRegistration : MonoBehaviour
{
GetDatabaseInfo getDatabaseInfo;
private void validate()
{
string url = "http://localhost:8080/KingdomRushB_Accounts/Accounts.php";
getDatabaseInfo=GameObject.FindGameObjectWithTag("DatabaseInfo").GetComponent<GetDatabaseInfo>(); //(allowing me to call the other script's functions from this script)
string data = getDatabaseInfo.getData(url);
}
How can I get the data variable to return? I would really appreciate if anyone can help me out
Answer by Namey5 · Jan 24, 2021 at 09:33 AM
Well you're already getting the data back, you're just passing it to the wrong function. If you instead pass a callback in the place of DataRetrieved() you can access the data when it's ready;
public void getData(string url, System.Action<string> callback)
{
StartCoroutine (GetRequest (url, callback));
}
private IEnumerator GetRequest(string url,System.Action<string> callback)
{
UnityWebRequest request = UnityWebRequest.Get(url);
yield return request.SendWebRequest();
if(request.isNetworkError || request.isHttpError)
{
Debug.LogError(request.error + " ...Cannot retrieve database information");
}
else
{
callback(request.downloadHandler.text);
}
}
...
private void validate()
{
string url = "http://localhost:8080/KingdomRushB_Accounts/Accounts.php";
getDatabaseInfo = GameObject.FindGameObjectWithTag ("DatabaseInfo").GetComponent<GetDatabaseInfo>(); //(allowing me to call the other script's functions from this script)
getDatabaseInfo.getData(url, onGetData);
}
private void onGetData (string data)
{
//Use the data here
}
In fact, you don't even really need a dedicated function; C# has support for anonymous functions in the form of lambdas;
private void validate()
{
string url = "http://localhost:8080/KingdomRushB_Accounts/Accounts.php";
getDatabaseInfo = GameObject.FindGameObjectWithTag ("DatabaseInfo").GetComponent<GetDatabaseInfo>(); //(allowing me to call the other script's functions from this script)
getDatabaseInfo.getData(url, (string data) =>
{
//Alternatively, use the data here
});
}
Just remember that this is asynchronous and so will most likely not happen in the same frame and therefore you cannot depend on the data being immediately available.
Your answer
Follow this Question
Related Questions
In coroutin function,if(x) { yield return a } . if(y) { yield return b }. Is it possible? 0 Answers
How to wait for a co routine to finish when calling from a method 1 Answer
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
What part of the delegate gets passed back?The return type? 1 Answer