- Home /
Function cannot return int!?
So basically I am doing a little mySQL retrieving stuff, and I made a function that would return the value gotten from the database. My function looks like this:
function RetrieveHunger() : int {
var formHunger : WWWForm = new WWWForm();
formHunger.AddField("user", user);
var wHunger : WWW = new WWW("", formHunger);
yield wHunger;
if(wHunger.error == null){
return parseInt(wHunger.text);
} else {
Debug.Log("Retrieve Failed: " + wHunger.error);
}
return -1;
}
aaaannnndd Unity rewards me with the error "Return type 'int' cannot be used on a generator. Did you mean 'IEnumerator'? You can also use 'System.Collections.IEnumerable' or 'object'." So I cannot return a function as an integer? That is stupid. Any help is appreciated.
Thanks in advance, william9518
Answer by aldonaletto · Sep 05, 2013 at 01:05 AM
A coroutine can only return type IEnumerator. I suspect that this is mainly due to the way it's implemented, but anyway it would be complicated to return any useful value: the coroutine runs "in the background", thus the return instruction may be executed a long time after the caller routine has finished.
EDITED: There are several possible ways to retrieve the result value from the coroutine, but you must keep in mind that the result may take an unpredictable time to get ready. You can use a flag to inform whether the result is ready, store the result in a member variable and read it when it's valid:
private var result: int;
private var resultReady = false;
function RetrieveHunger(){
resultReady = false; // result isn't ready yet
var formHunger : WWWForm = new WWWForm();
formHunger.AddField("user", user);
var wHunger : WWW = new WWW("", formHunger);
yield wHunger;
// store the result:
if wHunger.error == null){
result = parseInt(wHunger.text);
} else {
result = -1;
}
resultReady = true; // result ready now
}
function Start(){
RetrieveHunger(); // start RetrieveHunger
}
function Update(){
if (resultReady){ // when result ready, display it:
guiText.text = "Result = " + result;
}
}
Another possibility is to chain to RetriveHunger, if the caller is a coroutine too:
private result: int;
function Start(){ // yield makes Start a coroutine
yield RetrieveHunger(); // call RetrieveHunger and wait for its completion
guiText.text = "Result = " + result; // display the result
}
Finally, you could simply call a callback function at the end of the coroutine. If you want to define which function to call when the coroutine finishes, pass it as a parameter:
function RetrieveHunger(callbackFunction: Function){
var formHunger : WWWForm = new WWWForm();
formHunger.AddField("user", user);
var wHunger : WWW = new WWW("", formHunger);
yield wHunger;
if (wHunger.error == null){
callbackFunction(parseInt(wHunger.text));
} else {
callbackFunction(-1);
}
}
The callback function itself could be something like this:
function GetResult(res: int){
guiText.text = "Hunger = " + res;
}
And it should be passed like below:
...
RetrieveHunger(GetResult);
...
Could I be able to use a Invoke function ins$$anonymous$$d? Because this function will be called in other classes that need to get the value back and I don't know how the wrapping would return the value to the 'caller'
I posted some ways to get the result from your coroutine - take a look at my edited answer.
Answer by Eric5h5 · Sep 05, 2013 at 12:42 AM
It's not stupid...a coroutine must return IEnumerator; it can't return int. Typically you'd call a delegate when the coroutine is done.
Could I be able to use a Invoke function ins$$anonymous$$d? Because this function will be called in other classes that need to get the value back and I don't know how the wrapping would return the value to the 'caller'
Your answer
![](https://koobas.hobune.stream/wayback/20220613113611im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to use functions between two scripts 2 Answers
Restart this script 3 Answers
Camera Move 1 Answer
How to call a function from a script in another scene 5 Answers
Player Health drops fast, how to pause after damage 0 Answers