Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by william9518 · Sep 05, 2013 at 12:33 AM · javascriptfunctionintegerreturn value

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

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image reefwirrax · Dec 08, 2014 at 07:10 PM 0
Share

it has a yield statement in it.

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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);
  ...
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image william9518 · Sep 05, 2013 at 01:19 AM 0
Share

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'

avatar image aldonaletto · Sep 06, 2013 at 03:03 AM 0
Share

I posted some ways to get the result from your coroutine - take a look at my edited answer.

avatar image
0

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image william9518 · Sep 05, 2013 at 12:51 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

20 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges