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 FrHaYwOrKs · Apr 14, 2011 at 01:56 PM · wwwfunctionyieldwait

Yield And Return

I started working with the WWW class to retrieve levels data from my host, and obviously I need to wait for the download to complete before moving on with further line codes.

Now, being my first project my code is a little confused sometimes, I have several functions calling each others to retrieve data and stuff like this, and that posed me in front of a little problem.

Let's say I have function A that acts like a main engine for the system, calling function B to retrieve a data, so function B is supposed to RETURN a value...

Now let's say that this function B to evaluate the data to return must check an url result, so it will instantiate a WWW object and wait for it to be ready.

Here come the problem, a function with a yield inside it cannot return a value...

I worked the whole thing around by using global variables instead of return values, so at this point now I'm able to make it work, which is pretty good!

But is there a way to make it work without passing to global variables as I did?

Like another way to wait in a blocking manner (because the yield start a coroutine, if I have understood it right) for a download to be completed?

Comment
Add comment
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

2 Replies

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

Answer by yoyo · Apr 14, 2011 at 04:57 PM

Seems like you need to rework the design of function A, so it doesn't require data to be available immediately. While you could get B to block until the data is ready and then return it, that won't create a good experience for your user. Dealing with asynchronous processes is a fact of life in game development (and application programming generally), so you want to design your game to deal with systems that may not return data immediately.

The simplest way to handle this for level loading is to have some sort of loading screen. The loading screen could itself be a very small Unity "level" that's always in memory. When you leave level 1, you switch to the loading screen, request the data for level 2, unload level 1, and display the loading screen until level 2 data is ready.

In your case, function A would continue to update the loading screen until it hears back from B that the WWW level download has completed.

Comment
Add comment · Show 3 · 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 FrHaYwOrKs · Apr 15, 2011 at 07:39 AM 0
Share

But in that case function B is always saving the WWW level download to a global variable, so I'm back to start, right? :) (I'm already using this kind of "persistent loader" system, but thanks anyway! ^^)

avatar image yoyo · Apr 15, 2011 at 03:23 PM 0
Share

Not really. A creates an instance of B, B initiates the loading process, A polls B.result until it's not null. No global variables in sight.

avatar image FrHaYwOrKs · Apr 19, 2011 at 10:37 AM 0
Share

I misunderstood then... that sounds right! Thanks! :)

avatar image
0

Answer by Proclyon · Apr 14, 2011 at 02:30 PM

I am not able to give you a clear answer on how to solve this but with a work-around make a method C that does the returning for you when YOU want it to return and let method B do all the work/logic.

Also using globals is pretty much a common dilemma in programming. When using an OOP(Object-Oriented-Programming) design globals go straight around that by being a unique object that can not be instantiated and have a sort of "we only play OUR way" mentality. It's great and easy at first, just like singleton, but if you take the easy read to often you can end up with the worst headache code of your lifetime.

IF INTERESTED: To explain this point I suggest you look into the keywords. OOP Design, Design Patterns (Particularly singleton), Static and global variables. Those should lead to endless resources on just this topic.

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 FrHaYwOrKs · Apr 14, 2011 at 02:49 PM 0
Share

I'm aware of how OOP works, and of global, static and stuff like that... :)... I just wanted to know how to deal with the yield/return problem without using global, exactly because I know that it's not the best solution, usually... :)

avatar image Proclyon · Apr 14, 2011 at 06:10 PM 0
Share

well as I tried to explain , all I can give you as an answer to that specifically right now is the workaround using a method C and split the tasks

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

No one has followed this question yet.

Related Questions

Yield function goes slow sometimes/differently? 2 Answers

How to properly use Yield for a coroutine? 3 Answers

Calling function in another script dying without error 0 Answers

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Can't find Yield, LoadImageIntoTexture doesn't work with Texture 1 Answer


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